一、建立工程并配置环境。
1、下载eclipse3.6以上版本。下载Jbpm4.3。均可在其官方网下载。
2、eclipse中配置Jbpm4.3,通过help--install software--add--archive指定jbpm-4.3/jbpm-4.3/install/src/gpd/jbpm-gpd-site.zip文件。之后重新启动即可。
3、建立Jbpm4Helloworld 普通java工程。
4、拷贝jbpm4.3目录下的jbpm.jar以及lib目录下所有文件配置工程依赖包。
5、进入jbpm-4.3/examples/src目录中,拷贝其下所有配置文件到项目src目录下。
6、再src中右建新建Jbpm4 Process Definition,输入文件名helloworld。则在src下产生helloworld.jpdl.xml文件。
7、打开此文件,定义流程。放入start、end、state、transition。保存,系统自动产生相应的png图。
8、新建junit测试。在src右建选择 junit test case ,选择 New JUnit 3 test. package:com.nbchina name:HelloworldTest.
二、管理流程
打开HelloworldTest文件。
内容如下
package com.nbchina; import java.util.List; import org.jbpm.api.*; import junit.framework.TestCase; public class HelloworldTest extends TestCase { ProcessEngine processEngine; public HelloworldTest(){ processEngine=Configuration.getProcessEngine(); } public void testDeploy(){ RepositoryService repositoryService = processEngine.getRepositoryService(); String deploymentId = repositoryService.createDeployment().addResourceFromClasspath("helloworld.jpdl.xml").deploy();//部署流程 List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();//查询所有已经部署的流程 for(ProcessDefinition pd:list){ System.out.println("ID:"+pd.getId()+";Name:"+pd.getName()); } repositoryService.deleteDeploymentCascade(deploymentId);//级联删除此流程 System.out.println("还有["+repositoryService.createProcessDefinitionQuery().count()+"]个流程."); //打印还有几个流程 } }
三、流程执行
新建 ProcessInstanceTest文件
内容如下
package com.nbchina; import java.util.List; import junit.framework.TestCase; import org.jbpm.api.*; public class ProcessInstanceTest extends TestCase { ProcessEngine processEngine; public ProcessInstanceTest(){ processEngine=Configuration.getProcessEngine(); } public void setUp(){ RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addResourceFromClasspath("helloworld.jpdl.xml").deploy();//部署流程 } public void testProcessInstance(){ //启动流程并打印查看s ExecutionService executionService=processEngine.getExecutionService(); ProcessInstance pi = executionService.startProcessInstanceByKey("helloworld"); System.out.println(pi); System.out.println(pi.isEnded()); pi = executionService.signalExecutionById(pi.getId());//已启动,在等待中的流程进行流转 System.out.println(pi.isEnded()); } public void testProcessInstanceDelete(){//删除现在正在执行的流程 ExecutionService executionService=processEngine.getExecutionService(); ProcessInstance pi = executionService.startProcessInstanceByKey("helloworld"); executionService.deleteProcessInstanceCascade(pi.getId()); System.out.println(pi); System.out.println(pi.isEnded()); } public void testProcessInstanceEnd(){//终止现在正在执行的流程 ExecutionService executionService=processEngine.getExecutionService(); ProcessInstance pi = executionService.startProcessInstanceByKey("helloworld"); executionService.endProcessInstance(pi.getId(), "原因是:测试终止流程"); System.out.println(pi); System.out.println(pi.isEnded()); } public void testProcessInstanceList(){//启动两个流程,查询现在正在执行的流程 ExecutionService executionService=processEngine.getExecutionService(); ProcessInstance pi1 = executionService.startProcessInstanceByKey("helloworld"); ProcessInstance pi2 = executionService.startProcessInstanceByKey("helloworld"); List<ProcessInstance> list = executionService.createProcessInstanceQuery().list(); for(ProcessInstance pi:list){ System.out.println("ID:"+pi.getId()); } } }