Search in sources :

Example 16 with TimerInstance

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

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