接上篇,继续。
一、 在/src/main/resource 下,建立new -> activiti ->activiti diagram
名字为HelloWorld
然后画图,
空园,是开始,实粗线园,是结束。
中间添加task,user task,
然后添加连接线,connection -> sequence flow
保存,后自动生成png图片。
空白处 点击,在properties 视图中,可以修改流程的id和名字。
点击各个节点,在properties视图,可以修改流程名字,
各个节点,再点击main config 点击 assignee,指定办理人。
二、部署流程
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); @Test public void Deploy() { RepositoryService repositoryService = this.engine.getRepositoryService(); repositoryService.createDeployment().name("请假流程") .addClasspathResource("HelloWorld.bpmn") .addClasspathResource("HelloWorld.png").deploy(); }
zip部署
@Test public void ZipDeploy2() { // 如果资源名字前加斜杠,资源部署在src/main/resource下,如果不加斜杠,资源部署在类的同名包下 InputStream inputStream = this.getClass().getResourceAsStream("/HelloWorld.zip"); System.out.println(inputStream); ZipInputStream zipInputStream = new ZipInputStream(inputStream); RepositoryService repositoryService = this.engine.getRepositoryService(); Deployment deploy = repositoryService.createDeployment().name("请假流程2").addZipInputStream(zipInputStream).deploy(); System.out.println("Deploy name = " + deploy.getName()); System.out.println("Deploy id = " + deploy.getId() ); }
查询流程部署(非查询任务)
@Test public void queryDef() { RepositoryService repositoryService = this.engine.getRepositoryService(); List<Deployment> list = repositoryService.createDeploymentQuery().deploymentName("请假流程2").list(); for (Deployment deployment : list) { System.out.println("deployment id "+deployment.getId()); System.out.println("deployment name "+deployment.getName()); } }
repositoryService.createProcessDefinitionQuery(); //查询流程定义
repositoryService.deleteDeployment(deploymentId); //删除部署定义
repositoryService.deleteDeployment(deploymentId,cascade); //删除部署定义,级联删除,cascade取值true
查询流程图
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployId);
String processid = processDefinition.getId();
InputStream input = repositoryService.getProcessDiagram(processid);
File file = new File("d:/"+processDefinition.getDiagramResourceName());
try{
BufferedOutputStream out = new BufferOutputStream(new FileOutputStream(file));
int len = 0;
byte[] b = new byte[1024];
while((len=inputStream.read(b))!=-1){
outputStream.write(b,0,len);
outputStream.flush();
}
outputStream.close();
inputStream.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}
查询最新的流程定义
List<ProcessDefinition> list = repositorySerivice.createProcessDefinitionQuery().orderByProcessDefinition().asc().list();
if(null!=list&&list.size>0){
for(ProcessDefinition pd:list){
map.put(pd.getKey(),pd);
}
}
Collection<ProcessDefinition> values = map.value();
for(ProcessDefinition pd:values){
syso("流程定义Id"+pd.getId());
}
三、启动流程
@Test public void startProcess() { RuntimeService runtimeService = this.engine.getRuntimeService(); //ProcessInstance startProcessInstanceById = runtimeService.startProcessInstanceById("HelloWorld:4:22504"); ProcessInstance startProcessInstanceById = runtimeService.startProcessInstanceByKey("HelloWorld"); System.out.println("startProcessInstanceById:" + startProcessInstanceById.getId()); }
四、查找任务
@Test public void findProcess() { TaskService taskService = this.engine.getTaskService(); List<Task> list = taskService.createTaskQuery().taskAssignee("张三").list(); if(list!=null) { for (Task task : list) { System.out.println("id: "+task.getId()); // id: 27504 System.out.println("process id" + task.getProcessInstanceId()); System.out.println("name: "+task.getName()); System.out.println("代理人: "+task.getAssignee()); System.out.println("创建时间:"+task.getCreateTime()); System.out.println("描述:"+task.getDescription()); } } }
如果查找不到,看看流程图,是否有缺口,没有闭环
五、完成任务
@Test public void completeTask() { TaskService taskService = this.engine.getTaskService(); String taskId = "27504"; //在ru_task 表中,可以通过上步查找到。 taskService.complete(taskId); }