SpringBoot整合Activiti案例

1.Spring整合Activiti
   步骤一:导入依赖
    <!--activiti常规依赖-->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-engine</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-bpmn-model</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-bpmn-converter</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-json-converter</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-bpmn-layout</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <dependency>
      <groupId>org.activiti.cloud</groupId>
      <artifactId>activiti-cloud-services-api</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <!--activiti和Spring整合依赖-->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-spring</artifactId>
      <version>7.0.0.Beta1</version>
    </dependency>
    <!--数据库依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

    <!--单测依赖-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>

    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>

    <!--数据源-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
   
   
   步骤二:配置Activiti
    配置内容:1.数据源         2.配置processEngine对象        3.配置XXXXService
    <!--数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
     <property name="url" value="jdbc:mysql://localhost:3306/activiti-y2170"/>
     <property name="username" value="root"/>
     <property name="password" value="root"/>
    </bean>
    <!--配置ProcessEngineConfiguration-->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
     <!--数据源-->
     <property name="dataSource" ref="dataSource"/>
     <!--配置事务-->
     <property name="transactionManager" ref="transactionManager"/>
     <!--数据生成策略    true   false   create   drop-create -->
     <property name="databaseSchemaUpdate" value="true"/>
    </bean>
    <!--配置ProcessEngine-->
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
     <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    </bean>
    <!--配置RepositoryService-->
    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
    <!--配置RuntimeService-->
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
    <!--配置TaskService-->
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
    <!--配置HistoryService-->
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
    
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
    </bean>
    
   
   
   步骤三:测试Activiti
    @RunWith(SpringJUnit4ClassRunner.class)     //测试方式  junit4.12+
    @ContextConfiguration("classpath:activiti-spring.xml")   //配置
    public class ActivitiSpringTest {
     @Resource
     private RepositoryService repositoryService;
     @Resource
     private RuntimeService runtimeService;

     /**
      * 测试流程部署
      */
     @Test
     public void deployment(){
      repositoryService.createDeployment()
        .addClasspathResource("amedical/medical.bpmn")
        .name("测试Spring流程部署")
        .deploy();
     }
    }
    
  
  
  
  
  
  
  
  2.Activiti7的新特性
   ProcessRuntime
   TaskRuntaime
  
  
  
  
  
  
  3.SpringBoot2整合Activiti7
   步骤一:引入依赖
    <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.1.4.RELEASE</version>
    </parent>
    <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-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter -->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-spring-boot-starter</artifactId>
      <version>7.0.0.Beta2</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>
   
   步骤二:配置文件
    spring:
      datasource:
     url: jdbc:mysql://localhost:3306/activiti-y2170?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
     username : root
     password : root
     driver-class-name: com.mysql.jdbc.Driver
      activiti:
     db-history-used: true
     
   步骤三:将SpringSecurity配置添加到项目当中
    SecurityUtil.java 关于权限登陆
    DemoApplicationConfiguration 关于权限配置
   
   步骤四:启动工程
    @SpringBootApplication
    public class StartActiviti {
     public static void main(String[] args) {
      SpringApplication.run(StartActiviti.class,args);
     }
    }
   注意问题:
    1.Activiti7和SpringSecurity耦合,需要加入SpringSecurity的依赖和配置,我们可以使用Security中的用户角色组定义流程执行的组
    2.流程默认可自动部署,但是需要再resources/processes文件夹,将流程文件放入当中
    3.默认历史表不会生成,需要手动开启配置
  4.执行流程
   @RestController
   @RequestMapping("/activiti")
   public class ActivitiController {
    @Resource
    private ProcessRuntime processRuntime;
    @Resource
    private TaskRuntime taskRuntime;
    @Resource
    private SecurityUtil securityUtil;
    /**
     * 查询流程定义
     */
    @RequestMapping("/getProcess")
    public void getProcess(){
     //查询所有流程定义信息
     Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
     System.out.println("当前流程定义的数量:"+processDefinitionPage.getTotalItems());
     //获取流程信息
     for (ProcessDefinition processDefinition:processDefinitionPage.getContent()) {
      System.out.println("流程定义信息"+processDefinition);
     }
    }
    /**
     * 启动流程示例
     */
    @RequestMapping("/startInstance")
    public void startInstance(){
     ProcessInstance instance = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("demo").build());
     System.out.println(instance.getId());
    }
    /**
     * 获取任务,拾取任务,并且执行
     */
    @RequestMapping("/getTask")
    public void getTask(){
     securityUtil.logInAs("salaboy");        //指定组内任务人
     Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
     if(tasks.getTotalItems()>0){
      for (Task task:tasks.getContent()) {
       System.out.println("任务名称:"+task.getName());
       //拾取任务
       taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
       //执行任务
       taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
      }
     }
    }
   }
  
posted @ 2020-04-09 13:09  林染plus  阅读(1570)  评论(0编辑  收藏  举报