11package com .code .repository ;
2- import org .jbpm .api .Configuration ;
3- import org .jbpm .api .ProcessEngine ;
4- import org .jbpm .api .RepositoryService ;
2+ import org .jbpm .api .*;
53import org .jbpm .api .task .Task ;
64import org .junit .Test ;
75import org .junit .runner .RunWith ;
86import org .springframework .boot .test .context .SpringBootTest ;
97import org .springframework .test .context .junit4 .SpringRunner ;
108
11- import java .util .List ;
9+ import java .util .* ;
1210
1311@ RunWith (SpringRunner .class )
1412@ SpringBootTest
@@ -24,26 +22,35 @@ public void createSchema() { // hbm2ddl.auto=update
2422 new org .hibernate .cfg .Configuration ().configure ("jbpm.hibernate.cfg.xml" ).buildSessionFactory ();
2523 }
2624
27- // 一、部署流程定义
25+ // 一、部署流程定义 RepositoryService
2826 @ Test
29- public void deployProcessDefinition () {
30- ProcessEngine processEngine = Configuration .getProcessEngine ();
31- RepositoryService repositoryService = processEngine .getRepositoryService ();
32- repositoryService .createDeployment ().addResourceFromClasspath ("jbpm .jpdl.xml" ).deploy ();
27+ public void deploy () {
28+ ProcessEngine processEngine = Configuration .getProcessEngine ();// 创建流程引擎
29+ RepositoryService repositoryService = processEngine .getRepositoryService ();// 获取流程资源服务接口
30+ repositoryService .createDeployment ().addResourceFromClasspath ("test .jpdl.xml" ).deploy ();// 部署流程
3331 }
3432
33+ // 生成一个流程实例 ExecutionService
3534 @ Test
36- public void startProcessInstance () {
35+ public void createInstance () {
3736 ProcessEngine processEngine = Configuration .getProcessEngine ();
38- processEngine .getExecutionService ().startProcessInstanceByKey ("test" );
37+ String processKey = "test" ;
38+ ExecutionService executionService = processEngine .getExecutionService ();//执行服务
39+ Map <String , String > map = new HashMap <String , String >(); // 添加变量
40+ map .put ("staff" ,"hm员工" );
41+ ProcessInstance processInstance = executionService .startProcessInstanceByKey (processKey ,map );
42+ String id = processInstance .getId ();
43+ System .out .println ("流程实例id:" +id );
3944 }
4045
46+ // 查询任务列表 TaskService
4147 @ Test
4248 public void findMyTaskList () {
4349 ProcessEngine processEngine = Configuration .getProcessEngine ();
4450 // 查询任务
4551 String userId = "员工" ;
46- List <Task > list = processEngine .getTaskService ().findPersonalTasks (userId );
52+ TaskService taskService = processEngine .getTaskService ();
53+ List <Task > list = taskService .findPersonalTasks (userId );
4754
4855 // 显示任务
4956 System .out .println ("========= 【" +userId +"】的未办理的任务列表 =========" );
@@ -55,13 +62,32 @@ public void findMyTaskList() {
5562 }
5663 }
5764
65+ // 获取流程变量 TaskService
5866 @ Test
59- public void completeTask () {
67+ public void fetchTaskVariable () {
6068 ProcessEngine processEngine = Configuration .getProcessEngine ();
61- String taskId = "20002" ;
62- processEngine .getTaskService ().completeTask (taskId );
69+ String taskId = "40003" ;
70+ String instanceId = "test.40001" ;
71+ TaskService taskService = processEngine .getTaskService ();
72+ ExecutionService executionService = processEngine .getExecutionService ();
73+ // 取出当前的所有任务
74+ Set <String > activityNames = executionService .createProcessInstanceQuery ().processInstanceId (instanceId ).uniqueResult ().findActiveActivityNames ();
75+ String name = (String )activityNames .toArray ()[0 ];
76+ Task task = taskService .createTaskQuery ().processInstanceId (instanceId ).activityName (name ).uniqueResult ();
77+ // 根据任务取出变量
78+ Set <String > vars = taskService .getVariableNames (task .getId ());
79+ System .out .println (vars .toString ());
80+ String staff = (String )taskService .getVariable (taskId ,"staff" );
81+ System .out .println ("staff:" +staff );
6382 }
6483
65-
84+ // 完成当前任务 TaskService
85+ @ Test
86+ public void completeTask () {
87+ ProcessEngine processEngine = Configuration .getProcessEngine ();
88+ String taskId = "60001" ;
89+ TaskService taskService = processEngine .getTaskService ();
90+ taskService .completeTask (taskId );
91+ }
6692
6793}
0 commit comments