流程例如以下,首先须要部门经理审批。假设请假天数大于2天,则须要总经理审批,否则HR审批就可以
一:创建maven项目,项目结构例如以下:
增加依赖
pom.xml
activiti.cfg.xmlorg.activiti activiti-engine 5.17.0 org.activiti activiti-bpmn-layout 5.17.0 org.postgresql postgresql 9.3-1103-jdbc41 org.slf4j slf4j-log4j12 1.7.12
数据库使用postgresql
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="databaseSchemaUpdate" value="drop-create"/> <property name="jdbcUrl" value="jdbc:postgresql://127.0.0.1/db_cloud"/> <property name="jdbcDriver" value="org.postgresql.Driver"/> <property name="jdbcUsername" value="admin"/> <property name="jdbcPassword" value="root"/> <property name="jobExecutorActivate" value="true"/> </bean> </beans>
流程文件leave.bpmn20.xml流程图例如以下:= 3}]]>
二:代码演示
Leave.java
package com.lala.activiti;import java.util.HashMap;import java.util.List;import java.util.Map;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngineConfiguration;import org.activiti.engine.RepositoryService;import org.activiti.engine.RuntimeService;import org.activiti.engine.TaskService;import org.activiti.engine.task.Task;import org.activiti.engine.task.TaskQuery;public class Leave{ static void log(Object obj) { System.out.println(obj); } static void run(ProcessEngine processEngine) throws Exception { RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("leave.bpmn20.xml").deploy(); RuntimeService runtimeService = processEngine.getRuntimeService(); Map当variables = new HashMap<>(); variables.put("userid", "10010"); variables.put("day", 3); runtimeService.startProcessInstanceByKey("leave_process", variables); TaskService taskService = processEngine.getTaskService(); TaskQuery query = taskService.createTaskQuery(); List tasks = query.list(); for(Task task : tasks) { log(task.getId() + "," + task.getName()); taskService.complete(task.getId(), variables); } log("--------------------"); tasks = query.list(); for(Task task : tasks) { log(task.getId() + "," + task.getName()); taskService.complete(task.getId(), variables); } log("--------------------"); tasks = query.list(); for(Task task : tasks) { log(task.getId() + "," + task.getName()); taskService.complete(task.getId(), variables); } } public static void main(String[] args) { ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine(); try { run(processEngine); } catch (Exception e) { e.printStackTrace(); } processEngine.close(); }}
variables.put("day", 3);
大于等于3的时候,输出
9,部门经理审批
-------------------- 11,总经理审批 -------------------- 13,HR审批否则,输出
9,部门经理审批
-------------------- 11,HR审批