use of org.jbpm.workflow.instance.node.StateBasedNodeInstance in project jbpm by kiegroup.
the class UpdateTimerCommand method execute.
@Override
public Void execute(Context context) {
logger.debug("About to cancel timer in process instance {} by name '{}' or id {}", processInstanceId, timerName, timerId);
KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
TimerManager tm = getTimerManager(kieSession);
RuleFlowProcessInstance wfp = (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId);
if (wfp == null) {
throw new IllegalArgumentException("Process instance with id " + processInstanceId + " not found");
}
for (NodeInstance nodeInstance : wfp.getNodeInstances(true)) {
if (nodeInstance instanceof TimerNodeInstance) {
TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
if (tni.getNodeName().equals(timerName) || tni.getTimerId() == timerId) {
TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());
TimerInstance newTimer = rescheduleTimer(timer, tm);
logger.debug("New timer {} about to be registered", newTimer);
tm.registerTimer(newTimer, wfp);
tni.internalSetTimerId(newTimer.getId());
logger.debug("New timer {} successfully registered", newTimer);
break;
}
} else if (nodeInstance instanceof StateBasedNodeInstance) {
StateBasedNodeInstance sbni = (StateBasedNodeInstance) nodeInstance;
List<Long> timerList = sbni.getTimerInstances();
if (sbni.getNodeName().equals(timerName) || (timerList != null && timerList.contains(timerId))) {
if (timerList != null && timerList.size() == 1) {
TimerInstance timer = tm.getTimerMap().get(timerList.get(0));
TimerInstance newTimer = rescheduleTimer(timer, tm);
logger.debug("New timer {} about to be registered", newTimer);
tm.registerTimer(newTimer, wfp);
timerList.clear();
timerList.add(newTimer.getId());
sbni.internalSetTimerInstances(timerList);
logger.debug("New timer {} successfully registered", newTimer);
}
break;
}
}
}
return null;
}
use of org.jbpm.workflow.instance.node.StateBasedNodeInstance in project jbpm by kiegroup.
the class ListTimersCommand method processNodeInstance.
protected void processNodeInstance(TimerManager tm, NodeInstanceContainer container, List<TimerInstance> timers) {
for (NodeInstance nodeInstance : container.getNodeInstances()) {
if (nodeInstance instanceof TimerNodeInstance) {
TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
org.jbpm.process.instance.timer.TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());
TimerInstanceImpl details = buildTimer(timer);
details.setTimerName(resolveVariable(tni.getNodeName(), tni));
timers.add(details);
} else if (nodeInstance instanceof StateBasedNodeInstance) {
StateBasedNodeInstance sbni = (StateBasedNodeInstance) nodeInstance;
List<Long> timerList = sbni.getTimerInstances();
if (timerList != null) {
for (Long timerId : timerList) {
org.jbpm.process.instance.timer.TimerInstance timer = tm.getTimerMap().get(timerId);
TimerInstanceImpl details = buildTimer(timer);
details.setTimerName(resolveVariable(sbni.getNodeName(), sbni));
timers.add(details);
}
}
}
if (nodeInstance instanceof NodeInstanceContainer) {
processNodeInstance(tm, (NodeInstanceContainer) nodeInstance, timers);
}
}
}
use of org.jbpm.workflow.instance.node.StateBasedNodeInstance in project jbpm by kiegroup.
the class MigrationManager method cancelActiveTimersBeforeMigration.
protected Map<Long, List<TimerInstance>> cancelActiveTimersBeforeMigration(RuntimeManager manager) {
RuntimeEngine engineBefore = manager.getRuntimeEngine(ProcessInstanceIdContext.get(migrationSpec.getProcessInstanceId()));
try {
Map<Long, List<TimerInstance>> timerMigrated = engineBefore.getKieSession().execute(new ExecutableCommand<Map<Long, List<TimerInstance>>>() {
private static final long serialVersionUID = 7144271692067781976L;
@Override
public Map<Long, List<TimerInstance>> execute(Context context) {
Map<Long, List<TimerInstance>> result = new LinkedHashMap<>();
KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
TimerManager timerManager = getTimerManager(kieSession);
WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kieSession.getProcessInstance(migrationSpec.getProcessInstanceId());
Collection<org.jbpm.workflow.instance.NodeInstance> activeInstances = processInstance.getNodeInstances(true);
for (org.jbpm.workflow.instance.NodeInstance active : activeInstances) {
if (active instanceof TimerNodeInstance) {
TimerInstance timerInstance = timerManager.getTimerMap().get(((TimerNodeInstance) active).getTimerId());
timerManager.cancelTimer(timerInstance.getId());
result.put(active.getId(), Arrays.asList(timerInstance));
} else if (active instanceof StateBasedNodeInstance) {
List<Long> timers = ((StateBasedNodeInstance) active).getTimerInstances();
if (timers != null && !timers.isEmpty()) {
List<TimerInstance> collected = new ArrayList<>();
for (Long timerId : timers) {
TimerInstance timerInstance = timerManager.getTimerMap().get(timerId);
timerManager.cancelTimer(timerInstance.getId());
collected.add(timerInstance);
}
result.put(active.getId(), collected);
}
}
}
return result;
}
});
return timerMigrated;
} finally {
manager.disposeRuntimeEngine(engineBefore);
}
}
use of org.jbpm.workflow.instance.node.StateBasedNodeInstance in project jbpm by kiegroup.
the class MigrationManager method rescheduleTimersAfterMigration.
protected void rescheduleTimersAfterMigration(RuntimeManager manager, Map<Long, List<TimerInstance>> timerMigrated) {
RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(migrationSpec.getProcessInstanceId()));
try {
engine.getKieSession().execute(new ExecutableCommand<Void>() {
private static final long serialVersionUID = 7144657913971146080L;
@Override
public Void execute(Context context) {
KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
TimerManager timerManager = getTimerManager(kieSession);
WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kieSession.getProcessInstance(migrationSpec.getProcessInstanceId());
for (Entry<Long, List<TimerInstance>> entry : timerMigrated.entrySet()) {
org.jbpm.workflow.instance.NodeInstance active = processInstance.getNodeInstance(entry.getKey(), false);
if (active instanceof TimerNodeInstance) {
TimerInstance timerInstance = entry.getValue().get(0);
long delay = timerInstance.getDelay() - (System.currentTimeMillis() - timerInstance.getActivated().getTime());
timerInstance.setDelay(delay);
timerManager.registerTimer(timerInstance, processInstance);
((TimerNodeInstance) active).internalSetTimerId(timerInstance.getId());
} else if (active instanceof StateBasedNodeInstance) {
List<TimerInstance> timerInstances = entry.getValue();
List<Long> timers = new ArrayList<>();
for (TimerInstance timerInstance : timerInstances) {
long delay = timerInstance.getDelay() - (System.currentTimeMillis() - timerInstance.getActivated().getTime());
timerInstance.setDelay(delay);
timerManager.registerTimer(timerInstance, processInstance);
timers.add(timerInstance.getId());
}
((StateBasedNodeInstance) active).internalSetTimerInstances(timers);
}
}
return null;
}
});
} finally {
manager.disposeRuntimeEngine(engine);
}
}
Aggregations