使用JBPM工作流引擎測(cè)試的一個(gè)例子
本文提供使用jBPM工作流引擎測(cè)試的一個(gè)例子。
提供一個(gè)Persistence,用于存儲(chǔ)全局的變量值,方便存儲(chǔ)和獲取
- public class Persistence {
- private static Map variables = null;
- private static String tmpfile = System.getProperty("java.io.tmpdir") + "/temp.object";
- static{
- //加載文件
- try{
- if(new File(tmpfile).exists()){
- FileInputStream in = new FileInputStream(tmpfile);
- ObjectInputStream s = new ObjectInputStream(in);
- variables = (Map)s.readObject();
- }
- if(variables == null){
- variables = new HashMap();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- //設(shè)置一個(gè)變量的值
- public static void setVariable(String name,Serializable object){
- if(variables != null){
- variables.put(name, object);
- }
- try {
- FileOutputStream fos = new FileOutputStream(tmpfile);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(variables);
- oos.flush();
- oos.close();
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //獲取一個(gè)變量的值
- public static Serializable getVariable(String name){
- if(variables != null){
- return (Serializable)variables.get(name);
- }
- return null;
- }
- }
1.首先使用流程設(shè)計(jì)器,創(chuàng)建一個(gè)簡單的流程規(guī)則
2.創(chuàng)建數(shù)據(jù)庫表,創(chuàng)建流程定義對(duì)象,并部署流程定義
- //創(chuàng)建數(shù)據(jù)庫表
- public class Jbpm_01_CreateTable extends TestCase {
- public void testCreateTable(){
- JbpmConfiguration.getInstance().createSchema();
- }
- }
- //定義流程定義對(duì)象,并部署流程
- public class Jbpm_02_DeployProcessDefinition extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- public void testDeployProcessDefinition(){
- //讀取流程定義文件,得到流程定義對(duì)象
- ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("test01/processdefinition.xml");
- //可以得到流程定義的名稱
- Persistence.setVariable("processName", processDefinition.getName());
- //JbpmContext對(duì)象封裝了hibernate session 對(duì)象的功能,可以執(zhí)行與數(shù)據(jù)庫持久化相關(guān)的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- context.deployProcessDefinition(processDefinition);
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
3.定義公文Doucment 及其映射文件Doucment.hbm.xml
- public class Document {
- private int id;
- private String title;
- private String content;
- private Long processInstanceId;
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Long getProcessInstanceId() {
- return processInstanceId;
- }
- public void setProcessInstanceId(Long processInstanceId) {
- this.processInstanceId = processInstanceId;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- }
- //Document映射文件Document.hbm.xml
- <hibernate-mapping>
- <class table="tbl_document" name="Document">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="title"/>
- <property name="content"/>
- <property name="processInstanceId"/>
- </class>
- </hibernate-mapping>
4.創(chuàng)建公文并與流程實(shí)例綁定
- public class Jbpm_03_CreateDocument extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- public void testCreateDocument(){
- //JbpmContext對(duì)象封裝了hibernate session 對(duì)象的功能,可以執(zhí)行與數(shù)據(jù)庫持久化相關(guān)的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- Document doc = new Document();
- doc.setTitle("測(cè)試公文"+new Random().nextInt(9999));
- context.getSession().save(doc);
- Persistence.setVariable("docId", doc.getId());
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
5.提交公文到流程,觸發(fā)流程流轉(zhuǎn)
- public class Jbpm_05_SubmitDocument extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //提交公文到第一個(gè)環(huán)節(jié)
- public void testSubmitDocument(){
- //JbpmContext對(duì)象封裝了hibernate session 對(duì)象的功能,可以執(zhí)行與數(shù)據(jù)庫持久化相關(guān)的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- //已知公文的信息
- int docId = (Integer)Persistence.getVariable("docId");
- Document doc = (Document)context.getSession().load(Document.class, docId);
- long processInstanceId = doc.getProcessInstanceId();
- ProcessInstance processInstance = context.getProcessInstance(processInstanceId);
- //觸發(fā)流程實(shí)例流轉(zhuǎn)到下一個(gè)環(huán)節(jié)
- processInstance.signal();
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
6.查看公文所處的當(dāng)前任務(wù)節(jié)點(diǎn)
- public class Jbpm_06_CurrentNode extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //公文當(dāng)前所處的環(huán)節(jié)
- public void testCurrentNode(){
- //JbpmContext對(duì)象封裝了hibernate session 對(duì)象的功能,可以執(zhí)行與數(shù)據(jù)庫持久化相關(guān)的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- //已知公文的信息
- int docId = (Integer)Persistence.getVariable("docId");
- Document doc = (Document)context.getSession().load(Document.class, docId);
- long processInstanceId = doc.getProcessInstanceId();
- ProcessInstance processInstance = context.getProcessInstance(processInstanceId);
- String currentNode = processInstance.getRootToken().getNode().getName();
- System.err.println("公文【"+doc.getTitle()+"】當(dāng)前所處的環(huán)節(jié)" +
- "是:"+currentNode+",流程實(shí)例是否已結(jié)束?"+processInstance.hasEnded());
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
7.獲取流轉(zhuǎn)個(gè)某個(gè)參與者處待處理的任務(wù)列表
- public class Jbpm_07_SearchMyTaskList extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //搜索流轉(zhuǎn)到某個(gè)參與者手上的公文列表
- public void testSearchMyTaskList(){
- //JbpmContext對(duì)象封裝了hibernate session 對(duì)象的功能,可以執(zhí)行與數(shù)據(jù)庫持久化相關(guān)的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- printTask(context,"張三");
- printTask(context,"李四");
- printTask(context,"王五");
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- private void printTask(JbpmContext context,String actorId){
- List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
- for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
- TaskInstance ti = (TaskInstance) iter.next();
- Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("documnt");
- Document doc = (Document)context.getSession().load(Document.class, docId);
- System.err.println("正在等待【"+actorId+"】審批的公文是:"+doc.getTitle());
- }
- }
- }
8.參與者執(zhí)行審批操作,觸發(fā)流程流轉(zhuǎn)到下一個(gè)環(huán)節(jié)
- public class Jbpm_08_NextNode extends TestCase {
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
- //從一個(gè)TaskNode的中間節(jié)點(diǎn)出發(fā),觸發(fā)流程流轉(zhuǎn)到下一個(gè)環(huán)節(jié)
- public void testNextNode(){
- //JbpmContext對(duì)象封裝了hibernate session 對(duì)象的功能,可以執(zhí)行與數(shù)據(jù)庫持久化相關(guān)的操作
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- //*******************************************
- //某某用戶要將其手上的某某公文提交到下一個(gè)環(huán)節(jié)
- //*******************************************
- //已知要提交的公文
- Integer docId = (Integer)Persistence.getVariable("docId");
- nextStep(context,"張三",docId);
- nextStep(context,"李四",docId);
- nextStep(context,"王五",docId);
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- private void nextStep(JbpmContext context,String actorId,Integer docId){
- List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
- for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
- TaskInstance ti = (TaskInstance) iter.next();
- Integer _docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("documnt");
- //找到對(duì)應(yīng)的任務(wù)實(shí)例
- if(docId.equals(_docId)){
- Document doc = (Document)context.getSession().load(Document.class, docId);
- //參與者的任務(wù)已經(jīng)處理完成,需要結(jié)束這個(gè)任務(wù)實(shí)例
- //這個(gè)動(dòng)作,在缺省的情況下,會(huì)觸發(fā)對(duì)應(yīng)的Token的signal()方法,即流向下一個(gè)環(huán)節(jié)
- ti.end();
- System.err.println("公文【"+doc.getTitle()+"】已被【"+actorId+"】審批完成,已提交到下一個(gè)環(huán)節(jié)");
- }
- }
- }
- }
【編輯推薦】