美文网首页工作流
springboot1.5.4和activiti6.0整合(一)

springboot1.5.4和activiti6.0整合(一)

作者: 小龙哥Joeyyyyyyy | 来源:发表于2019-07-07 22:10 被阅读78次

            上周项目经理交给新的任务,在现在的工作基础上整合工作流引擎,技术选型选的是activiti6.0,于是我在网上就找各种资料整合,大部分的是先在pom.xml文件内添加依赖:

    <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-spring-boot-starter-basic</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-spring</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-engine</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>de.odysseus.juel</groupId>
                <artifactId>juel-api</artifactId>
                <version>2.2.7</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-process-validation</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-form-api</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-dmn-api</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.1</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-image-generator</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-bpmn-model</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-bpmn-converter</artifactId>
                <version>6.0.0</version>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>de.odysseus.juel</groupId>
                <artifactId>juel-impl</artifactId>
                <version>2.2.7</version>
    

    再在application.yml文件中增加如下配置:

    spring:
      activiti:
        database-schema-update: true
        history-level: full
        process-definition-location-prefix: classpath:/processes/
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/demo_workflow?useUnicode=true&characterEncoding=UTF-8
        username: root
        password: root
        driverClassName: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
    

    然后再用activiti.cfg.xml获取processEngine,配置如下:

    <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: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-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        <!--这里的类太多别导错了 -->
        <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
            <!-- 配置流程引擎配置对象 -->
            <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/demo_workflow?useUnicode=true&amp;characterEncoding=UTF-8"></property>
            <property name="jdbcUsername" value="root"></property>
            <property name="jdbcPassword" value="root"></property>
            <!-- 注入数据源信息 -->
            <property name="databaseSchemaUpdate" value="true"></property>
        </bean>
    
        <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
            <!-- 注入自动建表设置 -->
            <property name="processEngineConfiguration" ref="processEngineConfiguration"></property>
        </bean>
    </beans>
    

    然后在启动类里添加这个注解

    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class})

    还需要在resources下新建目录processes文件夹,里边需要新建流程文件,流程文件为.bomn文件,然后将它复制一份改名为xml,注意xml文件的结尾需为bpmn.xml或者bpmn20.xml不然会找不到相关流程配置文件。
    下面贴上简单的业务流程图:


    业务流程图

            接下来需要在项目里的controller里写一些流程代码来部署,启动,流转流程。

    下边贴上部分代码:

    @RestController
    @RequestMapping(value = "/activitiService")
    public class DemoController {
        private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
        /**
         * 流程定义和部署相关的存储服务
         */
        @Autowired
        private RepositoryService repositoryService;
    
        /**
         * 流程运行时相关的服务
         */
        @Autowired
        private RuntimeService runtimeService;
    
        /**
         * 节点任务相关操作接口
         */
        @Autowired
        private TaskService taskService;
    
        /**
         * 历史记录相关服务接口
         */
        @Autowired
        private HistoryService historyService;
    
        @RequestMapping(value = "/start")
        public String start() {
            Deployment deployment = repositoryService.createDeployment()//创建一个部署对象
                    .name("离司流程")
                    .addClasspathResource("processes/salemanResignDemo.bpmn")
                    .addClasspathResource("diagrams/salemanResignDemo.png")
                    .deploy();
            logger.info("部署ID:" + deployment.getId());
            logger.info("部署名称:" + deployment.getName());
    //      //查询流程定义
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
            //在流程模板中定义的流程ID
            String instanceKey = processDefinition.getId();
            logger.info("开启离司流程...");
            //设置流程参数,开启流程
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("salemanNo", "110234");
            map.put("resignReason", "个人原因");
            //使用流程定义的key启动流程实例,key对应salemanResignDemo.bpmn文件中id的属性值,使用key值启动,默认是按照最新版本的流程定义启动
            ProcessInstance instance = runtimeService.startProcessInstanceById(instanceKey, map);
            logger.info("启动流程实例成功:{}", instance);
            logger.info("流程实例ID:{}", instance.getId());
            logger.info("流程定义ID:{}", instance.getProcessDefinitionId());
            //验证是否启动成功
            //通过查询正在运行的流程实例来判断
            ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
            //根据流程实例ID来查询
            List<ProcessInstance> runningList = processInstanceQuery.processInstanceId(instance.getProcessInstanceId()).list();
            logger.info("根据流程ID查询条数:{}", runningList.size());
    
            // 返回流程ID
            return instance.getId();
    
        }
    
    
        @RequestMapping(value = "/applyResign")
        public String applyResign(@RequestParam String taskId, String salemanNo, String resignReason) {
            /*
             * 获取请求参数
             */
            // String taskId;
            List<Task> list = taskService.createTaskQuery().taskAssignee("xiaoli").list();
    
            /*
             *  查询任务
             */
            if (list != null && list.size() > 0) {
                for (Task task : list) {
                    logger.info("任务ID:" + task.getId());
                    logger.info("任务名称:" + task.getName());
                    logger.info("任务的创建时间:" + task.getCreateTime());
                    logger.info("任务的办理人:" + task.getAssignee());
                    logger.info("流程实例ID:" + task.getProcessInstanceId());
                    logger.info("执行对象ID:" + task.getExecutionId());
                    logger.info("流程定义ID:" + task.getProcessDefinitionId());
                    taskId = task.getId();
                }
    
            } else {
                logger.info("任务ID:{}查询到任务为空!");
                return "fail";
            }
            /*
             * 参数传递并提交申请
             */
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("salemanNo", salemanNo);
            map.put("resignReason", resignReason);
            taskService.complete(taskId, map);
            logger.info("执行【员工离司申请】环节,流程推动到【主管审核】环节");
    
            /*
             * 返回同意
             */
            return "success";
    
        }
    

    关于数据库表的说明,我在网上看到一张图,我就直接拿来用吧,事先声明这不是原创的图片:


    来源于网络

    可以看看,哪里有问题欢迎批评指正,如有遗漏,及时联系我。

    相关文章

      网友评论

        本文标题:springboot1.5.4和activiti6.0整合(一)

        本文链接:https://www.haomeiwen.com/subject/sokchctx.html