Search in sources :

Example 11 with TimerManager

use of org.jbpm.process.instance.timer.TimerManager 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

TimerManager (org.jbpm.process.instance.timer.TimerManager)11 InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)7 TimerInstance (org.jbpm.process.instance.timer.TimerInstance)7 KieSession (org.kie.api.runtime.KieSession)7 ArrayList (java.util.ArrayList)5 RegistryContext (org.drools.core.command.impl.RegistryContext)4 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)4 HashMap (java.util.HashMap)3 RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)3 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)3 StateBasedNodeInstance (org.jbpm.workflow.instance.node.StateBasedNodeInstance)3 TimerNodeInstance (org.jbpm.workflow.instance.node.TimerNodeInstance)3 Test (org.junit.Test)3 NodeInstance (org.kie.api.runtime.process.NodeInstance)3 Blob (java.sql.Blob)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 List (java.util.List)2 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)2