怎样在springboot项目集成flowable工作流,guns集成flowable工作流

1.添加依赖


    org.flowable
    flowable-engine
    6.4.2

2.使用Test类创建23张表,数据库驱动根据自己使用的数据库选择,在创建表的时候如果数据库已经存在flowable的表需要在链接后面加上nullCatalogMeansCurrent=true

/**使用代码创建工作流需要的23张表*/
@Test
public void createTable() {
    ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
            .createStandaloneProcessEngineConfiguration();
    //连接数据库的配置
    //配置数据库驱动:对应不同数据库类型的驱动
    processEngineConfiguration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
    //配置数据库的JDBC URL
    processEngineConfiguration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true");
    //配置连接数据库的用户名
    processEngineConfiguration.setJdbcUsername("root");
    //配置连接数据库的密码
    processEngineConfiguration.setJdbcPassword("1qaz!QAZ");
    /**
     public static final String DB_SCHEMA_UPDATE_FALSE = "false";不能自动创建表,需要表存在
     public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先删除表再创建表
     public static final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在,自动创建表
     */
    processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    //工作流的核心对象,ProcessEnginee对象
    ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
    System.out.println("processEngine:"+processEngine);
}

3.使用Test类进行流程图的发布和查看,流程图放在resources可以识别

流程图https://download.csdn.net/download/qq_19897551/11878422

import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * 工作流的业务测试
 *
 * @author ...
 * @date 2017-12-02 20:37
 */
public class FlowableTest {

    ProcessEngine processEngine;

    @Before
    public void init() {
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8")
                .setJdbcUsername("root")
                .setJdbcPassword("1qaz!QAZ")
                .setJdbcDriver("com.mysql.jdbc.Driver");
                //.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        processEngine = cfg.buildProcessEngine();
    }

    /**
     * 发布流程
     */
    @Test
    public void deploy() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("ExpenseProcess.bpmn20.xml")
                .deploy();

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
        System.out.println("Found process definition : " + processDefinition.getName());
    }

    /**
     * 查看发布
     */
    @Test
    public void findDeploy() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List list = repositoryService.createDeploymentQuery().list();
        for (Deployment deployment : list) {
            System.out.println(deployment.getCategory());
            System.out.println(deployment.getName());
            System.out.println("deploy = " + deployment.getId());
            System.out.println("key = " + deployment.getKey());
        }
    }


}

4.在项目中加载flowable,并加载对应的数据库,

初始化的时候报错:version mismatch: library version is '6.5.0.1', db version is 6.2.0.0 Hint

需要将初始化表的版本号改为一致

怎样在springboot项目集成flowable工作流,guns集成flowable工作流_第1张图片

import cn.stylefeng.roses.core.config.properties.DruidProperties;
import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlowAbleContext {

    private DruidProperties druidProperties;

    public FlowAbleContext(DruidProperties druidProperties) {
        this.druidProperties = druidProperties;
    }

    @Autowired
    ProcessEngine processEngine;



    @Bean
    public ProcessEngine processEngine() {
        ProcessEngineConfiguration engineConfiguration = new StandaloneProcessEngineConfiguration();
        engineConfiguration.setActivityFontName("宋体");
        engineConfiguration.setLabelFontName("宋体");
        engineConfiguration.setAnnotationFontName("宋体");
        engineConfiguration.setXmlEncoding("utf-8");
        engineConfiguration.setDatabaseType("mysql");
        //engineConfiguration.setDataSource(dataSource);
        engineConfiguration.setJdbcDriver(druidProperties.getDriverClassName());
        engineConfiguration.setJdbcUrl(druidProperties.getUrl());
        engineConfiguration.setJdbcUsername(druidProperties.getUsername());
        engineConfiguration.setJdbcPassword(druidProperties.getPassword());

        //唯一主键策略
        //engineConfiguration.setIdGenerator(new FlowStrongUuidGenerator());
        //设置外部管理的事务
       // engineConfiguration.setTransactionsExternallyManaged(true);
        //engineConfiguration.setTransactionFactory(new NutzTransactionFactory());

        return engineConfiguration.buildProcessEngine();
    }

    @Bean
    public RepositoryService repositoryService() {
        return processEngine.getRepositoryService();
    }
    @Bean
    public RuntimeService runtimeService() {
        return processEngine.getRuntimeService();
    }
    @Bean
    public IdentityService identityService() {
        return processEngine.getIdentityService();
    }

    @Bean
    public TaskService taskService() {
        return processEngine.getTaskService();
    }
    @Bean
    public HistoryService historyService() {
        return processEngine.getHistoryService();
    }
    public ManagementService managementService() {
        return processEngine.getManagementService();
    }
    public FormService formService() {
        return processEngine.getFormService();
    }
    public DynamicBpmnService dynamicBpmnService() {
        return processEngine.getDynamicBpmnService();
    }


}

