Search in sources :

Example 11 with InternalProcessRuntime

use of org.jbpm.process.instance.InternalProcessRuntime in project jbpm by kiegroup.

the class WorkflowProcessInstanceImpl method setState.

public void setState(final int state, String outcome) {
    super.setState(state, outcome);
    // TODO move most of this to ProcessInstanceImpl
    if (state == ProcessInstance.STATE_COMPLETED || state == ProcessInstance.STATE_ABORTED) {
        if (this.slaCompliance == SLA_PENDING) {
            if (System.currentTimeMillis() > slaDueDate.getTime()) {
                // completion of the process instance is after expected SLA due date, mark it accordingly
                this.slaCompliance = SLA_VIOLATED;
            } else {
                this.slaCompliance = state == ProcessInstance.STATE_COMPLETED ? SLA_MET : SLA_ABORTED;
            }
        }
        InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
        InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime();
        processRuntime.getProcessEventSupport().fireBeforeProcessCompleted(this, kruntime);
        // deactivate all node instances of this process instance
        while (!nodeInstances.isEmpty()) {
            NodeInstance nodeInstance = nodeInstances.get(0);
            ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel();
        }
        if (this.slaTimerId > -1) {
            processRuntime.getTimerManager().cancelTimer(this.slaTimerId);
            logger.debug("SLA Timer {} has been canceled", this.slaTimerId);
        }
        removeEventListeners();
        processRuntime.getProcessInstanceManager().removeProcessInstance(this);
        processRuntime.getProcessEventSupport().fireAfterProcessCompleted(this, kruntime);
        if (isSignalCompletion()) {
            RuntimeManager manager = (RuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER);
            if (getParentProcessInstanceId() > 0 && manager != null) {
                try {
                    org.kie.api.runtime.manager.Context<?> context = ProcessInstanceIdContext.get(getParentProcessInstanceId());
                    String caseId = (String) kruntime.getEnvironment().get(EnvironmentName.CASE_ID);
                    if (caseId != null) {
                        context = CaseContext.get(caseId);
                    }
                    RuntimeEngine runtime = manager.getRuntimeEngine(context);
                    KieRuntime managedkruntime = (KieRuntime) runtime.getKieSession();
                    managedkruntime.signalEvent("processInstanceCompleted:" + getId(), this);
                } catch (SessionNotFoundException e) {
                // in case no session is found for parent process let's skip signal for process instance completion
                }
            } else {
                processRuntime.getSignalManager().signalEvent("processInstanceCompleted:" + getId(), this);
            }
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) KieRuntime(org.kie.api.runtime.KieRuntime) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) EndNodeInstance(org.jbpm.workflow.instance.node.EndNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Example 12 with InternalProcessRuntime

use of org.jbpm.process.instance.InternalProcessRuntime 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 13 with InternalProcessRuntime

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

Aggregations

InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)13 TimerManager (org.jbpm.process.instance.timer.TimerManager)7 TimerInstance (org.jbpm.process.instance.timer.TimerInstance)4 ArrayList (java.util.ArrayList)3 ProcessEventSupport (org.drools.core.event.ProcessEventSupport)3 Test (org.junit.Test)3 KieSession (org.kie.api.runtime.KieSession)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 Blob (java.sql.Blob)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)2 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)2 ProtobufMarshaller (org.drools.core.marshalling.impl.ProtobufMarshaller)2 ProtobufMessages (org.drools.core.marshalling.impl.ProtobufMessages)2 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 TaskService (org.kie.api.task.TaskService)2