动态产生任务实例(taskInstance)。
1、将taskNode的create-tasks="false" , 而且在taskNode中设置一个<task> 。
2、在taskNode的node-enter事件上添加一个Handler ,在ActionHandler中动态添加任务实例。代码如下:
1public void execute(ExecutionContext executionContext) throws Exception {
2 Token token = executionContext.getToken();
3 TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
4 Token token1= token;
5 Token token2= new Token(token.getParent(),"forlimeng");
6 TaskNode taskNode = (TaskNode) executionContext.getNode();
7 //taskNode.addTask(new Task("task1"));
8 Task changeNappy = taskNode.getTask("task_s");
9 token1.setNode(taskNode);
10 token2.setNode(taskNode);
11
12 // now, 2 task instances are created for the same task.
13 TaskInstance ti1=tmi.createTaskInstance(changeNappy, token1);
14 ti1.setActorId("shennan");
15 TaskInstance ti2=tmi.createTaskInstance(changeNappy, token2);
16 ti2.setActorId("limeng");
17 executionContext.getJbpmContext().getSession().saveOrUpdate(ti1);
18 executionContext.getJbpmContext().getSession().saveOrUpdate(ti2);
19 System.out.println("create task a and z ok!");
20
21 }
22
23
2 Token token = executionContext.getToken();
3 TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
4 Token token1= token;
5 Token token2= new Token(token.getParent(),"forlimeng");
6 TaskNode taskNode = (TaskNode) executionContext.getNode();
7 //taskNode.addTask(new Task("task1"));
8 Task changeNappy = taskNode.getTask("task_s");
9 token1.setNode(taskNode);
10 token2.setNode(taskNode);
11
12 // now, 2 task instances are created for the same task.
13 TaskInstance ti1=tmi.createTaskInstance(changeNappy, token1);
14 ti1.setActorId("shennan");
15 TaskInstance ti2=tmi.createTaskInstance(changeNappy, token2);
16 ti2.setActorId("limeng");
17 executionContext.getJbpmContext().getSession().saveOrUpdate(ti1);
18 executionContext.getJbpmContext().getSession().saveOrUpdate(ti2);
19 System.out.println("create task a and z ok!");
20
21 }
22
23
一定要理解透彻Token和父Token,还有Fork和Join的机制。
流程定义的代码如下:
1<?xml version="1.0" encoding="UTF-8"?>
2
3<process-definition
4 xmlns="http://jbpm.org/3/jpdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://jbpm.org/3/jpdl http://docs.jboss.org/jbpm/xsd/jpdl-3.0.xsd"
6 name="paidan">
7
8
9
10 <start-state name="start-state1">
11 <transition to="派单"></transition>
12 </start-state>
13
14
15 <task-node name="派单">
16 <task name="task1"></task>
17 <transition to="fork1"></transition>
18 </task-node>
19
20 <fork name="fork1">
21 <transition to="处理派单"></transition>
22 </fork>
23
24 <task-node name="处理派单" create-tasks="false">
25 <task name="task_s"></task>
26 <event type="node-enter">
27 <action name="createTasks" class="sn.paidan.CreateTasksAction"></action>
28 </event>
29 <transition to="join1"></transition>
30 </task-node>
31
32 <join name="join1">
33 <transition to="end-state1"></transition>
34 </join>
35
36
37 <end-state name="end-state1"></end-state>
38
39
40</process-definition>
41
2
3<process-definition
4 xmlns="http://jbpm.org/3/jpdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://jbpm.org/3/jpdl http://docs.jboss.org/jbpm/xsd/jpdl-3.0.xsd"
6 name="paidan">
7
8
9
10 <start-state name="start-state1">
11 <transition to="派单"></transition>
12 </start-state>
13
14
15 <task-node name="派单">
16 <task name="task1"></task>
17 <transition to="fork1"></transition>
18 </task-node>
19
20 <fork name="fork1">
21 <transition to="处理派单"></transition>
22 </fork>
23
24 <task-node name="处理派单" create-tasks="false">
25 <task name="task_s"></task>
26 <event type="node-enter">
27 <action name="createTasks" class="sn.paidan.CreateTasksAction"></action>
28 </event>
29 <transition to="join1"></transition>
30 </task-node>
31
32 <join name="join1">
33 <transition to="end-state1"></transition>
34 </join>
35
36
37 <end-state name="end-state1"></end-state>
38
39
40</process-definition>
41