由于项目需要使用到Activiti工作流框架,而idea2020.1没有直接下载的插件,需要手动安装,故记录下,以后方便安装使用。
idea插件官网
选择本地安装
找到下载到的本地的jar,然后重启idea即可。
安装完之后
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mystery</groupId>
<artifactId>demo_activiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_activiti</name>
<description>Demo project for Activiti</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--- Activiti依赖导入 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
<exclusion>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
</exclusions>
</dependency>
<!--MySQL 驱动包,如果是其他库的话需要换驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
测试类
package com.mystery.activiti;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LeaveActi {
/**
* 会默认按照Resources目录下的activiti.cfg.xml创建流程引擎
*/
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
@Test
public void test() {
//读对应的配置文件,初始化
testCreateProcessEngineByCfgXml();
//部署
deployProcess();
//启动
startProcess();
//查看
queryTask();
//处理
handleTask();
}
/**
* 根据配置文件activiti.cfg.xml创建ProcessEngine
*/
@Test
public void testCreateProcessEngineByCfgXml() {
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine engine = cfg.buildProcessEngine();
}
/**
* 发布流程
* RepositoryService
*/
@Test
public void deployProcess() {
RepositoryService repositoryService = processEngine.getRepositoryService();
DeploymentBuilder builder = repositoryService.createDeployment(); //创建部署对象
builder.name("请假审批流程"); //流程名称
builder.addClasspathResource("/processes/test.bpmn"); //加载资源文件 xml发布有问题
builder.addClasspathResource("/processes/test.png");
Deployment deploy = builder.deploy();
System.out.println("流程部署的ID: "+deploy.getId());
}
/**
* 启动流程
* RuntimeService
*/
@Test
public void startProcess() {
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance leaveInstance = runtimeService.startProcessInstanceByKey("myProcess_1");
System.out.println("流程id: "+leaveInstance.getId());
}
/**
* 查看任务
* TaskService
*/
@Test
public void queryTask() {
TaskService taskService = processEngine.getTaskService();
//根据assignee(代理人)查询任务
// String assignee = "项目经理";
// String assignee = "组长";
String assignee = "张三";
List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
System.out.println("任务数量:"+tasks.size());
int size = tasks.size();
for (int i = 0; i < size; i++) {
Task task = tasks.get(i);
}
for (Task task : tasks) {
System.out.println("taskId:" + task.getId() +
",taskName:" + task.getName() +
",assignee:" + task.getAssignee() +
",createTime:" + task.getCreateTime());
}
}
/**
* 办理任务
*/
@Test
public void handleTask() {
TaskService taskService = processEngine.getTaskService();
Map<String, Object> variables = new HashMap<>();
variables.put("day", 2); // 写申请流程变量
variables.put("action","同意"); //审批流程变量 (驳回/同意)
//根据上一步生成的taskId执行任务
String taskId = "10005";//5005
taskService.complete(taskId, variables);
System.out.println("任务完成");
}
}
acitiviti.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="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti_test" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="root" />
<property name="databaseSchemaUpdate" value="true" />
</bean>
</beans>