中文文档:https://tkjohn.github.io/flowable-userguide/
汉化地址:https://www.icode9.com/content-4-1237749.html
demo地址:https://gitee.com/qyh24/springboot-flowable.git
一、定义、部署流程
1.引入Flowable6.3
<!--引入Flowable6.3依赖-->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.3.0</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
如果项目中引入了mybatisPlus,引入Flowable时一定要排除mybais,如果不排除的话,会引起以下冲突
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.getLanguageDriver(MybatisMapperAnnotationBuilder.java:385)
The following method did not exist:
org.apache.ibatis.session.Configuration.getLanguageDriver(Ljava/lang/Class;)Lorg/apache/ibatis/scripting/LanguageDriver;
The method's class, org.apache.ibatis.session.Configuration, is available from the following locations:
jar:file:/Users/qiaoyuhang/.m2/repository/org/mybatis/mybatis/3.4.5/mybatis-3.4.5.jar!/org/apache/ibatis/session/Configuration.class
It was loaded from the following location:
file:/Users/qiaoyuhang/.m2/repository/org/mybatis/mybatis/3.4.5/mybatis-3.4.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.apache.ibatis.session.Configuration
Process finished with exit code 1
2.配置文件
Flowable默认使用h2数据源,可以更改,我用的是mysql
# mysql
spring:
datasource:
url: jdbc:mysql://*:3306/flowable?useUnicode=true&characterEncoding=utf8
username: *
password: *
driver-class-name: com.mysql.jdbc.Driver
@Configuration
@PropertySource("classpath:application-dev.yml")
public class FlowableConfig {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driver;
@Bean
public ProcessEngine getProcessEngine(){
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl(url)
.setJdbcUsername(username)
.setJdbcPassword(password)
.setJdbcDriver(driver)
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = cfg.buildProcessEngine();
return processEngine;
}
}
3.设计一个流程
在设计器中创建一个流程
4.部署流程定义
@RestController
@RequestMapping("/flowable")
@Slf4j
public class FlowableController {
@Autowired
private ProcessEngine processEngine;
/**
* 部署流程定义
* @return
*/
@RequestMapping("/def")
public Deployment getDeployment(){
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("file/请假.bpmn20.xml")
.deploy();
log.info("部署--{}--流程成功","请假");
return deployment;
}
}
部署成功后,查看ACT_RE_PROCDEF是否成功插入数据
5.查询流程定义
/**
* 查询流程定义
* @return
*/
@RequestMapping("/query")
public void getProcessDefinition(){
RepositoryService repositoryService = processEngine.getRepositoryService();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
log.info("Found process definition : " + list.toString());
}
二、启动流程,查看任务
1.启动流程实例
/**
* 启动流程实例
* @return
*/
@RequestMapping("/start")
public void ProcessInstance(){
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("employee", "申请人");
variables.put("nrOfHolidays", "3");
variables.put("description", "请假3天");
ProcessInstance processInstance =
runtimeService.startProcessInstanceByKey("leave_key", variables);//key为xml中processId
log.info("成功启动流程{}" ,"leave_key");
}
查看ACT_RU_EXECUTION(执行表)
ACT_RU_TASK(正在进行的任务表)
ACT_RU_VARIABLE(变量表)
ACT_HI_ACTINST(历史节点表)
2.查看任务
/**
* 查看任务表
* @return
*/
@RequestMapping("/getTask")
public void getTaskService(){
TaskService taskService = processEngine.getTaskService();
List<Task> tasks = taskService.createTaskQuery().taskAssignee("张三").list();
System.out.println("You have " + tasks.size() + " tasks:");
for (int i=0; i<tasks.size(); i++) {
System.out.println((i+1) + ") " + tasks.get(i).getName());
}
//查看第一个任务,并且输出变量
Map<String, Object> processVariables = taskService.getVariables(tasks.get(0).getId());
System.out.println(processVariables.get("employee") + " wants " +
processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?");
}
}
3.提交任务
/**
* 完成任务
* @return
*/
@RequestMapping("/submit")
public void submit(){
TaskService taskService = processEngine.getTaskService();
List<Task> tasks = taskService.createTaskQuery().taskAssignee("张三").list();
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("approved", true);
taskService.complete(tasks.get(0).getId(), variables);
}
4.查看历史任务
/**
* 根据任务分配人查看历史任务
*/
@RequestMapping("/history")
public void history(){
HistoryService historyService = processEngine.getHistoryService();
String name = "张三";
List<HistoricActivityInstance> activities =
historyService.createHistoricActivityInstanceQuery()
.taskAssignee(name)
.finished()
.orderByHistoricActivityInstanceEndTime().asc()
.list();
activities.forEach(a->{
Date endTime = a.getEndTime();
String date = DateUtil.format(endTime,"yyyy-MM-dd hh:mm:ss");
System.out.println("任务ID:" + a.getId() + "完成于:" + date);
});
}
三、Tomcat部署Flowable设计器
1.下载Flowable
[https://github.com/flowable/flowable-engine/releases/download/flowable-6.4.0/flowable-6.4.0.zip](https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fflowable%2Fflowable-engine%2Freleases%2Fdownload%2Fflowable-6.4.0%2Fflowable-6.4.0.zip)
2.放入tomcat
把Flowable中5个war包放入tomcat的webapps下
3.启动完成即可登录
http://localhost:8080/flowable-modeler :
4.踩坑
网上教程大部分是本地部署,我是部署在自己的腾讯云服务器,访问地址需要进行修改
flowable-modeler/WEB-INF/classes中修改application.properties