本章内容注册中心所需要代码可以参考https://blog.csdn.net/rishengcsdn/article/details/89956473
本章得应用有两个:
1.eureka 注册中心,端口:1112
2.activiTest ,流程引擎,端口:8089
代码下载地址:https://download.csdn.net/download/rishengcsdn/11195855
流程引擎代码如下:
pom.xml内容:
==================================================
==================================================
application.properties内容:
#spring-cloud
spring.application.name=avtiviTest
server.port=8089
feign.hystrix.enabled=true
eureka.instance.preferIpAddress=true
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka
ribbon.eureka.enabled=true
==================================================
applicationContext-activiti.xml内容:
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"
default-lazy-init="true">
==================================================
vacationRequest.bpmn20.xml流程定义文件内容:
Reason: ${managerMotivation}
==================================================
启动类:
package com.ecctest.activi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@ComponentScan(basePackages = "com.ecctest.activi")
@EnableDiscoveryClient
@SpringBootApplication
//@ImportResource({"classpath*:/spring/applicationContext-*.xml"})
@ImportResource({"classpath:/applicationContext-activiti.xml"})
public class ActiviApplication {
public static void main(String[] args) {
SpringApplication.run(ActiviApplication.class, args);
}
}
==================================================
Ctrl类:
package com.ecctest.activi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 无需登录验证的功能控制器
*/
@RestController
public class ActiviController {
@Autowired
private ActiviService activiService;
@RequestMapping(value = "/test/testFlow1", method = RequestMethod.GET)
public String testFlow1() throws Exception {
String bb1="ketmit";
String bb2="I'm really tired!";
String retMsg=activiService.doTest1(bb1,bb2);
//map.put("retMsg", retMsg);
//String retMsg="OK";
return retMsg;
}
@RequestMapping(value = "/test/testFlow2", method = RequestMethod.GET)
public String testFlow2() throws Exception {
String bb1="ketmit";
String bb2="I'm really tired!";
String retMsg=activiService.doTest2(bb1,bb2);
//String retMsg="OK";
return retMsg;
}
}
==================================================
activiti业务类:
package com.ecctest.activi;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ActiviService {
@Autowired
private RepositoryService repositoryService ;
@Autowired
private RuntimeService runtimeService ;
@Autowired
private TaskService taskService ;
public String doTest1(String bb1,String bb2) throws Exception{
//ProcessEngine processEngine=processEngineObj.getObject();
//RepositoryService repositoryService = processEngine.getRepositoryService();
// ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// RepositoryService repositoryService = processEngine.getRepositoryService();
// repositoryService.createDeployment()
// .addClasspathResource("org/activiti/test/testBMPN.xml")
// .deploy();
String filePath = "D:/workSpace/activiTest/src/main/resources/org/activiti/test/vacationRequest.bpmn20.xml";
FileInputStream fis = new FileInputStream(filePath);
repositoryService.createDeployment().addInputStream("userAndGroupInUserTask.bpmn", fis).deploy();// 用userAndGroupInUserTask.bpmn作为资源名称
System.out
.println("Number of process definitions: " + repositoryService.createProcessDefinitionQuery().count());
Map
variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");
//RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vacationRequest", variables);
// Verify that we started a new process instance
System.out.println("Number of process instances: " + runtimeService.createProcessInstanceQuery().count());
//TaskService taskService = processEngine.getTaskService();
//List
List
for (Task task : tasks) {
System.out.println("Task available: " + task.getName());
}
Task task = tasks.get(0);
Map
taskVariables.put("vacationApproved", "false");
taskVariables.put("managerMotivation", "We have a tight deadline!");
taskService.complete(task.getId(), taskVariables);
//职员Kermit重新申请
tasks = taskService.createTaskQuery().taskCandidateUser("Kermit").list();
task = tasks.get(0);
System.out.println("Task available: " + task.getName());
taskVariables = new HashMap
taskVariables.put("employeeName", "Kermit");
taskVariables.put("numberOfDays", new Integer(4));
taskVariables.put("vacationMotivation", "I'm really tired!");
taskVariables.put("resendRequest", "false");
taskService.complete(task.getId(), taskVariables);
//流程结束
return "流程1结束";
}
public String doTest2(String bb1,String bb2) throws Exception{
// ProcessEngine processEngine=processEngineObj.getObject();
//
// ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// RepositoryService repositoryService = processEngine.getRepositoryService();
// repositoryService.createDeployment()
// .addClasspathResource("org/activiti/test/testBMPN.xml")
// .deploy();
String filePath = "D:/workSpace/activiTest/src/main/resources/org/activiti/test/vacationRequest.bpmn20.xml";
FileInputStream fis = new FileInputStream(filePath);
repositoryService.createDeployment().addInputStream("userAndGroupInUserTask.bpmn", fis).deploy();// 用userAndGroupInUserTask.bpmn作为资源名称
System.out
.println("Number of process definitions: " + repositoryService.createProcessDefinitionQuery().count());
Map
variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");
//variables.put("busiServiceTaskImpl", new BusiServiceTaskImpl()); //spring cloud无需创建对象。会自动注入
//RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vacationRequest", variables);
// Verify that we started a new process instance
System.out.println("Number of process instances: " + runtimeService.createProcessInstanceQuery().count());
//TaskService taskService = processEngine.getTaskService();
//List
List
for (Task task : tasks) {
System.out.println("Task available: " + task.getName());
}
Task task = tasks.get(0);
Map
taskVariables.put("vacationApproved", "true");
taskVariables.put("managerMotivation", "I agree!");
taskService.complete(task.getId(), taskVariables);
//职员Kermit重新申请
tasks = taskService.createTaskQuery().taskCandidateUser("Kermit").list();
//流程结束
return "流程2结束";
}
}
==================================================
电子邮件服务类:
package com.ecctest.activi;
import java.io.Serializable;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* 服务节点执行的任务
*/
@Service(value="busiServiceTaskImpl")
public class BusiServiceTaskImpl implements Serializable,JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("variavles=" + execution.getVariables());
System.out.println("send Mail ============经理同意申请了!");
//sendMail();
}
}
==================================================
具体得流程图看这里:
================================================================
两个应用,启动完毕后,访问业务地址1:
http://localhost:8089/test/testFlow1
页面显示结果:
流程1结束
后台日志输出:
Number of process definitions: 5
Number of process instances: 1
Task available: 经理处理请假申请
Task available: 调整重新申请
访问业务地址2:
http://localhost:8089/test/testFlow2
页面显示结果:
流程2结束
后台日志输出:
Number of process definitions: 6
Number of process instances: 1
Task available: 经理处理请假申请
variavles={employeeName=Kermit, vacationMotivation=I'm really tired!, vacationApproved=true, managerMotivation=I agree!, numberOfDays=4}
send Mail ============经理同意申请了!
=================================================
流程1和流程2走向不一样。从业务流程图能看出有两个结束点,所以展示了两段代码来实现业务流程。
==================================================
另外是Activiti 5的参考资料:中文版:http://www.mossle.com/docs/activiti/index.html#activitiDesigner
英文版:https://www.activiti.org/5.x/userguide
Activiti 5的流程引擎图形编辑工具下载地址:https://github.com/Activiti/Activiti/releases/download/activiti-5.22.0/activiti-5.22.0.zip
下载后解压部署到Tomcat8的webapps下就可以。
访问地址:http://localhost:8080/activiti-explorer/
用户名和密码:
kermit
kermit
==================================================
图形化编辑工具并非必须的,似乎还有跟IDE集成的插件方案可以编辑BPMN的流程文件。中文文档上的例子并不正确,作者已经修改成正确的流程定义,可以通过流程引擎图形编辑工具导入,编辑完成后导出。然后部署到项目里面运行。
项目代码里面有两个MainStart.java和MainStart2.java如何不通过Spring引擎来调用activi,注意两种调用方式的区别。