Activity6.0集成springboot2.0
1、添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaofeiyang</groupId> <artifactId>activiti</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter</artifactId> <version>7.1.0.M1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!-- <scope>runtime</scope>--> <version>5.1.30</version> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxrs</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!--引入spring security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.0.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> <version>1.0.8.RELEASE</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>net.sf.jmimemagic</groupId> <artifactId>jmimemagic</artifactId> <version>0.1.5</version> </dependency> <!--<dependency>--> <!--<groupId>org.mybatis.spring.boot</groupId>--> <!--<artifactId>mybatis-spring-boot-starter</artifactId>--> <!--<version>1.3.2</version>--> <!--</dependency>--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>16.0.1</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.5.0</version> </dependency> <!-- <dependency>--> <!-- <groupId>commons-lang</groupId>--> <!-- <artifactId>commons-lang</artifactId>--> <!-- <version>2.6</version>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>commons-collections</groupId>--> <!-- <artifactId>commons-collections</artifactId>--> <!-- <version>3.2.1</version>--> <!-- </dependency>--> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.2</version> </dependency> <!-- <dependency>--> <!-- <groupId>commons-collections</groupId>--> <!-- <artifactId>commons-collections</artifactId>--> <!-- <version>3.1</version>--> <!-- </dependency>--> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.2</version> </dependency> <!--阿里云短信--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency> <!--生成二维码--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.0</version> </dependency> <!--阿里云日志--> <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>aliyun-log</artifactId> <version>0.6.7</version> <exclusions> <exclusion> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> </exclusion> </exclusions> </dependency> <!--excel导入导出依赖--> <!--<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency>--> <!--引入easyexcel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.1.2-beta5</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb-codegen</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-http</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-local</artifactId> <version>1.7.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis/axis --> <!-- https://mvnrepository.com/artifact/axis/axis --> <dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <finalName>${project.artifactId}-1.0.0</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、创建activiti数据库
3、创建配置activiti配置文件
activiti.cfg.xml
<?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="databaseType" value="mysql"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti"></property> <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUsername" value="root"></property> <property name="jdbcPassword" value="123"></property> </bean> </beans>
4、创建activiti表
import org.activiti.engine.impl.db.DbSchemaCreate;
/**
* @author: yangchun
* @description:
* @date: Created in 2020-05-26 17:04
*/
public class CreateActivitiTables {
public static void main(String[] args) {
DbSchemaCreate.main(args);
}
}
5、创建流程图
<?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="databaseType" value="mysql"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti"></property> <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUsername" value="root"></property> <property name="jdbcPassword" value="123"></property> </bean> </beans>
6、activity表介绍
ACT_RE_*,repository,表示定义的流程等静态资源。
ACT_RU_*,表示runtime,表示运行的时的表,包含流程实例,任务,变量,异步任务,
ACT_HI_*,表示history,表示历史数据
ACT_GE_*,表示通用数据,
7、activiti 的api操作如下
1、部署流程
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.springframework.util.ResourceUtils; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 15:47 */ public class ActivitiDeployment { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); String path = Thread.currentThread().getContextClassLoader().getResource("").getPath(); Deployment deployment = repositoryService.createDeployment().addClasspathResource("askForLeave.bpmn") .addClasspathResource("askForLeave.png") .name("请假流程") .deploy(); } }
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import java.io.InputStream; import java.util.zip.ZipInputStream; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 15:47 */ public class ActivitiZipDeployment { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); InputStream path = ActivitiZipDeployment.class.getClassLoader().getResourceAsStream("askForleave.zip"); ZipInputStream zipInputStream=new ZipInputStream(path); Deployment deployment = repositoryService.createDeployment().addZipInputStream(zipInputStream) .name("请假流程") .deploy(); } }
2、启动流程
package com.xiaofeiyang.activiti; 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.runtime.ProcessInstance; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 16:57 */ public class ActivitiRuntime { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1"); } }
3、查询分配任务,完成任务
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import java.util.List; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 17:24 */ public class ActivityTask { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); List<Task> list = taskService.createTaskQuery().processDefinitionKey("myProcess_1").taskAssignee("yangchun").list(); for(Task task:list){ System.out.println(task.getId()); taskService.complete(task.getId()); } } }
4、查询流程和删除流程
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import java.util.List; /** * @author: yangchun * @description: * @date: Created in 2020-06-03 15:12 */ public class ActivitiProcessDefinition { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().processDefinitionKey("myProcess_1"). orderByProcessDefinitionVersion().desc().list(); for(ProcessDefinition processDefinition:processDefinitions){ System.out.println(processDefinition.getDeploymentId()); System.out.println(processDefinition.getId()); System.out.println(processDefinition.getName()); System.out.println(processDefinition.getKey()); //第二个参数强制删除未完成的流程 repositoryService.deleteDeployment(processDefinition.getDeploymentId()); } } }
5、查询流程历史动作
package com.xiaofeiyang.activiti; import org.activiti.engine.HistoryService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.history.HistoricActivityInstance; import org.activiti.engine.history.HistoricActivityInstanceQuery; import org.activiti.engine.repository.Deployment; import java.util.List; /** * @author: yangchun * @description: * @date: Created in 2020-06-01 15:47 */ public class ActivitiHistory { public static void main(String[] args) { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); HistoryService historyService = processEngine.getHistoryService(); HistoricActivityInstanceQuery querInstance = historyService.createHistoricActivityInstanceQuery(); querInstance.processInstanceId("2501"); List<HistoricActivityInstance> list = querInstance.list(); for(HistoricActivityInstance historicActivityInstance:list){ System.out.println(historicActivityInstance.getActivityId()); System.out.println(historicActivityInstance.getActivityName()); System.out.println(historicActivityInstance.getProcessDefinitionId()); System.out.println(historicActivityInstance.getProcessInstanceId()); } } }
6、查询流程图和png图片
package com.xiaofeiyang.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.repository.ProcessDefinitionQuery; import org.apache.commons.io.IOUtils; import java.io.*; import java.util.List; /** * @author: yangchun * @description: * @date: Created in 2020-06-03 15:12 */ public class QueryBpmFile { public static void main(String[] args) throws IOException { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); processDefinitionQuery.processDefinitionKey("myProcess_1"); ProcessDefinition processDefinition = processDefinitionQuery.singleResult(); String deploymentId = processDefinition.getDeploymentId(); InputStream inputStream = repositoryService.getResourceAsStream(deploymentId,processDefinition.getDiagramResourceName()); InputStream inputStream2 = repositoryService.getResourceAsStream(deploymentId,processDefinition.getResourceName()); OutputStream outputStream = new FileOutputStream("E:\\spark\\"+processDefinition.getDiagramResourceName()); OutputStream outputStream2 = new FileOutputStream("E:\\spark\\"+processDefinition.getResourceName()); IOUtils.copy(inputStream,outputStream); IOUtils.copy(inputStream2,outputStream2); outputStream.close(); outputStream2.close(); inputStream.close(); inputStream2.close(); } }
package com.xiaofeiyang.activiti;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.util.ResourceUtils;
/**
* @author: yangchun
* @description:
* @date: Created in 2020-06-01 15:47
*/
public class ActivitiDeployment {
public static void main(String[] args) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("askForLeave.bpmn")
.addClasspathResource("askForLeave.png")
.name("请假流程")
.deploy();
}
}