Search in sources :

Example 6 with InternalProcessRuntime

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

the class TimerTest method testTimer.

@Test
@Ignore
public void testTimer() {
    // AbstractRuleBase ruleBase = (AbstractRuleBase) RuleBaseFactory.newRuleBase();
    // ExecutorService executorService = new DefaultExecutorService();
    // final StatefulSession workingMemory = new ReteooStatefulSession(1, ruleBase, executorService);
    // executorService.setCommandExecutor( new CommandExecutor( workingMemory ) );
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final KieSession workingMemory = kbase.newKieSession();
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance() {

        private static final long serialVersionUID = 510l;

        public void signalEvent(String type, Object event) {
            if ("timerTriggered".equals(type)) {
                TimerInstance timer = (TimerInstance) event;
                logger.info("Timer {} triggered", timer.getId());
                counter++;
            }
        }
    };
    processInstance.setKnowledgeRuntime(((InternalWorkingMemory) workingMemory).getKnowledgeRuntime());
    processInstance.setId(1234);
    InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ((InternalWorkingMemory) workingMemory).getProcessRuntime());
    processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);
    new Thread(new Runnable() {

        public void run() {
            workingMemory.fireUntilHalt();
        }
    }).start();
    TimerManager timerManager = ((InternalProcessRuntime) ((InternalWorkingMemory) workingMemory).getProcessRuntime()).getTimerManager();
    TimerInstance timer = new TimerInstance();
    timerManager.registerTimer(timer, processInstance);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    timer = new TimerInstance();
    timer.setDelay(500);
    timerManager.registerTimer(timer, processInstance);
    assertEquals(0, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    timer = new TimerInstance();
    timer.setDelay(500);
    timer.setPeriod(300);
    timerManager.registerTimer(timer, processInstance);
    assertEquals(0, counter);
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    // we can't know exactly how many times this will fire as timers are not precise, but should be atleast 4
    assertTrue(counter >= 4);
    timerManager.cancelTimer(timer.getId());
    int lastCount = counter;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(lastCount, counter);
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) TimerManager(org.jbpm.process.instance.timer.TimerManager) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 7 with InternalProcessRuntime

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

the class ProtobufProcessMarshaller method readProcessTimers.

public void readProcessTimers(MarshallerReaderContext inCtx) throws IOException, ClassNotFoundException {
    inCtx.readersByInt.put(ProtobufMessages.Timers.TimerType.PROCESS_VALUE, new TimerManager.ProcessTimerInputMarshaller());
    ProtobufMessages.ProcessData _pdata = (ProtobufMessages.ProcessData) inCtx.parameterObject;
    TimerManager timerManager = ((InternalProcessRuntime) ((InternalWorkingMemory) inCtx.wm).getProcessRuntime()).getTimerManager();
    timerManager.internalSetTimerId(_pdata.getExtension(JBPMMessages.timerId));
// 
// int token;
// while ( (token = inCtx.readShort()) != PersisterEnums.END ) {
// switch ( token ) {
// case PersisterEnums.TIMER : {
// TimerInstance timer = readTimer( inCtx );
// timerManager.internalAddTimer( timer );
// break;
// }
// case PersisterEnums.DEFAULT_TIMER : {
// InputMarshaller.readTimer( inCtx );
// break;
// }
// }
// }
}
Also used : InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) TimerManager(org.jbpm.process.instance.timer.TimerManager) ProtobufMessages(org.drools.core.marshalling.impl.ProtobufMessages)

Example 8 with InternalProcessRuntime

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

the class SerializedTimerRollbackTest method testSerizliableTestsWithExternalRollback.

@Test
public void testSerizliableTestsWithExternalRollback() {
    try {
        createRuntimeManager("org/jbpm/test/functional/timer/HumanTaskWithBoundaryTimer.bpmn");
        RuntimeEngine runtimeEngine = getRuntimeEngine();
        KieSession ksession = runtimeEngine.getKieSession();
        TaskService taskService = runtimeEngine.getTaskService();
        logger.debug("Created knowledge session");
        TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
        List<Long> committedProcessInstanceIds = new ArrayList<Long>();
        for (int i = 0; i < 10; i++) {
            tm.begin();
            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);
            if (i % 2 == 0) {
                committedProcessInstanceIds.add(pi.getId());
                tm.commit();
            } else {
                tm.rollback();
            }
        }
        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) TransactionManager(javax.transaction.TransactionManager) 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 InternalProcessRuntime

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

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

the class WorkflowProcessInstanceImpl method handleSLAViolation.

private void handleSLAViolation() {
    if (slaCompliance == SLA_PENDING) {
        InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
        InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime();
        processRuntime.getProcessEventSupport().fireBeforeSLAViolated(this, kruntime);
        logger.debug("SLA violated on process instance {}", getId());
        this.slaCompliance = SLA_VIOLATED;
        this.slaTimerId = -1;
        processRuntime.getProcessEventSupport().fireAfterSLAViolated(this, kruntime);
    }
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime)

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