在jbpm4中,必须给任务分配参与者(assignee),我总结了下,有以下的几种方法
一,直接在jpdl.xml文件中指明参与者,如:
task name="review" g="280,146,92,52" assignee="zcl">
<transition name="to wait" to="wait" g="-25,-25"/>
</task>
这样在程序中就可以直接使用zcl作为用户的id了。当然这并不常用。
二,通过使用流程实例变量获得
<task name="review" g="280,146,92,52" assignee="#{owner}">
<transition name="to wait" to="wait" g="-25,-25"/>
</task>
在程序中可以这样分配参与者:
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("owner", "zcl");
executionService.startProcessInstanceByKey("assignee", variables);
三,通过流程实例变量的导航
<task name="review" g="280,146,92,52" assignee="#{order.owner}">
<transition name="to wait" to="wait" g="-25,-25"/>
</task>
其中#{order.owner}相当于调用了order所代表类的getOwner()方法,因此首先得定义一个类,其中包括getOwner()方法,此类必须实现Serializable 接口。
public class Order implements Serializable {
private String owner;
public Order() {}
public Order(String owner) {
this.owner = owner;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
在程序中这样指定:
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("order", new Order("zcl"));
executionService.startProcessInstanceByKey("assignee1", variables);
四,通过实现AssignmentHandler接口来分配参与者
<task name="review" g="280,146,92,52">
<assignment-handler class="cn.zcl.jbpm4.task.assignee.AssigTask">
<field name="owner">
<string value="zcl"/>
</field>
</assignment-handler>
<transition name="to wait" to="wait" g="-25,-25"/>
</task>
再定义AssigTask类
public class AssigTask implements AssignmentHandler {
private String owner;
@Override
public void assign(Assignable assignable, OpenExecution execution)
throws Exception {
assignable.setAssignee(owner);//分配任务参与者
}
}
这样就给任务分配了参与者了,将zcl分配给了当前的任务
测试程序
executionService.startProcessInstanceByKey("assignee2");
List<Task> tasks = taskService.findPersonalTasks("zcl");
assertEquals("任务数不是1", 1, tasks.size());
Task task = tasks.get(0);
taskService.completeTask(task.getId());
tasks = taskService.findPersonalTasks("zcl");
assertEquals("没有任务", 0, tasks.size());
五,通过candidates给任务分配参与者
<task name="review" g="280,151,92,52" candidate-groups="groups">
<transition name="to wait" to="wait" g="-56,-22"/>
</task>
在程序中这样调用
String groupId = identityService.createGroup("groups");
identityService.createUser("zcl", "zcl", "z");
identityService.createUser("ch", "ch", "c");
identityService.createMembership("zcl", groupId);
identityService.createMembership("ch", groupId);
这样就把任务同时分派给了zcl和ch
下面是测试程序:
List<Task> tasks1 = taskService.findPersonalTasks("zcl");
assertEquals("是否相等", 0, tasks1.size());
List<Task> tasks2 = taskService.findPersonalTasks("ch");
assertEquals("是否相等", 0, tasks2.size());
tasks1 = taskService.findGroupTasks("zcl");
assertEquals("是否相等", 1, tasks1.size());
tasks2 = taskService.findGroupTasks("ch");
assertEquals("是否相等", 1, tasks2.size());
Task task = tasks1.get(0);
taskService.takeTask(task.getId(), "zcl");//组任务被zcl接受了
tasks1 = taskService.findGroupTasks("zcl");
assertEquals("是否相等", 0, tasks1.size());
tasks2 = taskService.findGroupTasks("ch");
assertEquals("是否相等", 0, tasks2.size());
tasks1 = taskService.findPersonalTasks("zcl");
assertEquals("是否相等", 1, tasks1.size());
tasks2 = taskService.findPersonalTasks("ch");
assertEquals("是否相等", 0, tasks2.size());
taskService.completeTask(task.getId());
tasks1 = taskService.findPersonalTasks("zcl");
assertEquals("是否相等", 0, tasks1.size());
tasks2 = taskService.findPersonalTasks("ch");
assertEquals("是否相等", 0, tasks2.size());
六,通过swimlanes来分配
<swimlane candidate-groups="groups" name="sales representative"/>
<start g="135,185,48,48" name="start1">
<transition g="-58,-22" name="to review" to="review"/>
</start>
<task g="247,182,92,52" name="review" swimlane="sales representative">
<transition name="to check" to="check" g="-25,-25"/>
</task>
在程序中这样使用:
identityService.createGroup("groups");
identityService.createUser("zcl", "zcl", "z");
identityService.createUser("cn", "cn", "c");
identityService.createMembership("zcl", "groups", "develop");
identityService.createMembership("cn", "groups", "develop");
executionSerivce.startProcessInstanceByKey("swimlanes");
List<Task> tasks1 = taskService.findPersonalTasks("zcl");
assertEquals("是否相等", 0, tasks1.size());
List<Task> tasks2 = taskService.findPersonalTasks("cn");
assertEquals("是否相等", 0, tasks2.size());
tasks1 = taskService.findGroupTasks("zcl");
assertEquals("是否相等", 1, tasks1.size());
tasks2 = taskService.findGroupTasks("cn");
assertEquals("是否相等", 1, tasks2.size());
Task task = tasks1.get(0);
taskService.takeTask(task.getId(), "zcl");
tasks1 = taskService.findGroupTasks("zcl");
assertEquals("是否相等", 0, tasks1.size());
tasks2 = taskService.findGroupTasks("cn");
assertEquals("是否相等", 0, tasks2.size());
tasks1 = taskService.findPersonalTasks("zcl");
assertEquals("是否相等", 1, tasks1.size());
tasks2 = taskService.findPersonalTasks("cn");
assertEquals("是否相等", 0, tasks2.size());
taskService.completeTask(task.getId());
tasks1 = taskService.findPersonalTasks("zcl");
assertEquals("是否相等", 1, tasks1.size());
tasks2 = taskService.findPersonalTasks("cn");
assertEquals("是否相等", 0, tasks2.size());