Activiti应用实践(二)-流程部署和创建流程实例

接上一篇描述,开发完请假流程后可以导出一份bpmn格式的xml文件,这个文件可用于部署流程和创建流程实例。包含以下几个步骤:

一、引入activiti依赖的jar包

创建spring boot的maven项目,在pom文件中添加如下依赖項:


    org.activiti
    activiti-spring-boot-starter-basic
    6.0.0



    org.postgresql
    postgresql
    runtime

我用的数据库是postgres,也可以使用MySQL等其它数据库。activiti支持哪些数据库?查看官方文档说明。

二、配置数据库连接

在spring boot项目的application.yml文件中增加数据库连接配置:

spring:
 datasource:
  activiti:
   jdbcUrl: jdbc:postgresql://127.0.0.1:5432/activiti
   username: postgres
   password: postgres
   driver-class-name: org.postgresql.Driver

三、增加datasource配置

package elon.activiti.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Activiti数据源配置
 */
@Configuration
@MapperScan(basePackages = "", value = "activitiSessionFactory")
public class ActivitiDataSource {

    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.activiti")
    public DataSource getDataSource(){
        DataSourceBuilder builder = DataSourceBuilder.create();
        return builder.build();
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        try {
            return sqlSessionFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

四、将流程xml文件及流程图片文件放到resources下

服务启动时,会从resources/processes下加载xml文件初始化数据库,创建activiti的28张表。
在这里插入图片描述

五、添加用于测试流程部署和创建流程实例的接口, 方便测试

/**
 * 部署流程
 * @param bpmnXmlFile xml定义文件路径
 * @param bmpnImageFile bpmn图片文件路径
 * @return 处理结果
 */
@PostMapping("/deploy")
@ApiOperation(value = "部署流程")
@ApiImplicitParams({
        @ApiImplicitParam(name = "bpmnXmlFile", value = "xml定义文件路径", defaultValue = "leave.bpmn20.xml"),
        @ApiImplicitParam(name = "bmpnImageFile", value = "bpmn图片文件路径", defaultValue = "leave.png")
})
public ResponseModel deploy(@RequestHeader("bpmnXmlFile") String bpmnXmlFile,
                                    @RequestHeader("bmpnImageFile") String bmpnImageFile) {
    String id = activitiProcessService.deploy(bpmnXmlFile, bmpnImageFile);
    return ResponseModel.success(id);
}

/**
 * 创建流程实例。
 *
 * @param processDefinitionId 部署ID
 * @return 流程定义ID
 */
@PostMapping("/v1/create-process-instance")
@ApiOperation(value = "创建流程实例")
@ApiImplicitParams({
        @ApiImplicitParam(name = "processDefinitionId", value = "流程定义ID", defaultValue = "requestleave:9:22505"),
        @ApiImplicitParam(name = "applyAssignee", value = "申请人", defaultValue = "zhangsan"),
        @ApiImplicitParam(name = "approvalAssignee", value = "审批人", defaultValue = "lisi")
})
public ResponseModel createProcessInstance(@RequestHeader("processDefinitionId") String processDefinitionId,
                                                   @RequestHeader("applyAssignee") String applyAssignee,
                                                   @RequestHeader("approvalAssignee") String approvalAssignee) {
    String processInstanceId =  activitiProcessService.createProcessInstance(processDefinitionId,
            applyAssignee, approvalAssignee);
    return ResponseModel.success(processInstanceId);
}

一般来说只要流程定义不修改,部署一次就可以了,部署后拿到流程定义ID。这个流程定义ID,在创建流程实例时作为入参。每个人提交一次请假申请都需要生成一份相应的流程实例,创建流程实例时可以指定各节点的处理责任人,也可以在创建后修改责任人。

六、实现流程部署和创建实例逻辑

/**
 * 流程服务类
 */
@Component
public class ActivitiProcessService {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程。
     *
     * @param bpmnXmlFile
     * @param bmpnImageFile
     */
    public String deploy(String bpmnXmlFile, String bmpnImageFile) {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource(bpmnXmlFile)
                .addClasspathResource(bmpnImageFile)
                .deploy();

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();

        return processDefinition.getId();
    }

    /**
     * 启动流程实例,返回流程实例ID。
     *
     * @param processDefinitionId 流程定义ID
     * @return 流程实例ID
     */
    public String createProcessInstance(String processDefinitionId, String applyAssignee, String approvalAssignee) {
        Map paramMap = new HashMap<>();
        paramMap.put("apply_id_assignee", applyAssignee);
        paramMap.put("approval_id_assignee", approvalAssignee);
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, paramMap);
        return processInstance.getProcessInstanceId();
    }
}

源码GitHub路径: https://github.com/ylforever/elon-activiti

你可能感兴趣的:(流程编排,Java开发技术,Activiti,流程编排,Java)