Jbpm4.3 學習筆記一

sz_bdqn發表於2011-04-12

Jbpm4.3 學習筆記(一)

 

 

1.       jbpm建立資料庫

package com.langsi.jbpm;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.ProcessEngine;

 

public class Test1 {

       /**

        * 建立JBPM資料庫表

        */

       public static void main(String[] args) {

             

              Configuration configuration = new Configuration();

              //建立流程引擎

              @SuppressWarnings("unused")

              ProcessEngine processEngine = configuration.buildProcessEngine();

       }

 

}

 

2 流程一first.jpdl.xml

2.1 流程定義檔案

<?xml version="1.0" encoding="UTF-8"?>

<process name="first" xmlns="http://jbpm.org/4.3/jpdl">

   <start g="222,38,48,48" name="start1">

      <transition g="-59,-17" name="to state1" to="state1"/>

   </start>

   <state g="197,174,92,52" name="state1">

      <transition name="to end1" to="end1" g="-47,-17"/>

   </state>

   <end g="220,350,48,48" name="end1"/>

</process>

2.2 流程圖first.png

2.3 部署程式

package com.langsi.jbpm;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.RepositoryService;

 

public class Test2 {

 

       /**

        * 完成流程的部署功能

        */

       public static void main(String[] args) {

              //1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              //2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              //3、獲得資料倉儲服務物件

              RepositoryService repositoryService =  processEngine.getRepositoryService();

              //4、建立部署例項物件

              NewDeployment newDeployment  = repositoryService.createDeployment();

              //5、從類路徑下面增加流程部署檔案到部署例項物件

              newDeployment = newDeployment.addResourceFromClasspath("first.jpdl.xml");

              //6完成流程的部署

              String processNameString = newDeployment.deploy();            

              System.out.println(processNameString);            

       }

}

 

2.4 獲取流程定義

package com.langsi.jbpm;

 

import java.util.Iterator;

import java.util.List;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessDefinition;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.RepositoryService;

 

public class Test3 {

       /**

        * 完成流程的執行

        */

       public static void main(String[] args) {

              // 1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              // 2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              // 3、獲得資料倉儲服務物件

              RepositoryService repositoryService = processEngine

                            .getRepositoryService();

              // 4、建立部署例項物件

              NewDeployment newDeployment = repositoryService.createDeployment();

              // 5、從類路徑下面增加流程部署檔案到部署例項物件

              newDeployment = newDeployment

                            .addResourceFromClasspath("first.jpdl.xml");

              // 6完成流程的部署

              newDeployment.deploy();

              //7RepositoryService建立流程定義查詢介面

              List<ProcessDefinition> list = repositoryService

                            .createProcessDefinitionQuery().list();

              for (Iterator<ProcessDefinition> iterator = list.iterator(); iterator.hasNext();) {

                     ProcessDefinition processDefinition = (ProcessDefinition) iterator

                                   .next();

                     System.out.println(processDefinition.getDeploymentId()+"---"

                                   +processDefinition.getId()+"---"+processDefinition.getName());

              }

       }

}

 

2.5 流程開始執行

package com.langsi.jbpm;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.ProcessInstance;

 

public class Test4 {

       public static void main(String[] args) {

              // 1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              // 2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              //3、獲得流程執行服務物件

              ExecutionService executionService = processEngine.getExecutionService();

              //4、流程執行服務物件根據流程定義執行,獲得流程例項

              ProcessInstance processInstance = executionService.startProcessInstanceByKey("first");

              System.out.println("processInstance Id " +processInstance.getId());

       }

}

 

2.6 流程執行下一步

package com.langsi.jbpm;

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.ProcessInstance;

import org.jbpm.api.RepositoryService;

 

public class Test5 {

