use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.
the class EventOverviewPanel method getTransformers.
protected List<Function<EventLogEntry, SimulationEvent>> getTransformers() {
List<Function<EventLogEntry, SimulationEvent>> transformers = new ArrayList<Function<EventLogEntry, SimulationEvent>>();
transformers.add(new EventLogProcessInstanceCreateTransformer(PROCESS_INSTANCE_START_EVENT_TYPE, PROCESS_DEFINITION_ID_KEY, SIMULATION_BUSINESS_KEY, VARIABLES_KEY));
transformers.add(new EventLogUserTaskCompleteTransformer(USER_TASK_COMPLETED_EVENT_TYPE));
return transformers;
}
use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.
the class ReplayEventLogTest method testProcessInstanceStartEvents.
@Test
public void testProcessInstanceStartEvents() throws Exception {
ProcessEngineImpl processEngine = initProcessEngine();
TaskService taskService = processEngine.getTaskService();
RuntimeService runtimeService = processEngine.getRuntimeService();
ManagementService managementService = processEngine.getManagementService();
HistoryService historyService = processEngine.getHistoryService();
// record events
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);
variables = new HashMap<String, Object>();
variables.put(TASK_TEST_VARIABLE, TASK_TEST_VALUE);
taskService.complete(task.getId(), variables);
// transform log events
List<EventLogEntry> eventLogEntries = managementService.getEventLogEntries(null, null);
EventLogTransformer transformer = new EventLogTransformer(getTransformers());
List<SimulationEvent> simulationEvents = transformer.transform(eventLogEntries);
SimpleEventCalendar eventCalendar = new SimpleEventCalendar(processEngine.getProcessEngineConfiguration().getClock(), new SimulationEventComparator());
eventCalendar.addEvents(simulationEvents);
// replay process instance run
final SimulationDebugger simRun = new ReplaySimulationRun(processEngine, eventCalendar, getReplayHandlers(processInstance.getId()));
simRun.init(new NoExecutionVariableScope());
// 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
ProcessInstance replayProcessInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).singleResult();
assertNotNull(replayProcessInstance);
assertTrue(replayProcessInstance.getId().equals(processInstance.getId()) == false);
assertEquals(TEST_VALUE, runtimeService.getVariable(replayProcessInstance.getId(), TEST_VARIABLE));
// 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());
HistoricVariableInstance variableInstance = historyService.createHistoricVariableInstanceQuery().processInstanceId(replayProcessInstance.getId()).variableName(TASK_TEST_VARIABLE).singleResult();
assertNotNull(variableInstance);
assertEquals(TASK_TEST_VALUE, variableInstance.getValue());
// close simulation
simRun.close();
processEngine.close();
ProcessEngines.destroy();
}
use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.
the class SimulationAcquireJobsRunnable method run.
public synchronized void run() {
// if (log.isLoggable(Level.INFO)) {
// log.info(jobExecutor.getName() + " starting to acquire jobs");
// }
final CommandExecutor commandExecutor = jobExecutor.getCommandExecutor();
// while is not needed - repetition is done by event scheduling
// while (!isInterrupted) {
isWaiting.set(false);
int maxJobsPerAcquisition = jobExecutor.getMaxJobsPerAcquisition();
try {
AcquiredJobs acquiredJobs = commandExecutor.execute(jobExecutor.getAcquireJobsCmd());
for (List<String> jobIds : acquiredJobs.getJobIdBatches()) {
jobExecutor.executeJobs(jobIds);
}
// if all jobs were executed
millisToWait = jobExecutor.getWaitTimeInMillis();
int jobsAcquired = acquiredJobs.getJobIdBatches().size();
if (jobsAcquired < maxJobsPerAcquisition) {
isJobAdded = false;
// check if the next timer should fire before the normal sleep time is over
Date duedate = new Date(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait);
List<TimerEntity> nextTimers = commandExecutor.execute(new GetUnlockedTimersByDuedateCmd(duedate, new Page(0, 1)));
if (!nextTimers.isEmpty()) {
long millisTillNextTimer = nextTimers.get(0).getDuedate().getTime() - SimulationRunContext.getClock().getCurrentTime().getTime();
if (millisTillNextTimer < millisToWait && millisTillNextTimer != 0) {
millisToWait = millisTillNextTimer;
}
}
} else {
millisToWait = 0;
}
} catch (ActivitiOptimisticLockingException optimisticLockingException) {
// See https://activiti.atlassian.net/browse/ACT-1390
log.trace("Optimistic locking exception during job acquisition. If you have multiple job executors running against the same database, " + "this exception means that this thread tried to acquire a job, which already was acquired by another job executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple job executor acquisition threads running against the same database. " + "Exception message: " + optimisticLockingException.getMessage());
} catch (Exception e) {
log.error("exception during job acquisition: " + e.getMessage(), e);
millisToWait = jobExecutor.getWaitTimeInMillis();
}
if ((millisToWait > 0) && (!isJobAdded)) {
try {
log.trace("job acquisition thread sleeping for " + millisToWait + " millis");
synchronized (MONITOR) {
if (!isInterrupted) {
isWaiting.set(true);
SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait).property(this).build();
SimulationRunContext.getEventCalendar().addEvent(event);
// do not need to wait. - event scheduling is enough
//MONITOR.wait(millisToWait);
}
}
log.trace("job acquisition thread woke up");
} finally {
// isWaiting.set(false);
}
} else {
// schedule run now
SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime()).property(this).build();
SimulationRunContext.getEventCalendar().addEvent(event);
}
// }
// if (log.isLoggable(Level.INFO)) {
// log.info(jobExecutor.getName() + " stopped job acquisition");
// }
}
use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.
the class SimulationAcquireJobsRunnable method jobWasAdded.
public void jobWasAdded() {
isJobAdded = true;
if (isWaiting.compareAndSet(true, false)) {
// I am OK with the race condition
synchronized (MONITOR) {
// MONITOR.notifyAll();
//Notify is not needed - event is enough
SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime()).property(this).build();
SimulationRunContext.getEventCalendar().addEvent(event);
}
}
}
use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.
the class ProcessInstanceCreateTransformer method apply.
@Override
public SimulationEvent apply(ActivitiEvent event) {
if (ActivitiEventType.ENTITY_INITIALIZED.equals(event.getType()) && (event instanceof ActivitiEntityEvent) && ((ActivitiEntityEvent) event).getEntity() instanceof ProcessInstance && ((ExecutionEntity) ((ActivitiEntityEvent) event).getEntity()).isProcessInstanceType()) {
ProcessInstance processInstance = (ProcessInstance) ((ActivitiEntityEvent) event).getEntity();
ExecutionEntity executionEntity = (ExecutionEntity) ((ActivitiEntityEvent) event).getEntity();
Map<String, Object> simEventProperties = new HashMap<String, Object>();
simEventProperties.put(processDefinitionIdKey, processInstance.getProcessDefinitionId());
simEventProperties.put(businessKey, processInstance.getBusinessKey());
simEventProperties.put(variablesKey, executionEntity.getVariables());
simEventProperties.put(PROCESS_INSTANCE_ID, executionEntity.getProcessInstanceId());
return new SimulationEvent.Builder(simulationEventType).simulationTime(Context.getProcessEngineConfiguration().getClock().getCurrentTime().getTime()).properties(simEventProperties).build();
}
return null;
}
Aggregations