最近学习了jbpm4.X的定时器事件,下面是一个简单的测试,上一篇简单介绍了jbpm4初始化数据库,这次可以从数据库中查看详细的流程执行信息。
流程定义文件:TimerEventTest.jpdl.xml
<?xml version="1.0" encoding="UTF-8"?>
<process name="TimerEventTest" xmlns="http://jbpm.org/4.4/jpdl">
<start g="44,111,48,48" name="start1">
<transition g="-50,-18" name="to guardedWait" to="guardedWait"/>
</start>
<state g="181,108,140,52" name="guardedWait">
<on event="timeout">
<timer duedate="1 minutes"/>
<event-listener class="com.lujinyong.timer.event.Escalate"/>
</on>
<transition g="-34,-20" name="go on" to="next step"/>
</state>
<state g="442,106,92,52" name="next step"/>
</process>
定时器到到期时间触发的事件:Escalate.java
package com.lujinyong.timer.event;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
/**
* 监听事件
*/
public class Escalate implements EventListener {
private static final long serialVersionUID = 1L;
public void notify(EventListenerExecution execution) {
//触发监听事件,会将变量插入流程变量表(jbpm4_variable)中
execution.setVariable("escalation", Boolean.TRUE);
String mag = "超时了,我要发提醒信息!";
execution.setVariable("mag", mag);
}
}
测试类TestTimerEvent2.java
package com.lujinyong.timer.event;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.job.Job;
import org.junit.Test;
public class TestTimerEvent2 {
/**
*
* @Description: 部署流程
* @Auther: lujinyong
* @Date: 2013-6-24 下午03:41:01
*/
@Test
public void testDeploy1() {
// 1.get processEngine
ProcessEngine processEngine = Configuration.getProcessEngine();
//如果不用图片可以将:.addResourceFromClasspath( "com/lujinyong/timer/event/TimerEventTest.png")去掉
processEngine
.getRepositoryService()
.createDeployment()
.addResourceFromClasspath(
"com/lujinyong/timer/event/TimerEventTest.jpdl.xml")
.addResourceFromClasspath(
"com/lujinyong/timer/event/TimerEventTest.png").deploy();
}
/**
*
* @Description: 测试发起流程,注意:名称发起流程后将发起的方法注释起来,在下面的测试中才不会多出发起的记录
* @Auther: lujinyong
* @Date: 2013-6-24 下午03:50:31
*/
@Test
public void testStart() {
ProcessEngine processEngine = Configuration.getProcessEngine();
// 根据流程定义id发起流程
// String processDefinitionId = "TimerTransitionTest-2";
// Execution processInstance = processEngine.getExecutionService()
// .startProcessInstanceById(processDefinitionId);
// 根据流程定义key发起流程
// String processDefinitionKey = "TimerEventTest";
// ExecutionService executionService = processEngine.getExecutionService();
// ManagementService managementService = processEngine.getManagementService();
// Execution processInstance = executionService.startProcessInstanceByKey(processDefinitionKey);
// Execution processInstance = executionService.createProcessInstanceQuery().processInstanceKey(processDefinitionKey).list().get(0);
// System.out.println("piID:" + processInstance.getId());
// System.out.println("pdKey:" + processInstance.getKey());
// System.out.println("pdid:" + processInstance.getProcessDefinitionId());
// System.out.println("pdState:" + processInstance.getState());
}
/**
*
* @Description: 测试超时的情况
* 如果流程没有在开始以后 10 分钟内执行 singal,
* 事件 timeout 就会被触发 事件监听器com.lujinyong.timer.event.Escalate 将被触发
* 即在流程变量表中会插入监听类的一条记录
* @Auther: lujinyong
* @Date: 2013-6-24 下午05:38:35
*/
@Test
public void testTimerEventTimerFires() {
ProcessEngine processEngine = Configuration.getProcessEngine();
ExecutionService executionService = processEngine.getExecutionService();
ManagementService managementService = processEngine.getManagementService();
ProcessInstance processInstance = executionService.startProcessInstanceByKey("TimerEventTest");
//查询定时器
Job job = managementService.createJobQuery()
.processInstanceId(processInstance.getId())
.uniqueResult();
//假设定时器被触发了,通过编码执行定时器进行模拟
managementService.executeJob(job.getId());
processInstance = executionService.findProcessInstanceById(processInstance.getId());
Set<String> expectedActivityNames = Collections.singleton("guardedWait");
assertEquals(expectedActivityNames, processInstance.findActiveActivityNames());
assertEquals(Boolean.TRUE, executionService.getVariable(processInstance.getId(), "escalation"));
System.out.println(">>>>>>>>>>>>>>>>>>"+executionService.getVariable(processInstance.getId(), "mag"));
}
/**
*
* @Description: 测试未超时的情况
* 如果 guardedWait 活动在 10 分钟内被结束,
* 然后定时器就会被取消, Escalate 事件监听器也不会执行
* 即流程变量表中不会插入监听类的记录
* @Auther: lujinyong
* @Date: 2013-6-24 下午05:39:45
*/
// @Test
// public void testTimerEventContinueBeforeTimerFires() {
// ProcessEngine processEngine = Configuration.getProcessEngine();
// ExecutionService executionService = processEngine.getExecutionService();
// ManagementService managementService = processEngine.getManagementService();
// ProcessInstance processInstance = executionService.startProcessInstanceByKey("TimerEventTest");
//
// String executionId = processInstance.findActiveExecutionIn("guardedWait").getId();
//
// executionService.signalExecutionById(executionId, "go on");
//
// processInstance = executionService.findProcessInstanceById(processInstance.getId());
//
// assertTrue(processInstance.isActive("next step"));
//
// List<Job> jobs = managementService.createJobQuery()
// .processInstanceId(processInstance.getId())
// .list();
//
// assertEquals(new ArrayList<Job>(), new ArrayList<Job>(jobs));
// }
}