由於咱們的業務須要,最近一直在搞動態動過java程序實現爲用戶任務綁定監聽程序。碰了不少壁,查看了API文檔,最後終於在找到解決辦法,因此貼出來,但願可以留個底,也能幫助有須要的人。java
----------動態生成流程文件的工具類--------web
package com.travesky.bluesky.activiti.utils;spring
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;apache
import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.model.ActivitiListener;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.ExtensionAttribute;
import org.activiti.bpmn.model.ExtensionElement;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.ImplementationType;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.ServiceTask;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
/**
* 流程文件生成工具類,已在spring文件注入
* @author AnCan
* @date 2017/4/10
*/
public class AutodeployProcessBpmnAndImageUtils {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
private final String START_EVENT = "start";
private final String END_EVENT = "end";
/**
*
* @param PROCESSID 流程id
* @param PROCESSNAME 流程名字
* @throws Exception
*/
public void dynamicDeployBpmnAndImage(final String PROCESSID,
final String PROCESSNAME) throws Exception {
System.out.println(".........start...");
// String basePath = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/")+"bpmn\\";
// System.out.println(".........start...====="+basePath);
// 1. Build up the model from scratch
BpmnModel model = new BpmnModel();
org.activiti.bpmn.model.Process process=new org.activiti.bpmn.model.Process();
model.addProcess(process);
process.setId(PROCESSID);
process.setName(PROCESSNAME);
//設置開始節點
process.addFlowElement(createStartEvent());
//設置任務節點1
process.addFlowElement(createUserTask("task1", "組長審覈", "candidateGroup1"));
//設置排他節點1
process.addFlowElement(createExclusiveGateway("createExclusiveGateway1"));
//設置任務節點2
process.addFlowElement(createUserTask("task2", "項目經理審覈", "candidateGroup2"));
//設置排他節點2
process.addFlowElement(createExclusiveGateway("createExclusiveGateway2"));
//設置任務節點3 普通任務
// process.addFlowElement(createUserTask("task3", "產品部門經理審覈", "candidateGroup3"));
//設置任務節點3 監放任務
List<String> lisenerList = new ArrayList<String>();
lisenerList.add("com.travesky.bluesky.activiti.utils.MangerTaskHandlerCandidateUsers");
process.addFlowElement(createUserTask("task3", "產品部門經理審覈",lisenerList));
//設置排他節點3
process.addFlowElement(createExclusiveGateway("createExclusiveGateway3"));
process.addFlowElement(createUserTask("task4", "總經理審覈", "candidateGroup4"));
//設置結束節點
process.addFlowElement(createEndEvent());
process.addFlowElement(createSequenceFlow(START_EVENT, "task1", "", ""));
process.addFlowElement(createSequenceFlow("task1", "task2", "", ""));
//
process.addFlowElement(createSequenceFlow("task2", "createExclusiveGateway1"));
process.addFlowElement(createSequenceFlow("createExclusiveGateway1", "task1", "不經過", "${pass=='2'}"));
process.addFlowElement(createSequenceFlow("createExclusiveGateway1", "task3", "經過", "${pass=='1'}"));
process.addFlowElement(createSequenceFlow("task3", "createExclusiveGateway2"));
process.addFlowElement(createSequenceFlow("createExclusiveGateway2", "task2", "不經過", "${pass=='2'}"));
process.addFlowElement(createSequenceFlow("createExclusiveGateway2", "task4", "經過", "${pass=='1'}"));
process.addFlowElement(createSequenceFlow("task4", "createExclusiveGateway3"));
process.addFlowElement(createSequenceFlow("createExclusiveGateway3", "task3", "不經過", "${pass=='2'}"));
process.addFlowElement(createSequenceFlow("createExclusiveGateway3", END_EVENT, "經過", "${pass=='1'}"));
// 2. Generate graphical information
new BpmnAutoLayout(model).execute();
// 3. Deploy the process to the engine
Deployment deployment = repositoryService.createDeployment().addBpmnModel(PROCESSID+".bpmn", model).name(PROCESSID+"_deployment").deploy();
// 4. Start a process instance
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(PROCESSID);
// 5. Check if task is available
List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
// Assert.assertEquals(1, tasks.size());
// 6. Save process diagram to a file
InputStream processDiagram = repositoryService.getProcessDiagram(processInstance.getProcessDefinitionId());
FileUtils.copyInputStreamToFile(processDiagram, new File("target/"+PROCESSID+".png"));
// 7. Save resulting BPMN xml to a file
InputStream processBpmn = repositoryService.getResourceAsStream(deployment.getId(), PROCESSID+".bpmn");
FileUtils.copyInputStreamToFile(processBpmn,new File("target/"+PROCESSID+".bpmn"));
System.out.println(".........end...");
}
/**
* 建立節點任務 我的任務
* @param id 任務id標識
* @param name 任務名稱
* @param assignee 指定我的任務
* @return
*/
public UserTask createUserTask(String id, String name, String assignee) {
UserTask userTask = new UserTask();
userTask.setName(name);
userTask.setId(id);
userTask.setAssignee(assignee);
return userTask;
}
/**
* 建立節點任務 多人任務
* @param id 任務id標識
* @param name 任務名稱
* @param candidateUsers 任務人的集合
* @return
*/
public UserTask createUserTask(String id, String name, String[] candidateUsers) {
UserTask userTask = new UserTask();
userTask.setName(name);
userTask.setId(id);
if(null!=candidateUsers&&candidateUsers.length>0){
userTask.setCandidateUsers(Arrays.asList(candidateUsers));
}
return userTask;
}
/**
* 建立節點任務 使用監聽設置處理人
* @param id 任務id標識
* @param name 任務名稱
* @param taskListenerList 監聽的集合,TaskListener實現類的的具體路徑例:com.sky.bluesky.activiti.utils.MangerTaskHandlerCandidateUsers
* @return
*/
public UserTask createUserTask(String id, String name, List<String> taskListenerList) {
UserTask userTask = new UserTask();
userTask.setName(name);
userTask.setId(id);
List<ActivitiListener> list = new ArrayList<ActivitiListener>();
for (String taskListener : taskListenerList) {
ActivitiListener listener = new ActivitiListener();
listener.setEvent("create");
//Spring配置以變量形式調用沒法寫入,只能經過繼承TaskListener方法,
listener.setImplementationType("class");
listener.setImplementation(taskListener);
list.add(listener);ide
}
userTask.setTaskListeners(list);
return userTask;
}工具
/**
* 設置連線
* @param from 從哪裏出發
* @param to 鏈接到哪裏
* @return
*/
public SequenceFlow createSequenceFlow(String from, String to) {
SequenceFlow flow = new SequenceFlow();
flow.setSourceRef(from);
flow.setTargetRef(to);
return flow;
}ui
/**
* 設置起始節點
* @return
*/
public StartEvent createStartEvent() {
StartEvent startEvent = new StartEvent();
startEvent.setId(START_EVENT);
return startEvent;
}orm
/**
* 排他網關節點
* @param id
* @return
*/
public static ExclusiveGateway createExclusiveGateway(String id) {
ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
exclusiveGateway.setId(id);
return exclusiveGateway;
}
/**
* 設置結束節點
* @return
*/
public EndEvent createEndEvent() {
EndEvent endEvent = new EndEvent();
endEvent.setId(END_EVENT);
return endEvent;
}
/**
* 設置連線
* @param from 從哪裏出發
* @param to 鏈接到哪裏
* @param name 連線名稱
* @param conditionExpression 判斷條件${arg>2}
* @return
*/
public static SequenceFlow createSequenceFlow(String from, String to,String name,String conditionExpression) {
SequenceFlow flow = new SequenceFlow();
flow.setSourceRef(from);
flow.setTargetRef(to);
flow.setName(name);
if(null!=conditionExpression&&!"".equals(conditionExpression)){
flow.setConditionExpression(conditionExpression);
}
return flow;
}
public FlowElement createServiceTask(String name){
ServiceTask stask = new ServiceTask();
stask.setId("sid");
stask.setName(name);
stask.setImplementation("activitiTest.PrintVariables");
String implementationType = ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION;
stask.setImplementationType(implementationType );
return stask;
}
}xml
------------監聽類--------繼承
package com.travesky.bluesky.activiti.utils;
import java.util.Arrays;
import org.activiti.bpmn.model.ActivitiListener;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
public class MangerTaskHandlerCandidateUsers extends ActivitiListener implements TaskListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void notify(DelegateTask delegateTask) {
System.out.println("進入MangerTaskHandlerCandidateUsers=========");
/**重新查詢當前用戶,再獲取當前用戶對應的領導*/
//當前用戶
String[] empLoyees = {"馮小剛經紀人","范冰冰經紀人","馮小剛"};
delegateTask.addCandidateUsers(Arrays.asList(empLoyees));
System.out.println("節點任務人========馮小剛經紀人,范冰冰經紀人,馮小剛");
}
}
---這裏沒有再作封裝和細化,由於還在完善。剛纔想把 taskListener強制轉換爲ActivitiListener ,可是那是不對的。必須是按照activiti的規範來。