springboot activiti工作流简单示例

最近一直研究springboot,根据工作需求,工作流需要作为一个单独的微服务工程来提供给其他服务调用,现在简单的写下工作流(使用的activiti)微服务的搭建与简单使用

jdk:1.8

数据库:mysql  5.7

IDE:eclipse

springboot:1.5.8

activiti:6.0.0

1.新建空白的maven微服务架构

新建maven项目的流程不在阐述,这里添加上activiti、myslq连接的依赖,只贴主要代码
pox.xml


  4.0.0
  com.xxx
  xxx
  0.0.1-SNAPSHOT
   
        UTF-8
        1.8
    

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.8.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.activiti
            activiti-spring-boot-starter-basic
            6.0.0
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
确认服务是可用

2.连接服务名、服务端口、数据库配置

在resources目录下的application.properties(项目定位原因没有使用yaml,可根据自己项目使用)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activity?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
server.port=8081
server.context-path=/activity
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
确认配置的数据库可用

3.main

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Guo on 2017/11/15.
 */
@SpringBootApplication
public class ActivityApp
{
    public static void main(String[] args)
    {
        SpringApplication.run(ActivityApp.class, args);
    }
}

4.service及实现

service:
import org.activiti.engine.task.TaskQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/activityService")
public interface ActivityConsumerService {
	/**
	 * 流程demo
	 * @return
	 */
	@RequestMapping(value="/startActivityDemo",method=RequestMethod.GET)
    public boolean startActivityDemo();
	
}
impl
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongguaninfo.activity.service.ActivityConsumerService;
@Service("activityService")
public class ActivityConsumerServiceImpl implements ActivityConsumerService {

	@Autowired
    private RuntimeService runtimeService;
	@Autowired
    private TaskService taskService;
	
	@Override
	public boolean startActivityDemo() {
		System.out.println("method startActivityDemo begin....");
        Map map = new HashMap();
        map.put("apply","zhangsan");
        map.put("approve","lisi");
//流程启动
        ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave",map);
        String processId = pi1.getId();
        String taskId = pi1.getTasks().get(0).getId();
        taskService.complete(taskId, map);//完成第一步申请
        
        Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        String taskId2 = task.getId();
        map.put("pass", false);
        taskService.complete(taskId2, map);//驳回申请
        System.out.println("method startActivityDemo end....");
        return false;
	}
}

5.bpmn

在resources目录下新建文件夹:processes,并在processes创建一个新的bpmn文件,如图
springboot activiti工作流简单示例_第1张图片
文件内容:


  
    
    
    
    
    
    
      
    
    
    
    
    
    
      
    
    
  
  
    
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
        
      
      
        
        
      
    
  
需要认知的问题:.项目启动的时候,activiti会自动在mysql中创建activiti相关表,不用像oracle那样需要手动去创建

6.验证

启动项目前,连接数据库,查看需要连接数据库中没有表,启动项目完成后,刷新数据库,activiti已经创建相关表,打开act_re_procdef表,流程数据已经存在,即流程已经部署成功。
用浏览器访问地址:http://127.0.0.1:8081/activity/activityService/startActivityDemo
打印了预计的日志,没有报错信息,查看数据库中的act_ru_task表,发现刚才执行形成的数据,项目成功。
PS:只是简单的微服务,没有去写注册服务、网关配置、熔断机制等等,仅用于activiti与springboot的结合
=========================后续==========================
1.在项目单独作为一个引擎,本身不部署流程的时候,如果resources目录没有“processes”目录,启动项目报错--找不到processes目录。需要在配置文件中添加一下内容:
spring: 
  activiti: 
####校验流程文件,默认校验resources下的processes文件夹里的流程文件
    check-process-definitions: false
即启动项目,不去校验processes。


你可能感兴趣的:(springboot activiti工作流简单示例)