Search in sources :

Example 1 with StateBasedNodeInstance

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;
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) List(java.util.List) TimerManager(org.jbpm.process.instance.timer.TimerManager) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance)

Example 2 with StateBasedNodeInstance

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);
        }
    }
}
Also used : StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) TimerInstance(org.jbpm.services.api.admin.TimerInstance) ArrayList(java.util.ArrayList) List(java.util.List) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) TimerInstanceImpl(org.jbpm.kie.services.impl.admin.TimerInstanceImpl)

Example 3 with StateBasedNodeInstance

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);
    }
}
Also used : TimerInstance(org.jbpm.process.instance.timer.TimerInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) TimerManager(org.jbpm.process.instance.timer.TimerManager) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance)

Example 4 with StateBasedNodeInstance

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);
    }
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ArrayList(java.util.ArrayList) TimerManager(org.jbpm.process.instance.timer.TimerManager) Entry(java.util.Map.Entry) KieSession(org.kie.api.runtime.KieSession) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance)

Aggregations

StateBasedNodeInstance (org.jbpm.workflow.instance.node.StateBasedNodeInstance)4 TimerNodeInstance (org.jbpm.workflow.instance.node.TimerNodeInstance)4 NodeInstance (org.kie.api.runtime.process.NodeInstance)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RegistryContext (org.drools.core.command.impl.RegistryContext)3 TimerInstance (org.jbpm.process.instance.timer.TimerInstance)3 TimerManager (org.jbpm.process.instance.timer.TimerManager)3 KieSession (org.kie.api.runtime.KieSession)3 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)2 HumanTaskNodeInstance (org.jbpm.workflow.instance.node.HumanTaskNodeInstance)2 Context (org.kie.api.runtime.Context)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 ProcessInstanceIdContext (org.kie.internal.runtime.manager.context.ProcessInstanceIdContext)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 TimerInstanceImpl (org.jbpm.kie.services.impl.admin.TimerInstanceImpl)1