这篇主要介绍了activiti的流程运行控制服务——RuntimeService
RuntimeServie的主要功能:
1、启动流程及对流程数据的控制;
2、流程实例(ProcessInstance)与执行流(Execution)查询;
3、出发流程操作、接受信息和信号。
RuntimeService启动流程及变量管理:
1、启动流程常用方式(id,key,message):这里key是最常见的,每次部署流程时id会发生变化,但是key不会,message设置起来则相对更加复杂。
2、启动流程可选参数(businessKey,variables,tenantId):businessKey作为业务传来的唯一编码,比如订单号、物流单号;还可以通过variables传进上下文变量,map类型。tenantId是多用户的标志,确定流程运行的范围。
3、变量(variables)的设置和获取。
接下来我们开始测试:
/**
* Runtime服务器的测试
*/
public class RuntimeServiceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeServiceTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule(); //默认流程配置文件
/**
* 根据key启动流程
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testStartProcessByKey(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
Map variables = Maps.newHashMap(); //启动流程时传入一些参数
variables.put("key1","value1"); //添加一个参数为key1,值为value1
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", variables);//我们通过Key来启动流程
LOGGER.info("processInstance = [{}]",processInstance); //输出流程实例对象
}
}
其实每次对流程进行部署的时候,id与版本都会更新,但是我们上面根据key启动时,默认使用流程key对应最新的版本。
/**
* 根据流程ID启动流程
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testStartProcessById(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
ProcessDefinition processDefinition = activitiRule
.getRepositoryService().createProcessDefinitionQuery().singleResult(); //使用RepositoryService获取流程定义对象
Map variables = Maps.newHashMap(); //启动流程时传入一些参数
variables.put("key1","value1"); //添加一个参数为key1,值为value1
ProcessInstance processInstance = runtimeService
.startProcessInstanceById(processDefinition.getId(), variables);//我们通过流程Id来启动流程
LOGGER.info("processInstance = [{}]",processInstance); //输出流程实例对象
}
/**
* 根据builder来部署流程
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testProcessInstanceBuilder(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
Map variables = Maps.newHashMap(); //启动流程时传入一些参数
variables.put("key1","value1"); //添加一个参数为key1,值为value1
ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder(); //获取到流程实例建造者
ProcessInstance processInstance = processInstanceBuilder.businessKey("businessKey001")
.processDefinitionKey("my-process")
.variables(variables)
.start();//设置流程定义Key 变量 再将它启动
LOGGER.info("processInstance = [{}]",processInstance); //输出流程实例对象
}
以上三种方式启动效果一致,输出结果如下:
/**
* 根据变量测试的测试方法
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testVariables(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
Map variables = Maps.newHashMap(); //启动流程时传入一些参数
variables.put("key1","value1"); //添加一个参数为key1,值为value1
variables.put("key2","value2");
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey("my-process", variables); //我们通过Key来启动流程 前面的参数是 my-process.bpmn20.xml 里的id值
LOGGER.info("processInstance = [{}]",processInstance); //输出流程实例对象
runtimeService.setVariable(processInstance.getId(),"key3","value3"); //对参数进行修改,添加一个key3数据
runtimeService.setVariable(processInstance.getId(),"key2","value2_2"); //将key2键值修改为value2_2
Map variables1 = runtimeService.getVariables(processInstance.getId());//通过执行Id来获取参数
LOGGER.info("variables1 = [{}]",variables1); //可以获取实例对象中的参数
}
这里我们先设置两个参数,分别为key1、key2,值为value1、value2。然后通过Key启动流程,输出流程实例对象,再添加一个key3,值为value3的参数,修改key2的值为value2_2,最后通过执行ID获取参数并输出。
输出结果:
/**
* 根据流程实例来查询
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testProcessInstanceQuery(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
Map variables = Maps.newHashMap(); //启动流程时传入一些参数
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey("my-process", variables); //我们通过Key来启动流程 前面的参数是 my-process.bpmn20.xml 里的id值
LOGGER.info("processInstance = [{}]",processInstance); //输出流程实例对象
ProcessInstance processInstance1 = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstance.getId()).singleResult(); //processInstance1与processInstance 对象相同
LOGGER.info("processInstance1 = [{}]",processInstance1);
}
输出结果:
/**
* 根据流程实例对象来查询
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testExecutionQuery(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
Map variables = Maps.newHashMap(); //启动流程时传入一些参数
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey("my-process", variables); //我们通过Key来启动流程 前面的参数是 my-process.bpmn20.xml 里的id值
LOGGER.info("processInstance = [{}]",processInstance); //输出流程实例对象
List executionList = runtimeService.createExecutionQuery()
.listPage(0,100); //获取执行流
for (Execution execution:executionList){
LOGGER.info("execution = [{}]",execution);
}
}
输出结果:
可以看到查询了两条记录,ProcessInstance[4]对应着流程实例,Execution[ id '5' ] - activity 'someTask - parent '4' 对应着流程实例相关的流程实例对象