一、目录展示
二、导入依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> </properties> <dependencies> <!--activiti常规依赖--> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>7.0.0.Beta1</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-model</artifactId> <version>7.0.0.Beta1</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-converter</artifactId> <version>7.0.0.Beta1</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-json-converter</artifactId> <version>7.0.0.Beta1</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-layout</artifactId> <version>7.0.0.Beta1</version> </dependency> <dependency> <groupId>org.activiti.cloud</groupId> <artifactId>activiti-cloud-services-api</artifactId> <version>7.0.0.Beta1</version> </dependency> <!--activiti和Spring整合依赖--> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>7.0.0.Beta1</version> </dependency> <!--数据库依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--单测依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.7.RELEASE</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--数据源--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies>
三、配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/activiti"/> <property name="username" value="root"/> <property name="password" value="123"/> </bean> <!--配置ProcessEngineConfiguration--> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <!--数据源--> <property name="dataSource" ref="dataSource"/> <!--配置事务--> <property name="transactionManager" ref="transactionManager"/> <!--数据生成策略 true false create drop-create --> <property name="databaseSchemaUpdate" value="true"/> </bean> <!--配置流程引擎ProcessEngine--> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"/> </bean> <!--配置资源服务RepositoryService--> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/> <!--配置流程运行RuntimeService--> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/> <!--配置任务管理TaskService--> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/> <!--配置历史管理HistoryService--> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
四、流程bpmn文件
五、流程部署
1、代码块
package com.zn; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.runtime.ProcessInstance; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; /** * 流程部署 */ @Test public void deployment(){ repositoryService.createDeployment() .addClasspathResource("bpmnfiles/holidayInclusiveGateway.bpmn") .name("测试Spring流程部署") .deploy(); } }
2、数据库
六、启动流程实例
1、代码块
package com.zn; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.runtime.ProcessInstance; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; /** * 流程实例 */ @Test public void getInstance(){ //设置流程变量 Map<String, Object> assMap = new HashMap<>(); assMap.put("userType", 1); //启动流程实例 ProcessInstance holidayGroup = runtimeService.startProcessInstanceByKey("holidayInc", assMap); System.out.println("流程实例:" + holidayGroup.getId()); } }
2、数据库
七、处理任务
1、代码块
package com.zn; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; @Resource private TaskService taskService; /* *任务处理 */ @Test public void completeTask(){ //查询到自己的任务,然后通过任务ID进行提交 Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("zhangsan").singleResult(); //任务处理 taskService.complete(task.getId()); } }
2、数据库
八、处理任务
1、代码块
package com.zn; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; @Resource private TaskService taskService; /* *任务处理 */ @Test public void completeTask(){ //查询到自己的任务,然后通过任务ID进行提交 Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("lisi").singleResult(); //任务处理 taskService.complete(task.getId()); } }
2、数据库
九、处理任务
1、代码块
package com.zn; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; @Resource private TaskService taskService; /* *任务处理 */ @Test public void completeTask(){ //查询到自己的任务,然后通过任务ID进行提交 Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("wangwu").singleResult(); //任务处理 taskService.complete(task.getId()); } }
2、数据库
十、处理任务
1、代码块
package com.zn; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; @Resource private TaskService taskService; /* *任务处理 */ @Test public void completeTask(){ //查询到自己的任务,然后通过任务ID进行提交 Task task = taskService.createTaskQuery().processDefinitionKey("holidayInc").taskAssignee("sunqi").singleResult(); //任务处理 taskService.complete(task.getId()); } }
2、数据库
十一、查看历史任务
1、代码块
package com.zn; import org.activiti.engine.HistoryService; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.history.HistoricActivityInstance; import org.activiti.engine.history.HistoricActivityInstanceQuery; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:activiti-spring.xml") public class ActivitiSpringTest { @Resource private RepositoryService repositoryService; @Resource private RuntimeService runtimeService; @Resource private TaskService taskService; @Resource private HistoryService historyService; /** * 查看历史任务 */ @Test public void getHistory(){ //获取历史任务 HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery(); //获取指定流程实例的任务 historicActivityInstanceQuery.processInstanceId("2501"); //获取任务列表 List<HistoricActivityInstance> list = historicActivityInstanceQuery.list(); for (HistoricActivityInstance ai : list) { System.out.println("任务节点ID:"+ai.getActivityId()); System.out.println("任务节点名称:"+ai.getActivityName()); System.out.println("流程实例ID信息:"+ai.getProcessDefinitionId()); System.out.println("流程实例ID信息:"+ai.getProcessInstanceId()); System.out.println("=============================="); } } }
2、控制台