<dependencies>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-spring-boot-starter-basicartifactId>
<version>6.0.0.RC1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
在应用市场下载actiBPM插件,用来绘制流程图。(若在idea的应用市场搜不到,可以到idea应用市场官网下载)
@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();
}
}
@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());
}
这里的部署方式分为三种,我这里只选择了其中一种,想要了解所有的部署方式请点击这里
部署完成后的效果
@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());
}
@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());
}
此时的流程图进度如下:
这时已经发起审批,等待领导批准。。。
@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节点。
@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-同意,若不同意则进入发起审批节点,重新走流程;若同意,则结束流程。。。
@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);
}