📄 timerdbtest.java
字号:
" <start-state>" +
" <transition to='s' />" +
" </start-state>" +
" <state name='s'>" +
" <event type='node-enter'>" +
" <create-timer duedate='26 business seconds'>" +
" <action class='claim.you.are.Innocent' />" +
" </create-timer>" +
" </event>" +
" <transition to='end' />" +
" </state>" +
" <end-state name='end' />" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
processInstance.signal();
processInstance = saveAndReload(processInstance);
assertTrue(schedulerSession.findTimersByDueDate().hasNext());
processInstance.signal();
processInstance = saveAndReload(processInstance);
assertFalse(schedulerSession.findTimersByDueDate().hasNext());
}
public void testFindTimersByName() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='timed task' />" +
" </start-state>" +
" <task-node name='timed task'>" +
" <task name='find the hole in the market'>" +
" <timer name='reminder' duedate='23 business seconds'>" +
" <action class='geftem-eu-shuppe-oender-ze-konte'/>" +
" </timer>" +
" </task>" +
" </task-node>" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
processInstance.signal();
processInstance = saveAndReload(processInstance);
List timersByName = schedulerSession.findTimersByName("reminder", processInstance.getRootToken());
assertNotNull(timersByName);
assertEquals(1, timersByName.size());
Timer timer = (Timer) timersByName.get(0);
assertEquals("geftem-eu-shuppe-oender-ze-konte", timer.getAction().getActionDelegation().getClassName());
}
public static class NoOp implements ActionHandler {
private static final long serialVersionUID = 1L;
public void execute(ExecutionContext executionContext) throws Exception {
}
}
public void testTimerRepeat() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='a' />" +
" </start-state>" +
" <state name='a'>" +
" <timer name='reminder' " +
" duedate='0 seconds'" +
" repeat='5 seconds' >" +
" <action class='org.jbpm.scheduler.exe.TimerDbTest$NoOp' />" +
" </timer>" +
" <transition to='b'/>" +
" <transition name='back' to='a'/>" +
" </state>" +
" <state name='b'/>" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
long before = System.currentTimeMillis();
processInstance.signal();
long after = System.currentTimeMillis();
jbpmContext.save(processInstance);
newTransaction();
Iterator iter = schedulerSession.findTimersByDueDate();
assertTrue(iter.hasNext());
Timer timer = (Timer) iter.next();
assertNotNull(timer);
Date date = timer.getDueDate();
assertNotNull(date);
assertTrue(before <= date.getTime());
assertTrue(date.getTime() <= after);
long origDueDate = date.getTime();
assertFalse(iter.hasNext());
commitAndCloseSession();
SchedulerThread schedulerThread = new SchedulerThread(jbpmConfiguration);
schedulerThread.executeTimers();
beginSessionTransaction();
iter = schedulerSession.findTimersByDueDate();
assertTrue(iter.hasNext());
timer = (Timer) iter.next();
assertFalse(iter.hasNext());
assertEquals(origDueDate+5000, timer.getDueDate().getTime());
processInstance = jbpmContext.loadProcessInstance(processInstance.getId());
before = System.currentTimeMillis();
processInstance.signal("back");
after = System.currentTimeMillis();
jbpmContext.save(processInstance);
newTransaction();
iter = schedulerSession.findTimersByDueDate();
assertTrue(iter.hasNext());
timer = (Timer) iter.next();
assertNotNull(timer);
date = timer.getDueDate();
assertNotNull(date);
assertTrue(before <= date.getTime());
assertTrue(date.getTime() <= after);
assertFalse(iter.hasNext());
newTransaction();
processInstance = jbpmContext.loadProcessInstance(processInstance.getId());
processInstance.signal();
jbpmContext.save(processInstance);
newTransaction();
iter = schedulerSession.findTimersByDueDate();
assertFalse(iter.hasNext());
}
public static class UpdateVariables implements ActionHandler {
private static final long serialVersionUID = 1L;
public void execute(ExecutionContext executionContext) throws Exception {
executionContext.setVariable("a", "value a updated");
executionContext.setVariable("b", "value b updated");
}
}
public void testTimerUpdatingProcessVariables() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='a' />" +
" </start-state>" +
" <task-node name='a'>" +
" <task name='wait for var updates'>" +
" <controller>" +
// variable a will be a task instance local variable
// variable b will be a process instance variable
" <variable name='a' />" +
" </controller>" +
" <timer name='update variables' " +
" duedate='0 seconds'" +
" repeat='5 seconds' >" +
" <action class='org.jbpm.scheduler.exe.TimerDbTest$UpdateVariables' />" +
" </timer>" +
" </task>" +
" </task-node>" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
ContextInstance contextInstance = processInstance.getContextInstance();
contextInstance.setVariable("a", "value a");
contextInstance.setVariable("b", "value b");
processInstance.signal();
jbpmContext.save(processInstance);
commitAndCloseSession();
SchedulerThread schedulerThread = new SchedulerThread(jbpmConfiguration);
schedulerThread.executeTimers();
beginSessionTransaction();
processInstance = jbpmContext.loadProcessInstance(processInstance.getId());
contextInstance = processInstance.getContextInstance();
assertEquals("value a", contextInstance.getVariable("a") );
assertEquals("value b updated", contextInstance.getVariable("b") );
TaskInstance taskInstance = (TaskInstance) processInstance.getTaskMgmtInstance().getTaskInstances().iterator().next();
assertEquals("value a updated", taskInstance.getVariable("a") );
assertEquals("value b updated", taskInstance.getVariable("b") );
}
public static class ConcurrentUpdateAction implements ActionHandler {
private static final long serialVersionUID = 1L;
public void execute(ExecutionContext executionContext) throws Exception {
executionContext.setVariable("a", "value a timer actioned updated");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -