Search in sources :

Example 6 with TimerManager

use of org.jbpm.process.instance.timer.TimerManager in project jbpm by kiegroup.

the class StateBasedNodeInstance method internalTrigger.

public void internalTrigger(NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    // activate timers
    Map<Timer, DroolsAction> timers = getEventBasedNode().getTimers();
    if (timers != null) {
        addTimerListener();
        timerInstances = new ArrayList<Long>(timers.size());
        TimerManager timerManager = ((InternalProcessRuntime) getProcessInstance().getKnowledgeRuntime().getProcessRuntime()).getTimerManager();
        for (Timer timer : timers.keySet()) {
            TimerInstance timerInstance = createTimerInstance(timer);
            timerManager.registerTimer(timerInstance, (ProcessInstance) getProcessInstance());
            timerInstances.add(timerInstance.getId());
        }
    }
    if (getEventBasedNode().getBoundaryEvents() != null) {
        for (String name : getEventBasedNode().getBoundaryEvents()) {
            boolean isActive = ((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda()).isRuleActiveInRuleFlowGroup("DROOLS_SYSTEM", name, getProcessInstance().getId());
            if (isActive) {
                getProcessInstance().getKnowledgeRuntime().signalEvent(name, null);
            } else {
                addActivationListener();
            }
        }
    }
    ((WorkflowProcessInstanceImpl) getProcessInstance()).addActivatingNodeId((String) getNode().getMetaData().get("UniqueId"));
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) InternalAgenda(org.drools.core.common.InternalAgenda) Timer(org.jbpm.process.core.timer.Timer) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) TimerManager(org.jbpm.process.instance.timer.TimerManager)

Example 7 with TimerManager

use of org.jbpm.process.instance.timer.TimerManager in project jbpm by kiegroup.

the class ProtobufProcessMarshaller method writeProcessTimers.

public void writeProcessTimers(MarshallerWriteContext outCtx) throws IOException {
    outCtx.writersByClass.put(ProcessJobContext.class, new TimerManager.ProcessTimerOutputMarshaller());
    outCtx.writersByClass.put(StartProcessJobContext.class, new TimerManager.ProcessTimerOutputMarshaller());
    ProtobufMessages.ProcessData.Builder _pdata = (ProtobufMessages.ProcessData.Builder) outCtx.parameterObject;
    TimerManager timerManager = ((InternalProcessRuntime) ((InternalWorkingMemory) outCtx.wm).getProcessRuntime()).getTimerManager();
    long timerId = timerManager.internalGetTimerId();
    _pdata.setExtension(JBPMMessages.timerId, timerId);
}
Also used : Builder(org.jbpm.marshalling.impl.JBPMMessages.ProcessTimer.TimerInstance.Builder) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) TimerManager(org.jbpm.process.instance.timer.TimerManager) ProtobufMessages(org.drools.core.marshalling.impl.ProtobufMessages)

Example 8 with TimerManager

use of org.jbpm.process.instance.timer.TimerManager in project jbpm by kiegroup.

the class SerializedTimerRollbackTest method testSerizliableTestsWithEngineRollback.

@Test
public void testSerizliableTestsWithEngineRollback() {
    try {
        createRuntimeManager("org/jbpm/test/functional/timer/HumanTaskWithBoundaryTimer.bpmn");
        RuntimeEngine runtimeEngine = getRuntimeEngine();
        KieSession ksession = runtimeEngine.getKieSession();
        logger.debug("Created knowledge session");
        TaskService taskService = runtimeEngine.getTaskService();
        logger.debug("Task service created");
        List<Long> committedProcessInstanceIds = new ArrayList<Long>();
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("test", "john");
                logger.debug("Creating process instance: {}", i);
                ProcessInstance pi = ksession.startProcess("PROCESS_1", params);
                committedProcessInstanceIds.add(pi.getId());
            } else {
                try {
                    Map<String, Object> params = new HashMap<String, Object>();
                    // set test variable to null so engine will rollback
                    params.put("test", null);
                    logger.debug("Creating process instance: {}", i);
                    ksession.startProcess("PROCESS_1", params);
                } catch (Exception e) {
                    logger.debug("Process rolled back");
                }
            }
        }
        Connection c = getDs().getConnection();
        Statement st = c.createStatement();
        ResultSet rs = st.executeQuery("select rulesbytearray from sessioninfo");
        rs.next();
        Blob b = rs.getBlob("rulesbytearray");
        assertNotNull(b);
        KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        ProtobufMarshaller marshaller = new ProtobufMarshaller(builder.newKieBase(), new MarshallingConfigurationImpl());
        StatefulKnowledgeSession session = marshaller.unmarshall(b.getBinaryStream());
        assertNotNull(session);
        TimerManager timerManager = ((InternalProcessRuntime) ((InternalKnowledgeRuntime) session).getProcessRuntime()).getTimerManager();
        assertNotNull(timerManager);
        Collection<TimerInstance> timers = timerManager.getTimers();
        assertNotNull(timers);
        assertEquals(5, timers.size());
        for (TimerInstance timerInstance : timers) {
            assertTrue(committedProcessInstanceIds.contains(timerInstance.getProcessInstanceId()));
            ksession.abortProcessInstance(timerInstance.getProcessInstanceId());
        }
        List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
        assertEquals(0, tasks.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown");
    }
}
Also used : ProtobufMarshaller(org.drools.core.marshalling.impl.ProtobufMarshaller) HashMap(java.util.HashMap) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) ArrayList(java.util.ArrayList) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ResultSet(java.sql.ResultSet) KieSession(org.kie.api.runtime.KieSession) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Blob(java.sql.Blob) TaskService(org.kie.api.task.TaskService) Statement(java.sql.Statement) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Connection(java.sql.Connection) TimerManager(org.jbpm.process.instance.timer.TimerManager) TaskSummary(org.kie.api.task.model.TaskSummary) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) Test(org.junit.Test)

Example 9 with TimerManager

use of org.jbpm.process.instance.timer.TimerManager in project jbpm by kiegroup.

the class ListTimersCommand method execute.

public List<TimerInstance> execute(Context context) {
    List<TimerInstance> timers = new ArrayList<TimerInstance>();
    KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
    TimerManager tm = getTimerManager(kieSession);
    RuleFlowProcessInstance wfp = (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId, true);
    if (wfp == null) {
        throw new ProcessInstanceNotFoundException("No process instance can be found for id " + processInstanceId);
    }
    processNodeInstance(tm, wfp, timers);
    return timers;
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) TimerInstance(org.jbpm.services.api.admin.TimerInstance) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) TimerManager(org.jbpm.process.instance.timer.TimerManager) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException)

Example 10 with TimerManager

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

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