Activiti---demo
【请假流程】
0、初始化表
1、画流程图
1.1、画流程图
1.2、设置办理人
1.3、设置流程id、name
【空白区域】
2、部署流程定义
2.1、将流程图重命名为.xml文件,再保存png文件,再将.xml文件改回bpmn文件;
3、启动流程
4、查询任务
5、办理任务
<?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.an</groupId> <artifactId>activitidemo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <spring.version>4.3.17.RELEASE</spring.version> <mysql.version>5.1.10</mysql.version> <activiti.version>5.22.0</activiti.version> <mybatis.version>3.4.6</mybatis.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>${activiti.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
log4j.rootLogger=INFO,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n log4j.logger.com.codahale.metrics=DEBUG log4j.logger.com.ryantenney=DEBUG log4j.logger.com.zaxxer=DEBUG log4j.logger.org.apache=DEBUG log4j.logger.org.hibernate=DEBUG log4j.logger.org.hibernate.engine.internal=DEBUG log4j.logger.org.hibernate.engine.validator=DEBUG log4j.logger.org.springframework=DEBUG log4j.logger.org.springframework.web=DEBUG log4j.logger.org.springframework.security=DEBUG
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useSSL=false&allowPublicKeyRetrieval=true"/> <property name="jdbcUsername" value="root"/> <property name="jdbcPassword" value="123456"/> <property name="databaseSchemaUpdate" value="true"/> </bean> </beans>
package com.an.init; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.ProcessEngines; import org.junit.jupiter.api.Test; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class InitTables { @Test public static void initOne(){ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/activiti?useSSL=false"); dataSource.setUsername("root"); dataSource.setPassword("123456"); ProcessEngineConfiguration processEngineConfiguration=ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); //数据源 processEngineConfiguration.setDataSource(dataSource); //配置表的初始化方式 processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); //获取流程引擎 ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); System.out.println(processEngine); } @Test public static void initTwo(){ ProcessEngineConfiguration processEngineConfiguration=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("/activiti.cfg.xml"); processEngineConfiguration.buildProcessEngine(); } @Test public static void initThree(){ ProcessEngines.getDefaultProcessEngine(); } }
package com.an.helloworld; import org.activiti.engine.*; import org.activiti.engine.repository.Deployment; import org.activiti.engine.task.Task; import org.junit.jupiter.api.Test; import java.util.List; public class HelloWorld { private ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine(); /** * 流程部署 */ @Test public void deployProcess(){ RepositoryService repositoryService = processEngine.getRepositoryService(); Deployment deployment = repositoryService.createDeployment().name("请假流程") .addClasspathResource("helloworld.bpmn") .addClasspathResource("helloworld.png") .deploy(); System.out.println("部署成功!流程部署id:"+deployment.getId()); } /** * 启动流程 */ @Test public void startProcess(){ RuntimeService runtimeService = processEngine.getRuntimeService(); runtimeService.startProcessInstanceById("helloworld:1:4"); System.out.println("流程启动成功!"); } /** * 查询任务 */ @Test public void findTasks(){ TaskService taskService = processEngine.getTaskService(); List<Task> taskList = taskService.createTaskQuery().taskAssignee("王五").list(); for (Task t:taskList) { System.out.println("任务ID:"+t.getId()); System.out.println("流程实例ID:"+t.getProcessInstanceId()); System.out.println("执行实例ID:"+t.getExecutionId()); System.out.println("流程定义ID:"+t.getProcessDefinitionId()); System.out.println("任务名称:"+t.getName()); System.out.println("任务办理人:"+t.getAssignee()); } } /** * 办理任务 */ @Test public void doTask(){ TaskService taskService = processEngine.getTaskService(); taskService.complete("7502"); System.out.println("王五办理任务"); } }