注:IDEA在给任务Assignee和Task Listeners设置不上的问题

一.创建项目

创建一个新的SpringBoot的一个项目,pox.xml引入Activiti工作流和数据库Mysql驱动和连接相关依赖

<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.shunyi</groupId>
  <artifactId>ActivitiDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ActivitiDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>6.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二.IDEA安装Activiti的BPMN流程图绘制插件

Help-->Install New Software

Activiti BPMN 2.0 designer

http://activiti.org/designer/update/

 

springboot流程审批技术 springboot 审批流_Test

springboot流程审批技术 springboot 审批流_spring_02

一直Next直到Finish

三.绘制BPMN流程图

重新打开IDEA,在resources项目目录下创建一个文件夹processes,文件夹内创建一个BPMN文件

springboot流程审批技术 springboot 审批流_Test_03

springboot流程审批技术 springboot 审批流_System_04

 推拽即可,构建自己的业务流程  --可以右键Open With  XML编辑(报错排查)

BPMN元素使用说明地址:https://wenku.baidu.com/view/d99e98220b1c59eef8c7b4f0.html?sxts=1568095151066

Propertis事件和属性设置:

1.组长审批:

springboot流程审批技术 springboot 审批流_System_05

处理人:

springboot流程审批技术 springboot 审批流_Test_06

2.经理审批:采用Task Listeners设置处理人

springboot流程审批技术 springboot 审批流_springboot流程审批技术_07

处理人:

先New  

springboot流程审批技术 springboot 审批流_springboot流程审批技术_08

springboot流程审批技术 springboot 审批流_System_09

package com.shunyi.ActivitiDemo.controller;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class Assignee implements TaskListener {

	public void notify(DelegateTask delegateTask) {
		
		delegateTask.setAssignee("李四");
	}
	
}

注:Task listeners可以监听外部类,可以根据自身业务需求在相对应的环节操作外部类

3.流程网关条件:

springboot流程审批技术 springboot 审批流_spring_10

springboot流程审批技术 springboot 审批流_Test_11

 

四.Activiti28张表的构建

application.yml:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: #用户名
    password: #密码
    url:      #地址
  activiti:
    history-level: full
    check-process-definitions: false
server:
  port: 9099

src->main->java下创建一个config类

package com.shunyi.config;

import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;


/**
 * @author codinglife
 * @title: activitiConfig
 * @projectName activitidemo
 * @description: 获取数据库信息,生成表
 * @date 2019/9/916:40
 */
public class activitiConfig extends AbstractProcessEngineAutoConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource activitiDataSource(){
        return DataSourceBuilder.create().build();
    }


    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(PlatformTransactionManager platformTransactionManager, SpringAsyncExecutor springAsyncExecutor) throws IOException {
        return  baseSpringProcessEngineConfiguration(activitiDataSource(),platformTransactionManager,springAsyncExecutor);
    }





}

Application启动类:

package com.shunyi;

import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;

@SpringBootApplication(exclude = {LiquibaseAutoConfiguration.class,
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
        SecurityAutoConfiguration.class})   //排查免注册
public class ActivitidemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivitidemoApplication.class, args);
    }

}

启动后可以看到自己的Mysql数据库中生成了28张表

springboot流程审批技术 springboot 审批流_spring_12

五.流程的开始

Avtiviti常用service注入

package com.shunyi.ActivitiDemo;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiDemoTest {

	//仓储服务的表 创建和查询流程部署(流程模板--一个BPMN流程图详当于一个模板)
	@Autowired
	private RepositoryService repositoryService; 
	
    //运行时相关表开启流程实例,设置流程变量(对象参数,申请人,申请内容,申请时间.....)
	@Autowired
	private RuntimeService runtimeService;   
	
    //任务服务(每一个审批环节审批人员的任务,获取流程变量)
	@Autowired
	private TaskService taskService;   

	//历史服务(可以查看审批人曾经审批完成了哪些项目,审批项目总共花了多少时间,以及在哪个环节比较耗费时间等等)
	@Autowired
	private HistoryService historyService;  
	
	//实体服务(主要是操作用户信息,用户分组信息等,组信息包括如部门表和职位表  一般用自建表)
	@Autowired
	private IdentityService identityService;

1.部署流程

/**
	 * 部署流程
	 */
	@Test
	public void publishProcess() {
		String processDefinitionKey=null;
	Deployment deployment=	repositoryService.createDeployment().addClasspathResource("processes/ActivitiDemo.bpmn").deploy();
	System.out.println("部署编号:"+deployment.getId());
	
	List<ProcessDefinition> processDefinition=repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
	for (ProcessDefinition pro : processDefinition) {
		System.out.println("部署的key"+pro.getKey());
		processDefinitionKey=pro.getKey();
	}	
	
	// 启动流程    
	Map<String, Object>map=new HashMap<String, Object>();    //设置参数变量(表单数据)
	map.put("title", "我要申请一个审批");
	map.put("peopleCount",1);
	map.put("date", new Date());
	runtimeService.startProcessInstanceByKey(processDefinitionKey,map);

	
	}



	/**
	 * 删除流程
	 */
	@Test
	public void removeProcess() {
		List<ProcessDefinition> processDefinitions=repositoryService.createProcessDefinitionQuery().list();
		for (ProcessDefinition processD : processDefinitions) {
			repositoryService.deleteDeployment(processD.getDeploymentId(), true);   //true 级联删除
		}
		System.out.println("移除成功");
	}

  2.查询不同处理人员的任务

/**
	 * 查询任务,应用场景,可以在这里获取不同处理人(组长或经理)待处理的任务信息传给前台显示
	 */
	@Test
	public void quertTask() {
		String assignee="张三";     //这里是UserTask中设置的assignee    
	List<Task> task=	taskService.createTaskQuery().taskAssignee(assignee).list();
		for (Task t : task) {
                          //获取任务明细
			System.out.println("任务编号:"+t.getId()+"\n任务名称:"+t.getName()+"\n处理人:"+t.getAssignee()+"\n创建时间:"+t.getCreateTime());
			 //获取参数变量
			String title=(String) taskService.getVariable(t.getId(), "title");
			Integer peopleCount=(Integer) taskService.getVariable(t.getId(), "peopleCount");
			Date date=(Date) taskService.getVariable(t.getId(), "date");
			System.out.println("任务编号:"+t.getId()+"\n标题:"+title+"\n人数"+peopleCount+"\n时间"+date);
		
		}
					
	}

3.组长审批 

/**
	 * 处理任务(这里选择组长),应用场景,这里将上面查询任务通过前台将任务编号出过来
	 */
	@Test
	public void habdleTask() {
		//我们在网关设置了条件message的不同处理方式(同意还是不同意)
		
		Map<String, Object> map=new HashMap<String, Object>();  //参数
		//不同意  直接结束
		//map.put("message", "不同意");
		
		//同意  交给经理可查询到任务进行审批
		map.put("message", "同意");
		
		String taskId="5015";   //任务编号
		
		taskService.complete(taskId,map);      //任务处理
		System.out.println("完成任务"+taskId);
	}

4.经理审批

/**
	 * 处理任务(这里选择经理),应用场景,这里将上面查询任务通过前台将任务编号出过来
	 */
	@Test
	public void habdleTaskM() {
		String taskId="5015";   //任务编号
		taskService.complete(taskId);
                System.out.println("完成任务"+taskId);
	}