       public static void main(String[] args) {

              //1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              //2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              //3、獲得資料倉儲服務物件

              RepositoryService repositoryService =  processEngine.getRepositoryService();

              //4、建立部署例項物件

              NewDeployment newDeployment  = repositoryService.createDeployment();

              //5、從類路徑下面增加流程部署檔案到部署例項物件

              newDeployment = newDeployment.addResourceFromClasspath("first.jpdl.xml");

              //6完成流程的部署

              newDeployment.deploy();

              //7、獲得流程執行服務物件

              ExecutionService executionService = processEngine.getExecutionService();

              //8、流程執行服務物件根據流程定義執行,得到流程例項

              ProcessInstance processInstance = executionService.startProcessInstanceByKey("first");

              //9、使用流程執行服務物件觸發流程例項往下一節點走,產生一個新的流程例項物件

              ProcessInstance processInstance2 = executionService.signalExecutionById(processInstance.getId());       

              System.out.println(processInstance2.isEnded());

       }

}

 

3 secondprocess.jpdl.xml 第二個流程

3.1 流程定義檔案secondprocess.jpdl.xml

<?xml version="1.0" encoding="UTF-8"?>

<process name="secondprocess" xmlns="http://jbpm.org/4.3/jpdl">

   <start name="start1" g="272,38,48,48">

      <transition name="to state1" to="state1" g="-59,-17"/>

   </start>

   <state name="state1" g="419,90,92,52">

      <transition name="to state2" to="state2" g="-59,-17"/>

   </state>

   <state name="state2" g="526,202,92,52">

      <transition name="to state3" to="state3" g="-59,-17"/>

   </state>

   <state name="state3" g="440,355,92,52">

      <transition name="to end1" to="end1" g="-47,-17"/>

   </state>

    <end name="end1" g="339,428,48,48"/>

</process>

3.2 流程圖

3.3 流程測試

package com.langsi.jbpm;

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.ProcessInstance;

import org.jbpm.api.RepositoryService;

 

public class Test6 {

       public static void main(String[] args) {       

              //1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              //2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              //3、獲得資料倉儲服務物件

              RepositoryService repositoryService =  processEngine.getRepositoryService();

              //4、建立部署例項物件

              NewDeployment newDeployment  = repositoryService.createDeployment();

              //5、從類路徑下面增加流程部署檔案到部署例項物件

              newDeployment = newDeployment.addResourceFromClasspath("secondprocess.jpdl.xml");

              //6完成流程的部署

              newDeployment.deploy();

              //7、獲得流程執行服務物件

              ExecutionService executionService = processEngine.getExecutionService();

              //8、流程執行服務物件根據流程定義執行,得到流程例項

              ProcessInstance processInstance = executionService.startProcessInstanceByKey("secondprocess");

              //9、使用流程執行服務物件觸發流程例項往下一節點走,產生一個新的流程例項物件

              processInstance = executionService.signalExecutionById(processInstance.getId());

              processInstance = executionService.signalExecutionById(processInstance.getId());

              processInstance = executionService.signalExecutionById(processInstance.getId());         

              System.out.println(processInstance.isEnded());

       }

}

 

3.4 刪除流程

package com.langsi.jbpm;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.ProcessInstance;

import org.jbpm.api.RepositoryService;

 

public class Test7 {

       /**

        * 流程例項的刪除

        * @param args

        */

       public static void main(String[] args) {

              //1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              //2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              //3、獲得資料倉儲服務物件

              RepositoryService repositoryService =  processEngine.getRepositoryService();

              //4、建立部署例項物件

              NewDeployment newDeployment  = repositoryService.createDeployment();

              //5、從類路徑下面增加流程部署檔案到部署例項物件

              newDeployment = newDeployment.addResourceFromClasspath("secondprocess.jpdl.xml");

              //6完成流程的部署

              newDeployment.deploy();

              //7、獲得流程執行服務物件

              ExecutionService executionService = processEngine.getExecutionService();

              //8、流程執行服務物件根據流程定義執行,得到流程例項

              ProcessInstance processInstance = executionService.startProcessInstanceByKey("secondprocess");        

              //刪除流程例項物件

              executionService.deleteProcessInstance(processInstance.getId());             

       }

}

 

 

4 第三個流程 thirdprocess.jpdl.xml

4.1 流程定義

<?xml version="1.0" encoding="UTF-8"?>

<process name="thirdprocess" xmlns="http://jbpm.org/4.3/jpdl">

   <start name="start1" g="332,116,48,48">

      <transition name="to state1" to="state1" g="-59,-17"/>

   </start>

   <end name="end1" g="333,364,48,48"/>

   <state name="state1" g="456,265,92,52">

      <on event="start">

         <event-listener class="com.langsi.event.MyEventListener"/>

      </on>

      <on event="end">

         <event-listener class="com.langsi.event.MyEventListener"/>

      </on>

      <transition name="to end1" to="end1" g="-47,-17"/>

   </state>

