【Activiti学习之三】Activiti API(二)
环境
JDK 1.8
MySQL 5.6
Tomcat 7
Eclipse-Luna
activiti 6.0
一、流程定义
1、中止与激活流程定义
package com.wjy.pd; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.DeploymentBuilder; import org.activiti.engine.repository.ProcessDefinition; public class ActiveTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); DeploymentBuilder builder = rs.createDeployment(); builder.addClasspathResource("test3.bpmn"); Deployment dep = builder.deploy(); ProcessDefinition def = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult(); System.out.println("id: " + def.getId()); //根据 ACt_RE_PROCDEF中的key中止流程定义 rs.suspendProcessDefinitionByKey(def.getKey()); //根据 ACt_RE_PROCDEF中的key激活流程定义 rs.activateProcessDefinitionByKey(def.getKey()); //根据流程id中止流程定义 rs.suspendProcessDefinitionById(def.getId()); //根据流程id激活流程定义 rs.activateProcessDefinitionById(def.getId()); //如果流程被中止将会抛出异常 启动实例会报错 RuntimeService runService = engine.getRuntimeService(); runService.startProcessInstanceByKey(def.getKey()); } }
2、流程定义权限
package com.wjy.pd; import java.util.List; import java.util.UUID; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.identity.Group; import org.activiti.engine.identity.User; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.DeploymentBuilder; import org.activiti.engine.repository.ProcessDefinition; public class AuthTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); IdentityService is = engine.getIdentityService(); User user = is.newUser(UUID.randomUUID().toString()); user.setFirstName("Angus"); is.saveUser(user); DeploymentBuilder builder = rs.createDeployment(); builder.addClasspathResource("test3.bpmn"); Deployment dep = builder.deploy(); ProcessDefinition def = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult(); //设定流程定义和用户的关系 记录到表act_ru_identitylink rs.addCandidateStarterUser(def.getId(), user.getId()); //设定流程定义和用户组的关系 记录到表act_ru_identitylink rs.addCandidateStarterGroup(def.getId(), "group1"); //查询某个用户可以启动的流程清单 List<ProcessDefinition> defs = rs.createProcessDefinitionQuery().startableByUser(user.getId()).list(); for(ProcessDefinition de : defs) { System.out.println(de.getId()); } //查询某个用户可以启动的流程清单 //List<Group> list= is.createGroupQuery().potentialStarter(def.getId()).list(); } }
二、任务操作
1、设置任务候选人(组)
package com.wjy.task; import java.util.List; import java.util.UUID; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.identity.User; import org.activiti.engine.task.Task; public class CandidateTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); TaskService ts = engine.getTaskService(); IdentityService is = engine.getIdentityService(); // 创建任务 String taskId = UUID.randomUUID().toString(); Task task = ts.newTask(taskId); task.setName("测试任务"); ts.saveTask(task); // 创建用户 String userId = UUID.randomUUID().toString(); User user = is.newUser(userId); user.setFirstName("angus"); is.saveUser(user); //设置任务的候选用户 保存到act_ru_identitylink ts.addCandidateUser(taskId, userId); //设置任务的候选用户组 保存到act_ru_identitylink //ts.addCandidateGroup(taskId, "groupid"); //查询用户组任务清单 //ts.createTaskQuery().taskCandidateGroup("groupid").list(); //查询候选用户任务清单 List<Task> tasks = ts.createTaskQuery().taskCandidateUser(userId).list(); System.out.println(userId + " 这个用户有权限处理的任务有:"); for(Task t : tasks) { System.out.println(t.getName()); } } }
2、设置任务持有人/受理人
package com.wjy.task; import java.util.List; import java.util.UUID; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.identity.User; import org.activiti.engine.task.Task; public class OwnerTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); TaskService ts = engine.getTaskService(); IdentityService is = engine.getIdentityService(); // 创建任务 String taskId = UUID.randomUUID().toString(); Task task = ts.newTask(taskId); task.setName("测试任务"); ts.saveTask(task); // 创建用户 String userId = UUID.randomUUID().toString(); User user = is.newUser(userId); user.setFirstName("angus"); is.saveUser(user); // 设置任务的持有人 ts.setOwner(taskId, userId); // 根据用户来查询他所持有的任务 List<Task> tasks = ts.createTaskQuery().taskOwner(userId).list(); for(Task t : tasks) { System.out.println(t.getName()); } //设置任务受理人 act_ru_task ts.setAssignee(taskId, userId); //根据受理人查询 ts.createTaskQuery().taskAssignee(userId).list(); } }
3、任务的声明与完成
package com.wjy.task; import java.util.UUID; import org.activiti.engine.IdentityService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.identity.User; import org.activiti.engine.task.Task; public class ClaimTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); TaskService ts = engine.getTaskService(); IdentityService is = engine.getIdentityService(); // 创建任务 String taskId = UUID.randomUUID().toString(); Task task = ts.newTask(taskId); task.setName("测试任务"); ts.saveTask(task); // 创建用户 String userId = UUID.randomUUID().toString(); User user = is.newUser(userId); user.setFirstName("angus"); is.saveUser(user); //声明 效果等同于taskAssignee 再次声明会报错 ts.claim(taskId, userId); ts.claim(taskId, "100"); // List<Task> tasks = ts.createTaskQuery().taskAssignee(userId).list(); // for(Task t : tasks) { // System.out.println(t.getName()); // } //任务完成 让流程继续 ts.complete(taskId); } }
三、任务参数与附件
参数保存在表act_ru_variable里
1、基本类型参数设置
布尔:boolean
日期:date
双精度:double
整形:integer
长整型:long
空值:null
短整型:short
字符型:string
当设置参数是Map时会遍历Map依次插入多个参数;
设置:ts.setVariable(taskId, "参数名称", "参数值");
获取:taskService.getVariable(taskId, "参数名称", 参数类型);
package com.wjy.var; import java.util.UUID; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; public class VarTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); TaskService ts = engine.getTaskService(); Task task = ts.newTask(UUID.randomUUID().toString()); task.setName("测试任务"); ts.saveTask(task); ts.setVariable(task.getId(), "var1", "hello"); String s = ts.getVariable(task.getId(), "var1", String.class); System.out.println(s); } }
2、序列化参数
如果参数是自定义对象,这个类对象需要实现Serializable接口,被序列化存到act_ge_bytearray;
package com.wjy.var; import java.io.Serializable; public class Person implements Serializable { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.wjy.var; import java.util.UUID; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; public class SerVarTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); TaskService ts = engine.getTaskService(); Task task = ts.newTask(UUID.randomUUID().toString()); task.setName("测试任务"); ts.saveTask(task); Person p = new Person(); p.setId(1); p.setName("angus"); ts.setVariable(task.getId(), "person1", p); Person pr = ts.getVariable(task.getId(), "person1", Person.class); System.out.println(pr.getId() + "---" + pr.getName()); } }
3、参数的作用域
setVariable设置参数在整个流程中可用;
setVariableLocal设置参数仅在当前任务中可用;
package com.wjy.var; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; public class VarLocalTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); // 运行时服务 RuntimeService runService = engine.getRuntimeService(); // 任务服务 TaskService taskService = engine.getTaskService(); Deployment dep = rs.createDeployment().addClasspathResource("var_local.bpmn").deploy(); ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult(); ProcessInstance pi = runService.startProcessInstanceById(pd.getId()); Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult(); taskService.setVariableLocal(task.getId(), "days", 3); System.out.println("当前任务:" + task.getName() + ", days参数:" + taskService.getVariableLocal(task.getId(), "days")); taskService.complete(task.getId()); task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult(); System.out.println("当前任务:" + task.getName() + ", days参数:" + taskService.getVariableLocal(task.getId(), "days")); } }
4、数据参数
在bpmn定义文件中含有一个dataObject:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myProcess" name="My process" isExecutable="true"> <dataObject id="personName" name="personName" itemSubjectRef="xsd:string"> <extensionElements> <activiti:value>Crazyit</activiti:value> </extensionElements> </dataObject> <startEvent id="startevent1" name="Start"></startEvent> <endEvent id="endevent1" name="End"></endEvent> <userTask id="usertask1" name="task1"></userTask> <userTask id="usertask2" name="task2"></userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess"> <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="80.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="510.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="170.0" y="110.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> <omgdc:Bounds height="55.0" width="105.0" x="340.0" y="110.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="115.0" y="137.0"></omgdi:waypoint> <omgdi:waypoint x="170.0" y="137.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="275.0" y="137.0"></omgdi:waypoint> <omgdi:waypoint x="340.0" y="137.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="445.0" y="137.0"></omgdi:waypoint> <omgdi:waypoint x="510.0" y="137.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
package com.wjy.var; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; public class DataObjectTest { public static void main(String[] args) { ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 存储服务 RepositoryService rs = engine.getRepositoryService(); // 运行时服务 RuntimeService runService = engine.getRuntimeService(); // 任务服务 TaskService taskService = engine.getTaskService(); Deployment dep = rs.createDeployment().addClasspathResource("dataobject.bpmn").deploy(); ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult(); ProcessInstance pi = runService.startProcessInstanceById(pd.getId()); Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult(); String var = taskService.getVariable(task.getId(), "personName", String.class); System.out.println(var); } }
5、附件
package com.wjy.var; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; public class CreateAttachment { public static void main(String[] args) throws FileNotFoundException { // 获取流程引擎实例 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 获取任务服务组件 TaskService taskService = engine.getTaskService(); // 获取运行服务组件 RuntimeService runtimeService = engine.getRuntimeService(); // 流程存储服务组件 RepositoryService repositoryService = engine.getRepositoryService(); // 部署流程描述文件 Deployment dep = repositoryService.createDeployment() .addClasspathResource("bpmn/vacation.bpmn").deploy(); // 查找流程定义 ProcessDefinition pd = repositoryService.createProcessDefinitionQuery() .deploymentId(dep.getId()).singleResult(); // 启动流程 ProcessInstance pi = runtimeService .startProcessInstanceById(pd.getId()); // 查找任务 Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult(); // 设置任务附件 //Attachment createAttachment(String attachmentType, String taskId, String processInstanceId, String attachmentName, String attachmentDescription, InputStream content); taskService.createAttachment("web url", task.getId(), pi.getId(),"163.com", "163 web page", "http://www.163.com"); // 创建图片输入流 InputStream is = new FileInputStream(new File("result.png")); // 设置输入流为任务附件 //Attachment createAttachment(String attachmentType, String taskId, String processInstanceId, String attachmentName, String attachmentDescription, String url); taskService.createAttachment("web url", task.getId(), pi.getId(),"163.com", "163 web page", is); //保存附件 //void saveAttachment(Attachment attachment); //删除附件 //void deleteAttachment(String attachmentId); //查询附件 //Attachment getAttachment(String attachmentId); //InputStream getAttachmentContent(String attachmentId); //List<Attachment> getTaskAttachments(String taskId); //List<Attachment> getProcessInstanceAttachments(String processInstanceId); } }
6、评论
package com.wjy.var; import java.util.List; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Comment; import org.activiti.engine.task.Task; public class AddComment { public static void main(String[] args) { // 获取流程引擎实例 ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); // 获取任务服务组件 TaskService taskService = engine.getTaskService(); // 获取运行服务组件 RuntimeService runtimeService = engine.getRuntimeService(); // 流程存储服务组件 RepositoryService repositoryService = engine.getRepositoryService(); // 部署流程描述文件 Deployment dep = repositoryService.createDeployment() .addClasspathResource("vacation.bpmn").deploy(); // 查找流程定义 ProcessDefinition pd = repositoryService.createProcessDefinitionQuery() .deploymentId(dep.getId()).singleResult(); // 启动流程 ProcessInstance pi = runtimeService .startProcessInstanceById(pd.getId()); // 查找任务 Task task = taskService.createTaskQuery().processInstanceId(pi.getId()) .singleResult(); // 添加任务评论 taskService.addComment(task.getId(), pi.getId(),"this is comment message"); // 查询评论 List<Comment> comments = taskService.getTaskComments(task.getId()); System.out.println("评论数量:" + comments.size()); //增加评论 // Comment addComment(String taskId, String processInstanceId, String message); //查询评论 // Comment getComment(String commentId); // List<Comment> getTaskComments(String taskId); //删除评论 // void deleteComments(String taskId, String processInstanceId); // void deleteComment(String commentId); } }
学习技术不是用来写HelloWorld和Demo的,而是要用来解决线上系统的真实问题的.