Springboot整合activiti 6.0
下载源码请点击这里
一 导入依赖
<dependencies>
<!--Activiti工作流 start-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0.RC1</version>
</dependency>
<!--Activiti工作流 end-->
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--jpa end-->
<!--thymeleaf模版 start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--thymeleaf模版 end-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二 绘制流程图插件 actiBPM
在应用市场下载actiBPM插件,用来绘制流程图。(若在idea的应用市场搜不到,可以到idea应用市场官网下载)
三 有了这个插件就可以创建bpmn文件,进行流程图的绘制
关于流程图的绘制这里就不做详解了,想了解的朋友可以点击这里
这里我画了一个请假流程图,作为案例示范
四 代码的编写
1 通过代码生成28张数据库表
@Test
public void init(){
try {
System.out.println("开始建表。。。");
//创建引擎配置对象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
//创建流程引擎对象
//目标生成数据库表
configuration.setJdbcUrl("jdbc:mysql://120.0.0.1:3306/acti");
configuration.setJdbcDriver("com.mysql.jdbc.Driver");
configuration.setJdbcUsername("root");
configuration.setJdbcPassword("root");
//设置表的生成策略
/**
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";如果表不存在,自动创建表
*/
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = configuration.buildProcessEngine();
System.out.println(processEngine.getName() + "***********");
System.out.println("建表结束。。。");
}catch (Exception e){
System.out.println("建表失败!!!");
e.printStackTrace();
}
}
结果如下:
关于各个表的作用及其详细介绍,请点击这里!!!(超详细)
2 进行流程部署
@Autowired
private ProcessEngine processEngine;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private IdentityService identityService;
@Autowired
private HistoryService historyService;
@Autowired
private RepositoryService repositoryService;
@RequestMapping("deploy")
public void deploy() {
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.name("第一次请假")
.addClasspathResource("processes/actiTest1.bpmn")
.addClasspathResource("processes/actiTest1.png")
.deploy();
System.out.println("部署ID:" + deployment.getId());//1
System.out.println("部署时间:" + deployment.getDeploymentTime());
}
这里的部署方式分为三种,我这里只选择了其中一种,想要了解所有的部署方式请点击这里
部署完成后的效果
3 启动流程
@RequestMapping("start")
public void start() {
//这里根据流程实例id进行启动,也可以用key来启动
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceById("myProcess_1:8:87504");
System.out.println("流程实例ID:" + processInstance.getId());// 流程实例ID:
System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());
}
4 进行流程第一步,发起审批
@RequestMapping("submit")
public void submit() {
Task task = taskService.createTaskQuery()
.processInstanceId("90001")
.singleResult();
HashMap<String, Object> map = new HashMap<>();
map.put("day", 4);
taskService.complete(task.getId(), map);
System.out.println(task.getName());
System.out.println(task.getId());
}
这里的请假天数day,我设置为4
此时的流程图进度如下:
这时已经发起审批,等待领导批准。。。
5 进行领导审批
@RequestMapping("leader1")
public void leader1() {
Task task = taskService.createTaskQuery()
.processInstanceId("90001")
.singleResult();
HashMap<String, Object> map = new HashMap<>();
map.put("result", 1);
taskService.complete(task.getId(), map);
System.out.println(task.getName());
System.out.println(task.getId());
}
我这里设定的变量为result,result=0,表示不同意,回到发起审批节点,重新走流程,result=1,表示同意,进入下一个流程。
这里我设置result=1,进入下一个流程:
领导审批同意,会根据假期天数进入不同的经理审批节点,这里的假期天数为4,所以进入了经理2节点。
6 经理审批
@RequestMapping("leader2")
public void leader2() {
Task task = taskService.createTaskQuery()
.processInstanceId("90001")
.singleResult();
HashMap<String, Object> map = new HashMap<>();
map.put("result2", 1);
taskService.complete(task.getId(), map);
System.out.println(task.getName());
System.out.println(task.getId());
}
这里的变量为result2,表示经理的审批结果,0-不同意,1-同意,若不同意则进入发起审批节点,重新走流程;若同意,则结束流程。。。
注意:若流程结束,则taskService.createTaskQuery()processInstanceId(“90001”)会获取不到数据,因为流程结束,所以这里会变成null,因此通常会用这种方法判断流程是否结束。
7 扩展
终止流程
@RequestMapping("delete")
public void delete() {
processEngine.getRuntimeService()
.deleteProcessInstance("90001", "终止流程");
System.out.println("终止流程");
}
流程图节点高亮显示
@RequestMapping("viewPng")
public void start(HttpServletResponse response) {
try {
InputStream is = getDiagram("90001");
if (is == null)
return;
response.setContentType("image/png");
BufferedImage image = ImageIO.read(is);
OutputStream out = response.getOutputStream();
ImageIO.write(image, "png", out);
is.close();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public InputStream getDiagram(String processInstanceId) {
//获得流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
String processDefinitionId = StringUtils.EMPTY;
if (processInstance == null) {
//查询已经结束的流程实例
HistoricProcessInstance processInstanceHistory =
historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
if (processInstanceHistory == null)
return null;
else
processDefinitionId = processInstanceHistory.getProcessDefinitionId();
} else {
processDefinitionId = processInstance.getProcessDefinitionId();
}
//使用宋体
String fontName = "宋体";
//获取BPMN模型对象
BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);
//获取流程实例当前的节点,需要高亮显示
List<String> currentActs = Collections.EMPTY_LIST;
if (processInstance != null)
currentActs = runtimeService.getActiveActivityIds(processInstance.getId());
return processEngine.getProcessEngineConfiguration()
.getProcessDiagramGenerator()
.generateDiagram(model, "png", currentActs, new ArrayList<String>(),
fontName, fontName, fontName, null, 1.0);
}
效果如下: