Search in sources :

Example 6 with TimerInstance

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

the class ProtobufProcessMarshaller method readTimer.

public static TimerInstance readTimer(MarshallerReaderContext context, JBPMMessages.ProcessTimer.TimerInstance _timer) {
    TimerInstance timer = new TimerInstance();
    timer.setId(_timer.getId());
    timer.setTimerId(_timer.getTimerId());
    timer.setDelay(_timer.getDelay());
    timer.setPeriod(_timer.getPeriod());
    timer.setProcessInstanceId(_timer.getProcessInstanceId());
    if (_timer.hasDEPRECATEDSessionId()) {
        timer.setSessionId(_timer.getDEPRECATEDSessionId());
    } else {
        timer.setSessionId(_timer.getSessionId());
    }
    timer.setActivated(new Date(_timer.getActivatedTime()));
    if (_timer.hasLastTriggered()) {
        timer.setLastTriggered(new Date(_timer.getLastTriggered()));
    }
    return timer;
}
Also used : TimerInstance(org.jbpm.process.instance.timer.TimerInstance) Date(java.util.Date)

Example 7 with TimerInstance

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

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

the class TimerNodeInstance method createTimerInstance.

protected TimerInstance createTimerInstance(InternalKnowledgeRuntime kruntime) {
    Timer timer = getTimerNode().getTimer();
    TimerInstance timerInstance = new TimerInstance();
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
        BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
        String delay = resolveVariable(timer.getDelay());
        timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(delay));
        if (timer.getPeriod() == null) {
            timerInstance.setPeriod(0);
        } else {
            String period = resolveVariable(timer.getPeriod());
            timerInstance.setPeriod(businessCalendar.calculateBusinessTimeAsDuration(period));
        }
    } else {
        configureTimerInstance(timer, timerInstance);
    }
    timerInstance.setTimerId(timer.getId());
    return timerInstance;
}
Also used : Timer(org.jbpm.process.core.timer.Timer) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar)

Example 9 with TimerInstance

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

the class StateBasedNodeInstance method createTimerInstance.

protected TimerInstance createTimerInstance(Timer timer) {
    TimerInstance timerInstance = new TimerInstance();
    KieRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
        BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
        String delay = null;
        switch(timer.getTimeType()) {
            case Timer.TIME_CYCLE:
                if (CronExpression.isValidExpression(timer.getDelay())) {
                    timerInstance.setCronExpression(timer.getDelay());
                } else {
                    String tempDelay = resolveVariable(timer.getDelay());
                    String tempPeriod = resolveVariable(timer.getPeriod());
                    if (DateTimeUtils.isRepeatable(tempDelay)) {
                        String[] values = DateTimeUtils.parseISORepeatable(tempDelay);
                        String tempRepeatLimit = values[0];
                        tempDelay = values[1];
                        tempPeriod = values[2];
                        if (!tempRepeatLimit.isEmpty()) {
                            try {
                                int repeatLimit = Integer.parseInt(tempRepeatLimit);
                                if (repeatLimit > -1) {
                                    timerInstance.setRepeatLimit(repeatLimit + 1);
                                }
                            } catch (NumberFormatException e) {
                            // ignore
                            }
                        }
                    }
                    timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(tempDelay));
                    if (tempPeriod == null) {
                        timerInstance.setPeriod(0);
                    } else {
                        timerInstance.setPeriod(businessCalendar.calculateBusinessTimeAsDuration(tempPeriod));
                    }
                }
                break;
            case Timer.TIME_DURATION:
                delay = resolveVariable(timer.getDelay());
                timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(delay));
                timerInstance.setPeriod(0);
                break;
            case Timer.TIME_DATE:
                // even though calendar is available concrete date was provided so it shall be used
                configureTimerInstance(timer, timerInstance);
            default:
                break;
        }
    } else {
        configureTimerInstance(timer, timerInstance);
    }
    timerInstance.setTimerId(timer.getId());
    return timerInstance;
}
Also used : TimerInstance(org.jbpm.process.instance.timer.TimerInstance) KieRuntime(org.kie.api.runtime.KieRuntime) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar)

Example 10 with TimerInstance

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

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