</process>

4.2 流程圖

4.3 相關事件

package com.langsi.event;

import org.jbpm.api.listener.EventListener;

import org.jbpm.api.listener.EventListenerExecution;

 

@SuppressWarnings("serial")

public class MyEventListener implements EventListener {

       @Override

       public void notify(EventListenerExecution execution) throws Exception {

              System.out.println("-------------------------------------"+execution.getId());

       }

}

 

4.4 測試流程

package com.langsi.jbpm;

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.ProcessInstance;

import org.jbpm.api.RepositoryService;

 

public class Test8 {

       /**

        * 增加事件

        * @param args

        */

       public static void main(String[] args) {

                     //1、建立JBPM配置物件

                     Configuration configuration = new Configuration();

                     //2、建立流程引擎

                     ProcessEngine processEngine = configuration.buildProcessEngine();

                     //3、獲得資料倉儲服務物件

                     RepositoryService repositoryService =  processEngine.getRepositoryService();

                     //4、建立部署例項物件

                     NewDeployment newDeployment  = repositoryService.createDeployment();

                     //5、從類路徑下面增加流程部署檔案到部署例項物件

                     newDeployment = newDeployment.addResourceFromClasspath("thirdprocess.jpdl.xml");

                     //6完成流程的部署

                     newDeployment.deploy();

                     //7、獲得流程執行服務物件

                     ExecutionService executionService = processEngine.getExecutionService();

                     //8、流程執行服務物件根據流程定義執行,得到流程例項

                     ProcessInstance processInstance = executionService.startProcessInstanceByKey("thirdprocess");                  

                     executionService.signalExecutionById(processInstance.getId());

       }

}

 

5 流程四 fourthprocess.jpdl.xml

5.1 定義檔案

<?xml version="1.0" encoding="UTF-8"?>

 

<process name="fourthprocess" xmlns="http://jbpm.org/4.3/jpdl">

   <start name="start1" g="242,62,48,48">

      <transition name="to task1" to="task1" g="-53,-17"/>

   </start>

   <end name="end1" g="246,393,48,48"/>

   <task name="task1" g="237,220,92,52" assignee="zhangsan">

      <transition name="to end1" to="end1" g="-47,-17"/>

   </task>

 

</process>

 

5.2 流程圖

5.3 測試流程

package com.langsi.jbpm;

 

import java.util.List;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.NewDeployment;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.ProcessInstance;

import org.jbpm.api.RepositoryService;

import org.jbpm.api.TaskService;

import org.jbpm.api.task.Task;

 

//任務與事件

public class Test9 {

      

       public static void main(String[] args) {

             

              //1、建立JBPM配置物件

              Configuration configuration = new Configuration();

              //2、建立流程引擎

              ProcessEngine processEngine = configuration.buildProcessEngine();

              //3、獲得資料倉儲服務物件

              RepositoryService repositoryService =  processEngine.getRepositoryService();

              //4、建立部署例項物件

              NewDeployment newDeployment  = repositoryService.createDeployment();

              //5、從類路徑下面增加流程部署檔案到部署例項物件

              newDeployment = newDeployment.addResourceFromClasspath("fourthprocess.jpdl.xml");

              //6完成流程的部署

              newDeployment.deploy();

              //7、獲得流程執行服務物件

              ExecutionService executionService = processEngine.getExecutionService();

              //8、流程執行服務物件根據流程定義執行,得到流程例項

              ProcessInstance processInstance = executionService.startProcessInstanceByKey("fourthprocess");

             

              System.out.println(processInstance.findActiveActivityNames());

             

              // 得到任務處理服務物件

              TaskService taskService = processEngine.getTaskService();

              List<Task> tasks = taskService.findPersonalTasks("zhangsan");

              System.out.println(tasks.size());

             

              //完成任務例項,taskService.completeTasktaskId

              Task task = tasks.iterator().next();

              taskService.completeTask(task.getId());

       }

}

 

 

 

 

相關文章