5.在实现类中调用flowable,先引入flowable实现类,实现一个提交,审核通过,审核驳回,查看流程图的方法

@Autowired
private RuntimeService runtimeService;

@Autowired
private TaskService taskService;

@Autowired
private RepositoryService repositoryService;

@Autowired
private ProcessEngine processEngine;
//启动flowable流程
@Override
public void add(SysExpenseParam param) {
    SysExpense entity = getEntity(param);
    //保存业务数据
    entity.setCreateUser(ShiroKit.getUser().getId());
    entity.setState(ExpenseState.SUBMITING.getCode());

    //启动流程
    HashMap map = new HashMap<>();
    map.put("taskUser", ShiroKit.getUser().getName());
    map.put("money", entity.getMoney());
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Test", map);
    entity.setProcessId(processInstance.getId());
    this.save(entity);
}
//通过审核流程
@Override
@Transactional(rollbackFor = Exception.class)
public void pass(String taskId) {
    //使用任务ID,查询任务对象,获取流程流程实例ID
    Task task = taskService.createTaskQuery()//
            .processInstanceId(taskId)
            .singleResult();

    //通过审核
    HashMap map = new HashMap<>();
    map.put("outcome", "1");
    taskService.complete(task.getId(), map);

    //判断流程是否结束,结束之后修改状态
    ProcessInstance pi = runtimeService.createProcessInstanceQuery()//
            .processInstanceId(task.getProcessInstanceId())//使用流程实例ID查询
            .singleResult();
    QueryWrapper wrapper = new QueryWrapper().eq("PROCESS_ID", task.getProcessInstanceId());
    SysExpense expense = this.getOne(wrapper);

    //审核通过修改为通过状态
    if (pi == null) {
        expense.setState(ExpenseState.PASS.getCode());
        this.updateById(expense);
    } else {
        //审核通过如果还有记录则为经理或boss审核中
        expense.setState(ExpenseState.CHECKING.getCode());
        this.updateById(expense);
    }
}
//驳回审核流程
@Override
@Transactional(rollbackFor = Exception.class)
public void unPass(String taskId) {
    //使用任务ID,查询任务对象,获取流程流程实例ID
    Task task = taskService.createTaskQuery()//
            .processInstanceId(taskId)
            .singleResult();
    HashMap map = new HashMap<>();
    map.put("outcome", "0");
    taskService.complete(task.getId(), map);
    //修改审核状态为未通过
    QueryWrapper wrapper = new QueryWrapper().eq("PROCESS_ID", task.getProcessInstanceId());
    SysExpense expense = this.getOne(wrapper);
    expense.setState(ExpenseState.UN_PASS.getCode());
    this.updateById(expense);
}
//返回流程原图
@Override
public void printProcessImage(Long expenseId) throws IOException {
    SysExpense expense = this.baseMapper.selectById(expenseId);
    String processId = expense.getProcessId();
    ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();

    //流程走完的不显示图
    if (pi == null) {
        return;
    }

    Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();

    //使用流程实例ID,查询正在执行的执行对象表,返回流程实例对象
    String InstanceId = task.getProcessInstanceId();
    List executions = runtimeService
            .createExecutionQuery()
            .processInstanceId(InstanceId)
            .list();

    //得到正在执行的Activity的Id
    List activityIds = new ArrayList<>();
    List flows = new ArrayList<>();
    for (Execution exe : executions) {
        List ids = runtimeService.getActiveActivityIds(exe.getId());
        activityIds.addAll(ids);
    }

    //获取流程图
    BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
    ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
    ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
    InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, true);
    OutputStream out = null;
    byte[] buf = new byte[1024];
    int legth = 0;
    try {
        out = HttpKit.getResponse().getOutputStream();
        while ((legth = in.read(buf)) != -1) {
            out.write(buf, 0, legth);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

你可能感兴趣的:(flowable)