原创

activiti操作流程的几个demo

1.详细查看github的代码

https://github.com/mx342/luceneDemo


2.pom文件

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cyjz</groupId>
    <artifactId>activiti</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>activiti</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <activiti.version>7.0.0.Beta1</activiti.version>
    </properties>

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

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.11</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

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

        <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>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-model</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-converter</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-json-converter</artifactId>
            <version>7.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-bpmn-layout</artifactId>
            <version>7.0.0.GA</version>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
            <exclusions>
                <exclusion>
                    <groupId>tk.mybatis</groupId>
                    <artifactId>mapper-spring-boot-autoconfigure</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-autoconfigure</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

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

</project>

3.conf配置(本案例使用的是springboot)

activiti配置

package com.cyjz.activiti.config;

import org.activiti.engine.*;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

//import org.activiti.spring.SpringProcessEngineConfiguration;

/**
 * Created by liuruijie on 2017/2/20.
 * activiti工作流配置
 */
@Configuration
public class Cfg_Activiti {

    //流程配置,与spring整合采用SpringProcessEngineConfiguration这个实现
    @Bean
    public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource, PlatformTransactionManager transactionManager){
        SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
        processEngineConfiguration.setDataSource(dataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate("true");
        processEngineConfiguration.setDatabaseType("mysql");

        processEngineConfiguration.setTransactionManager(transactionManager);

//        流程图字体
//        processEngineConfiguration.setActivityFontName("宋体");
//        processEngineConfiguration.setAnnotationFontName("宋体");
//        processEngineConfiguration.setLabelFontName("宋体");

        return processEngineConfiguration;
    }

    //流程引擎,与spring整合使用factoryBean
    @Bean
    public ProcessEngineFactoryBean processEngine(ProcessEngineConfiguration processEngineConfiguration){
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
        return processEngineFactoryBean;
    }

    //八大接口
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine){
        return processEngine.getRepositoryService();
    }

    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine){
        return processEngine.getRuntimeService();
    }

    @Bean
    public TaskService taskService(ProcessEngine processEngine){
        return processEngine.getTaskService();
    }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine){
        return processEngine.getHistoryService();
    }
//7里面已经删除了
//    @Bean
//    public FormService formService(ProcessEngine processEngine){
//        return processEngine.getFormService();
//    }
//
//    @Bean
//    public IdentityService identityService(ProcessEngine processEngine){
//        return processEngine.getIdentityService();
//    }

    @Bean
    public ManagementService managementService(ProcessEngine processEngine){
        return processEngine.getManagementService();
    }

    @Bean
    public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine){
        return processEngine.getDynamicBpmnService();
    }

    //八大接口 end
}

数据源配置:

package com.cyjz.activiti.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.cyjz.activiti.pojo.dao.mapper")
public class DataSourceOneConfig {
	

	  @Bean
	  @ConfigurationProperties(prefix = "spring.datasource")
	  @Primary //设置主数据源1
	  public DataSource dataSource(){
	      return new DruidDataSource();
	  }
	
//	  @Bean(name = "sqlSessionFactory")
//	  @Primary
//	  public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)throws Exception{
//	      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//	      bean.setDataSource(dataSource);
//	      bean.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis/mybatis-config.xml"));
//	      bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml"));
//	      return bean.getObject();
//	  }
//
//	  @Bean(name = "sqlSessionTemplate")
//	  @Primary
//	  public SqlSessionTemplate sqlSessionTemplateOne(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception{
//	      return new SqlSessionTemplate(sqlSessionFactory);
//	  }
	  
}

yml配置:

spring:
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
  datasource:
    #第一个数据源
    username: root
    password: mysql
    #    url: jdbc:mysql://120.26.82.31:3306/tianyou?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
    url: jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC
#    url: jdbc:mysql://47.111.69.10:3306/tianyou?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    hostIp: 120.77.223.232
    dbName: tianyou

4.demo代码

