init();
SimulationEvent event = removeSimulationEvent();
while (!simulationEnd(event)) {
executeEvent(event);
event = removeSimulationEvent();
}
close();
@Override
public void onEvent(ActivitiEvent event) {
Collection<SimulationEvent> simulationEvents = transform(event);
store(simulationEvents);
}
<process id="theSimplestProcess" name="Without task Process">
<documentation>This is a process for testing purposesdocumentation>
<startEvent id="theStart"/>
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theEnd"/>
<endEvent id="theEnd"/>
process>
流程发布,可以用于真实和模拟的运行:
// get process engine with record listener to log events
ProcessEngine processEngine = (new RecordableProcessEngineFactory(THE_SIMPLEST_PROCESS, listener))
.getObject();
// start process instance with variables
Map<String,Object> variables = new HashMap<String, Object>();
variables.put(TEST_VARIABLE, TEST_VALUE);
processEngine.getRuntimeService().startProcessInstanceByKey(SIMPLEST_PROCESS, BUSINESS_KEY,variables);
// check process engine status - there should be one process instance in the history
checkStatus(processEngine);
// close and destroy process engine
EventRecorderTestUtils.closeProcessEngine(processEngine, listener);
ProcessEngines.destroy();
在startProcessInstanceByKey方法调用后,记录ActivitiEventType.ENTITY_CREATED
final SimpleSimulationRun.Builder builder = new SimpleSimulationRun.Builder();
// init simulation run
// get process engine factory - the only difference from RecordableProcessEngineFactory that log listener is not added
DefaultSimulationProcessEngineFactory simulationProcessEngineFactory = new DefaultSimulationProcessEngineFactory(THE_SIMPLEST_PROCESS);
// configure simulation run
builder.processEngine(simulationProcessEngineFactory)
// set playback event calendar from recorded events
.eventCalendar(new PlaybackEventCalendarFactory(new SimulationEventComparator(), listener.getSimulationEvents()))
// set handlers for simulation events
.customEventHandlerMap(EventRecorderTestUtils.getHandlers());
SimpleSimulationRun simRun = builder.build();
simRun.execute(new NoExecutionVariableScope());
// check the status - the same method which was used in record events method
checkStatus(simulationProcessEngineFactory.getObject());
// close and destroy process engine
simRun.getProcessEngine().close();
ProcessEngines.destroy();
/**
* Allows to run simulation in debug mode
*/
public interface SimulationDebugger {
/**
* initialize simulation run
* @param execution - variable scope to transfer variables from and to simulation run
*/
void init(VariableScope execution);
/**
* step one simulation event forward
*/
void step();
/**
* continue in the simulation run
*/
void runContinue();
/**
* execute simulation run till simulationTime
*/
void runTo(long simulationTime);
/**
* execute simulation run till simulation event of the specific type
*/
void runTo(String simulationEventType);
/**
* close simulation run
*/
void close();
}
重播一个流程实例示例: ReplyRunTest
ProcessEngine processEngine = initProcessEngine();
TaskService taskService = processEngine.getTaskService();
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> variables = new HashMap<String, Object>();
variables.put(TEST_VARIABLE, TEST_VALUE);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(USERTASK_PROCESS, BUSINESS_KEY,
variables);
Task task = taskService.createTaskQuery().taskDefinitionKey("userTask").singleResult();
TimeUnit.MILLISECONDS.sleep(50);
taskService.complete(task.getId());
使用的流程引擎是基础的InMemoryStandaloneProcessEngine: 配置了InMemoryRecordActivitiEventListener(记录Activiti事件,并转换为模拟事件)和UserTaskExecutionListener(当创建新用户任务时,新任务会重播流程实例,把任务完成事件放到事件日历中)
final SimulationDebugger simRun = new ReplaySimulationRun(processEngine,
getReplayHandlers(processInstance.getId()));
simRun.init();
// original process is finished - there should not be any running process instance/task
assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
assertEquals(0, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
simRun.step();
// replay process was started
assertEquals(1, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
// there should be one task
assertEquals(1, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
simRun.step();
// userTask was completed - replay process was finished
assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
assertEquals(0, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
simRun.close();
processEngine.close();
ProcessEngines.destroy();