jbpm4.4 会签实例
开发环境为MyEclipse8.5 jbpm4.4。
根据会签的一票否决制编写。
流程的XML文件 process.jpd.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <process name="会签实例" xmlns="http://jbpm.org/4.3/jpdl" description="会签中一个不同意,会签就结束">
- <start g="110,14,48,48" name="Start">
- <transition g="-95,-17" name="to JointlySign" to="会签"/>
- </start>
- <end g="110,264,48,48" name="End"/>
- <task g="88,96,92,52" name="会签">
- <assignment-handler class="test.join.AssignTask">
- </assignment-handler>
- <transition g="-71,-17" name="to 执行" to="执行"/>
- <transition g="-41,-17" name="to 结束" to="End"/>
- </task>
- <state g="16,180,92,52" name="执行">
- <transition g="-41,-17" name="to End" to="End"/>
- </state>
- </process>
AssignTask.java
- package test.join;
- import java.util.List;
- import org.jbpm.api.Configuration;
- import org.jbpm.api.ProcessEngine;
- import org.jbpm.api.TaskService;
- import org.jbpm.api.model.OpenExecution;
- import org.jbpm.api.task.Assignable;
- import org.jbpm.api.task.AssignmentHandler;
- import org.jbpm.api.task.Participation;
- import org.jbpm.api.task.Task;
- import org.jbpm.pvm.internal.task.OpenTask;
- public class AssignTask implements AssignmentHandler{
- private static final long serialVersionUID = 1L;
- List<String> participants;
- ProcessEngine processEngine=Configuration.getProcessEngine();
- TaskService taskService=processEngine.getTaskService();
- @SuppressWarnings("unchecked")
- public void assign(Assignable assignable, OpenExecution execution) throws Exception {
- //System.out.println("分配");
- String pid=execution.getProcessInstance().getId();
- //System.out.println("pid :"+pid);
- Task task=taskService.createTaskQuery().processInstanceId(pid).activityName(execution.getName()).uniqueResult();
- //System.out.println(task.getName());
- participants=(List<String>)taskService.getVariable(task.getId(), "participants");
- if(participants!=null){
- for(String participant:participants){
- //System.out.println(participant);
- Task subTask=((OpenTask)task).createSubTask();
- subTask.setAssignee(participant);
- taskService.addTaskParticipatingUser(task.getId(), participant, Participation.CLIENT);
- }
- }
- }
- }
Sign.java
- package test.join;
- import org.jbpm.api.TaskService;
- import org.jbpm.api.cmd.Command;
- import org.jbpm.api.cmd.Environment;
- import org.jbpm.api.task.Task;
- public class Sign implements Command<Boolean>{
- private static final long serialVersionUID = 1L;
- public static String VAR_SIGN="同意";
- private String PASS;
- private String NOPASS;
- private String parentTaskId;
- private Task parentTask;
- private Task joinTask;
- String pid;
- public Sign(String parentTaskId,Task joinTask,String PASS,String NOPASS){
- this.parentTaskId=parentTaskId;
- this.PASS=PASS;
- this.NOPASS=NOPASS;
- this.joinTask=joinTask;
- }
- public String getPid(){
- return pid;
- }
- public Boolean execute(Environment environment) throws Exception {
- TaskService taskService=environment.get(TaskService.class);
- parentTask=taskService.getTask(parentTaskId);
- pid=parentTask.getExecutionId();
- String sign=(String)taskService.getVariable(joinTask.getId(), VAR_SIGN);
- if(sign!=null&&sign.equals("不同意")){
- taskService.completeTask(joinTask.getId());
- taskService.completeTask(parentTask.getId(), NOPASS);
- return true;
- }
- taskService.completeTask(joinTask.getId());
- if(taskService.getSubTasks(parentTaskId).size()==0){
- taskService.completeTask(parentTaskId,PASS);
- return true;
- }
- return false;
- }
- }
test.java
- package test.join;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.jbpm.api.Configuration;
- import org.jbpm.api.ExecutionService;
- import org.jbpm.api.HistoryService;
- import org.jbpm.api.ProcessEngine;
- import org.jbpm.api.ProcessInstance;
- import org.jbpm.api.RepositoryService;
- import org.jbpm.api.TaskService;
- import org.jbpm.api.history.HistoryProcessInstance;
- import org.jbpm.api.task.Task;
- public class Test {
- public static void main(String []args){
- ProcessEngine processEngine=Configuration.getProcessEngine();
- ExecutionService executionService=processEngine.getExecutionService();
- TaskService taskService=processEngine.getTaskService();
- RepositoryService repositoryService=processEngine.getRepositoryService();
- repositoryService.createDeployment().addResourceFromClasspath("test/join/process.jpdl.xml").deploy();
- Map<String,Object> variables=new HashMap<String,Object>();
- List<String> pariticipants=new ArrayList<String>();
- pariticipants.add("yunix");
- pariticipants.add("snrqtdhuqf");
- variables.put("participants", pariticipants);
- ProcessInstance processInstance=executionService.startProcessInstanceByKey("会签实例",variables);
- Task task=taskService.createTaskQuery()
- .processInstanceId(processInstance.getId())
- .activityName(processInstance.findActiveActivityNames().iterator().next()).uniqueResult();
- //System.out.println(task.getId());
- Task joinTask=taskService.findPersonalTasks("yunix").get(0);
- Sign sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");
- processEngine.execute(sign);
- joinTask=taskService.findPersonalTasks("snrqtdhuqf").get(0);
- sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");
- processEngine.execute(sign);
- processInstance=executionService.findProcessInstanceById(processInstance.getId());
- System.out.println(processInstance.isActive("执行"));
- String executionId = processInstance.findActiveExecutionIn("执行").getId();
- processInstance=executionService.signalExecutionById(executionId);
- System.out.println("流程是否结束:"+processInstance.isEnded());
- processInstance=executionService.startProcessInstanceByKey("会签实例",variables);
- System.out.println("会签不通过流程开始,流程ID为:"+processInstance.getId()+" ");
- task=taskService.createTaskQuery()
- .processInstanceId(processInstance.getId())
- .activityName(processInstance.findActiveActivityNames().iterator().next())
- .uniqueResult();
- joinTask=taskService.findPersonalTasks("yunix").get(0);
- Map<String,Object> v=new HashMap<String,Object>();
- v.put(Sign.VAR_SIGN, "不同意");
- taskService.setVariables(joinTask.getId(), v);
- sign=new Sign(task.getId(),joinTask,"to 执行","to 结束");
- System.out.println(processEngine.execute(sign));
- HistoryService historyService=processEngine.getHistoryService();
- List<HistoryProcessInstance> hProcessInstances = historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstance.getId())
- .list();
- for(HistoryProcessInstance h:hProcessInstances){
- System.out.println("流程"+processInstance.getId()+"状态:"+h.getState());
- }
- }
- }
测试结果:
true
流程是否结束:true
会签不通过流程开始,流程ID为:会签实例.17
true
流程会签实例.17状态:ended
明天你还会爱我?