package com.cyjz.activiti;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricDetailQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
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;
import sun.nio.ch.IOUtil;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiApplicationTests {
    
    @Autowired
    ProcessEngine processEngine;

    @Autowired
    ProcessEngineConfiguration processEngineConfiguration;
    /**
     * 作者 ZYL
     * 功能描述 : 生成表,整个程序只需要执行一次
     * 日期 2019/5/3 11:22
     * 参数
     * 返回值 void
     */
    @Test
    public void testGeTable(){
//        1.创建processEngineConfiguration对象
//        @Autowired
//        2.创建processEngine对象
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
//        processEngine.get

        //3.输出processEngine对象
        System.err.println(processEngine);

    }


    /**
     * 作者 ZYL
     * 功能描述 : 流程定义4步骤
     * 日期 2019/5/3 19:38  
     * 参数 
     * 返回值 void
     */
    @Test
    public void processDefine() {
        //1.获得activiti相关service
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        System.err.println(repositoryService);
        //2.createDeployment方法进行部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("static/diagram/holiday.png")
                .addClasspathResource("static/diagram/holiday.bpmn")
                .name("请假申请")
                .deploy();
        //3.输出部署的一些信息
        System.err.println("流程部署id:" + deploy.getId());
        System.err.println("流程部署名称:" + deploy.getName());
    }

    
    /**
     * 作者 ZYL
     * 功能描述 : 启动流程实例,前提是已经完成流程定义的部署工作
     * 背后影响的表:
     *  act_hi_actinst  已完成的活动实例
     *  act_hi_identitylink 参与者信息
     *  act_hi_procinst  流程实例
     *  act_hi_taskinst  任务实例
     *  act_ru_execution 执行表
     *  act_ru_identitylink 参与者信息
     *  act_ru_task 任务
     * 日期 2019/5/3 20:03  
     * 参数 
     * 返回值 void
     */
    @Test
    public void startProcessInstance(){
        //1.获取runtimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //2.根据流程定义key启动流程,此处需要知道流程定义的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
        //3.输出实例相关信息
        System.err.println("流程部署id:" + processInstance.getDeploymentId());
        System.err.println("流程实例id:" + processInstance.getId());
        System.err.println("流程定义id:" + processInstance.getProcessDefinitionId());
        System.err.println("流程活动id:" + processInstance.getActivityId());


    }
    /**
     * 作者 ZYL
     * 功能描述 : 查询用户的任务列表,天佑里面使用的是用户工号
     * 日期 2019/5/3 20:29  
     * 参数 
     * 返回值 void
     */
    @Test
    public void findPersonalTaskList(){
        //1.任务负责人
        String assignee = "zhangsan";
        //2.创建TaskService
        TaskService taskService = processEngine.getTaskService();
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("holiday")
                .taskAssignee(assignee)
                .list();
        //任务列表的展示
        for (Task task : list) {
            System.err.println("流程实例id:" + task.getProcessInstanceId());
            System.err.println("任务id:" + task.getId());
            System.err.println("任务责任人:" + task.getAssignee());
            System.err.println("任务名称:" + task.getName());
            System.err.println("======================");
        }
    }
    /*
     * 作者 ZYL
     * 功能描述 : 处理任务完成任务
     * 操作的表:
     *  act_hi_actinst
     *  act_hi_identitylink
     *  act_hi_taskinst
     *  act_ru_execution
     *  act_ru_identitylikn
     *  act_ru_task
     * 日期 2019/5/3 20:36
     * 参数
     * 返回值 void
     */
    @Test
    public void completTask(){
        //任务id 测试为:2505,5005
        String taskId = "22502";
        //创建TaskService
        TaskService taskService = processEngine.getTaskService();
        //完成任务
        taskService.complete(taskId);
        System.err.println("完成任务id:" + taskId);
    }
    /**
     * 作者 ZYL
     * 功能描述 : zip方式部署流程
     * 日期 2019/5/3 21:24
     * 参数
     * 返回值 void
     */
    @Test
    public void deployProcessByZip(){
        //定义zip输入流
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/diagram/holiday.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        //获取repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //流程部署
        Deployment deployment = repositoryService.createDeployment().addZipInputStream(zipInputStream).deploy();
        System.err.println("流程部署id:" + deployment.getId());
        System.err.println("流程部署名称:" + deployment.getName());
    }
    /**
     * 作者 ZYL
     * 功能描述 : 流程定义的查询
     * 日期 2019/5/3 22:15
     * 参数
     * 返回值 void
     */
    @Test
    public void queryProceccDefinition(){
        //流程定义
        String processDefinitionKey = "holiday";
        //获取repositorySerice
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //查询流程定义
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //遍历查询结果
        List<ProcessDefinition> list = processDefinitionQuery.processDefinitionKey(processDefinitionKey)
                .orderByProcessDefinitionVersion().desc().list();
        for (ProcessDefinition processDefinition : list) {
            System.err.println("------------------");
            System.err.println("流程部署id:" + processDefinition.getId());
            System.err.println("流程定义名称:" + processDefinition.getName());
            System.err.println("流程定义key:" + processDefinition.getKey());
            System.err.println("流程定义版本:" + processDefinition.getVersion());
            System.err.println("流程部署的id:" + processDefinition.getDeploymentId());
        }


    }
    /**
     * 作者 ZYL
     * 功能描述 : 删除已经部署成功的流程定义
     * 日期 2019/5/3 22:32
     * 参数
     * 返回值 void
     */
    @Test
    public void deleteDeployment(){
        //流程部署id
        String deploymentId = "15001";
        //通过流程引擎获取repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除流程定义,如果该流程定义已经有流程实例启动则删除时出错
        repositoryService.deleteDeployment(deploymentId);
        //设置true级联删除流程定义,即时该流程有流程实例启动也可以删除,设置为false非级别删除方式,如果流程
//        repositoryService.deleteDeployment(deploymentId,true);
    }

    /**
     * 作者 ZYL
     * 需求:1.从activiti的act_ge_bytearray表中读取两个资源文件
     *      2.将两个资源文件保存到本地
     *      用户想要查看请假流程具体有哪些步骤要走
     * 技术方案:
     *      1.第一种:使用activiti的api来实现
     *      2.第二种:从原理层jdbc的blob或者clob类型读取
     *      3.IO流转换:最好commons-io.jar可以轻松解决
     * 功能描述 : 通过流程定义对象获取流程定义资源,获取bpmn和png
     * 日期 2019/5/3 23:15
     * 参数 null
     * 返回值
     */
    @Test
    public void getProcessResources() throws Exception{
        //流程定义id
        String processDefinitionKey = "holiday";
        //获取repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //流程定义对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //查询到流程定义
        ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionKey(processDefinitionKey).singleResult();
        //获取部署id
        String deploymentId = processDefinition.getDeploymentId();
        //获取bpmn资源的名称
        String resource_bpmn = processDefinition.getResourceName();
        //获取png图片资源的名称
        String resource_png = processDefinition.getDiagramResourceName();
//
        //通过repositoryService的方法,实现读取图片信息及bpmn文件信息(输入流)
        //getResourceAsStream,两个参数:1.表示部署id,2.表示资源名称
        InputStream pngIs = repositoryService.getResourceAsStream(deploymentId,resource_png);
        InputStream bpmnIs = repositoryService.getResourceAsStream(deploymentId,resource_bpmn);

        //构建outputStream流
        OutputStream pngOs = new FileOutputStream("E:\\BaiduNetdiskDownload\\3-1 Activiti7工作流引擎\\" + resource_png);
        OutputStream bpmnOs = new FileOutputStream("E:\\BaiduNetdiskDownload\\3-1 Activiti7工作流引擎\\" + resource_bpmn);
        //输入流,输出流的转换 commons-io的方法
        IOUtils.copy(pngIs,pngOs);
        IOUtils.copy(bpmnIs,bpmnOs);

        //关闭流
        pngIs.close();
        pngOs.close();
        bpmnIs.close();
        bpmnOs.close();
    }
    
    /**
     * 作者 ZYL
     * 功能描述 : 流程历史信息的查看
     * 描述:即时流程定义已经删除了,流程执行的历史信息通过前面的分析,依然保存在activiti的act_hi_*的相关表中,所以我们还是可以查询流程执行的历史信息,
     * 可以通过HistoryService来查看相关记录
     * 日期 2019/5/3 23:48  
     * 参数 null
     * 返回值
     */
    @Test
    public void testHistory01(){
        //获取historyService对象
        HistoryService historyService = processEngine.getHistoryService();
        //获取查询对象
        HistoricActivityInstanceQuery query = historyService.createHistoricActivityInstanceQuery();
        //添加查询条件
        query.processInstanceId("17501");
        //执行查询
        List<HistoricActivityInstance> list = query.list();
        //遍历结果
        for (HistoricActivityInstance historicActivityInstance : list) {
            System.err.println("activitiId:" + historicActivityInstance.getActivityId());
            System.err.println("activitiName:" + historicActivityInstance.getActivityName());
        }

    }

}


正文到此结束
本文目录