流程图:
1、通过assignee直接指定某个人办理
<? xml version="1.0" encoding="UTF-8" ?>
< process name ="AssignTask" xmlns ="http://jbpm.org/4.4/jpdl" >
< start g ="150,61,48,48" name ="start1" > < transition g ="-43,-20" name ="to 申请" to ="申请" />
</ start >
< task assignee ="张三" g ="131,150,92,52" name ="申请" >
< transition g ="-45,-20" name ="to end1" to ="end1" />
</ task >
<end g="155,239,48,48"name="end1"/>
</ process >
在”申请“任务节点直接使用 assignee="张三"指定张三办理申请任务。
查找出待张三处理的任务:
String userId = "张三" ;
List<Task> tasks = processEngine.getTaskService().findPersonalTasks(userId);
2、使用流程变量分配任务
<? xml version="1.0" encoding="UTF-8" ?>
< process name ="AssignTask" xmlns ="http://jbpm.org/4.4/jpdl" >
< start g ="150,61,48,48" name ="start1" >
< transition g ="-43,-20" name ="to 申请" to ="申请" />
</ start >
< task assignee ="#{userId}" g ="131,150,92,52" name ="申请" >
< transition g ="-45,-20" name ="to end1" to ="end1" />
</ task >
<end g="155,239,48,48"name="end1"/>
</ process >
使用assignee="#{userId}"分配,userId是一个流程变量,在到达该任务之前需要把userId的值存到流程变量中。
Map<String,String> variables = newHashMap<String,String> ();variables.put("userId", "张三" ); // 把下一步处理人存到流程变量中processEngine.getExecutionService().startProcessInstanceByKey("AssignTask",variables);
3、使用AssignmentHandler接口指定任务处理人
<? xml version="1.0" encoding="UTF-8" ?>
< process name ="AssignTask" xmlns ="http://jbpm.org/4.4/jpdl" >
< start g ="142,59,48,48" name ="start1" >
< transition g ="-43,-20" name ="to 申请" to ="申请" />
</ start >
< task g ="131,150,92,52" name ="申请" >
< assignment-handler class ="cn.luxh.jbpm4.handler.AssignmentHandlerImpl" >
</ assignment-handler >
< transition g ="-45,-20" name ="to end1" to ="end1" />
</ task >
<end g="155,239,48,48"name="end1"/>
</ process >
使用<assignment-handler class="cn.luxh.jbpm4.handler.AssignmentHandlerImpl"></assignment-handler>指定一个实现了AssignmentHandler接口的类来分配任务处理人。
package cn.luxh.jbpm4.handler;
import org.jbpm.api.model.OpenExecution;
import org.jbpm.api.task.Assignable;
import org.jbpm.api.task.AssignmentHandler;
public classAssignmentHandlerImpl implements AssignmentHandler {
@Override
public void assign(Assignable assignable, OpenExecution execution) throws Exception {
// 实际可能是从数据库查出相应的用户}
String userId = "张三" ;
assignable.setAssignee(userId);
}
4、在程序中直接分配
二、组任务的分配
流程走到某个任务节点,这个任务可以分配给多个人,谁先拾取了这个任务就变成谁的待办任务。比如说会计核算任务,一些大的公司,会有多个会计,流程到达会计核算任务节点时,如果有张三、李四、王五三个会计,这个任务就会变成张三、李四、王五的组任务,这个时候张三、李四、王五都能看到这个任务,但是这个时候还没有具体的处理人,如果其中一个会计张三拾取了组任务,这个任务就变成张三的个人任务由张三处理了,李四和王五就看不到这个组任务了。
流程图:
1、使用 candidate-users="张三,李四,王五" 分配组任务,各个处理人之间用英文输入法下的逗号隔开
<? xml version="1.0" encoding="UTF-8" ?>
< process name ="AssignTask" xmlns ="http://jbpm.org/4.4/jpdl" >
< start name ="start1" g ="164,33,48,48" >
< transition name ="to 会计核算" to ="会计核算" g ="-67,-20" />
</ start >
< task name ="会计核算" g ="152,125,92,52" candidate-users ="张三,李四,王五" >
< transition name ="to end1" to ="end1" g ="-45,-20" />
</ task >
<end name="end1"g="174,218,48,48"/>
</ process >
查找组任务:
String userId = "张三" ;
List<Task> tasks = processEngine.getTaskService().findGroupTasks(userId);
for (Task task : tasks)
{
// 张三拾取组任务,拾取之后就变成张三的个人任务了,只能由张三处理,其他人就看不到了
processEngine.getTaskService().takeTask(task.getId(), userId);
}
2、使用流程变量candidate-users="#{userIds}"分配组任务
<? xml version="1.0" encoding="UTF-8" ?>
< process name ="AssignTask" xmlns ="http://jbpm.org/4.4/jpdl" >
< start name ="start1" g ="164,33,48,48" >
< transition name ="to 会计核算" to ="会计核算" g ="-67,-20" />
</ start >
< task name ="会计核算" g ="152,125,92,52" candidate-users ="#{userIds}" >
< transition name ="to end1" to ="end1" g ="-45,-20" />
</ task >
<end name="end1"g="174,218,48,48"/>
</ process >
candidate-users="#{userIds}"中的userIds也是用英文输入法下的逗号隔开的字符串。
String userIds = "张三,李四,王五" ;
Map<String,String> variables = newHashMap<String,String> ();
variables.put("userIds" , userIds);
processEngine.getExecutionService().startProcessInstanceByKey("AssignTask",variables);
3、使用AssignmentHandler接口指定任务处理人
<? xml version="1.0" encoding="UTF-8" ?>
< process name ="AssignTask" xmlns ="http://jbpm.org/4.4/jpdl" >
< start name ="start1" g ="164,33,48,48" >
< transition name ="to 会计核算" to ="会计核算" g ="-67,-20" />
</ start >
< task name ="会计核算" g ="152,125,92,52" >
< assignment-handler class ="cn.luxh.jbpm4.handler.AssignmentHandlerImpl" >
</ assignment-handler >
< transition name ="to end1" to ="end1" g ="-45,-20" />
</ task >
<end name="end1"g="174,218,48,48"/>
</ process >
使用<assignment-handler class="cn.luxh.jbpm4.handler.AssignmentHandlerImpl"></assignment-handler>
指定一个实现了AssignmentHandler接口的类来分配任务处理人。
package cn.luxh.jbpm4.handler;
import org.jbpm.api.model.OpenExecution;
import org.jbpm.api.task.Assignable;
import org.jbpm.api.task.AssignmentHandler;
public class AssignmentHandlerImpl implements AssignmentHandler
{
@Override public void assign(Assignable assignable, OpenExecution execution) throws Exception {
assignable.addCandidateUser("张三" );
assignable.addCandidateUser("李四" );
assignable.addCandidateUser("王五" );
}
}