Search in sources :

Example 1 with TimerInstance

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

the class StateBasedNodeInstance method configureSla.

protected void configureSla() {
    String slaDueDateExpression = (String) getNode().getMetaData().get("customSLADueDate");
    if (slaDueDateExpression != null) {
        TimerInstance timer = ((WorkflowProcessInstanceImpl) getProcessInstance()).configureSLATimer(slaDueDateExpression);
        if (timer != null) {
            this.slaTimerId = timer.getId();
            this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay());
            this.slaCompliance = ProcessInstance.SLA_PENDING;
            logger.debug("SLA for node instance {} is PENDING with due date {}", this.getId(), this.slaDueDate);
            addTimerListener();
        }
    }
}
Also used : TimerInstance(org.jbpm.process.instance.timer.TimerInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Date(java.util.Date)

Example 2 with TimerInstance

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

the class WorkflowProcessInstanceImpl method configureSLA.

public void configureSLA() {
    String slaDueDateExpression = (String) getProcess().getMetaData().get("customSLADueDate");
    if (slaDueDateExpression != null) {
        TimerInstance timer = configureSLATimer(slaDueDateExpression);
        if (timer != null) {
            this.slaTimerId = timer.getId();
            this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay());
            this.slaCompliance = SLA_PENDING;
            logger.debug("SLA for process instance {} is PENDING with due date {}", this.getId(), this.slaDueDate);
        }
    }
}
Also used : TimerInstance(org.jbpm.process.instance.timer.TimerInstance) Date(java.util.Date)

Example 3 with TimerInstance

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

the class WorkflowProcessInstanceImpl method configureSLATimer.

public TimerInstance configureSLATimer(String slaDueDateExpression) {
    // setup SLA if provided
    slaDueDateExpression = resolveVariable(slaDueDateExpression);
    if (slaDueDateExpression == null || slaDueDateExpression.trim().isEmpty()) {
        logger.debug("Sla due date expression resolved to no value '{}'", slaDueDateExpression);
        return null;
    }
    logger.debug("SLA due date is set to {}", slaDueDateExpression);
    InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
    long duration = -1;
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
        BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
        duration = businessCalendar.calculateBusinessTimeAsDuration(slaDueDateExpression);
    } else {
        duration = DateTimeUtils.parseDuration(slaDueDateExpression);
    }
    TimerInstance timerInstance = new TimerInstance();
    timerInstance.setId(-1);
    timerInstance.setDelay(duration);
    timerInstance.setPeriod(0);
    if (useTimerSLATracking()) {
        ((InternalProcessRuntime) kruntime.getProcessRuntime()).getTimerManager().registerTimer(timerInstance, this);
    }
    return timerInstance;
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar)

Example 4 with TimerInstance

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

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

the class UpdateTimerCommand method execute.

@Override
public Void execute(Context context) {
    logger.debug("About to cancel timer in process instance {} by name '{}' or id {}", processInstanceId, timerName, timerId);
    KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
    TimerManager tm = getTimerManager(kieSession);
    RuleFlowProcessInstance wfp = (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId);
    if (wfp == null) {
        throw new IllegalArgumentException("Process instance with id " + processInstanceId + " not found");
    }
    for (NodeInstance nodeInstance : wfp.getNodeInstances(true)) {
        if (nodeInstance instanceof TimerNodeInstance) {
            TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
            if (tni.getNodeName().equals(timerName) || tni.getTimerId() == timerId) {
                TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());
                TimerInstance newTimer = rescheduleTimer(timer, tm);
                logger.debug("New timer {} about to be registered", newTimer);
                tm.registerTimer(newTimer, wfp);
                tni.internalSetTimerId(newTimer.getId());
                logger.debug("New timer {} successfully registered", newTimer);
                break;
            }
        } else if (nodeInstance instanceof StateBasedNodeInstance) {
            StateBasedNodeInstance sbni = (StateBasedNodeInstance) nodeInstance;
            List<Long> timerList = sbni.getTimerInstances();
            if (sbni.getNodeName().equals(timerName) || (timerList != null && timerList.contains(timerId))) {
                if (timerList != null && timerList.size() == 1) {
                    TimerInstance timer = tm.getTimerMap().get(timerList.get(0));
                    TimerInstance newTimer = rescheduleTimer(timer, tm);
                    logger.debug("New timer {} about to be registered", newTimer);
                    tm.registerTimer(newTimer, wfp);
                    timerList.clear();
                    timerList.add(newTimer.getId());
                    sbni.internalSetTimerInstances(timerList);
                    logger.debug("New timer {} successfully registered", newTimer);
                }
                break;
            }
        }
    }
    return null;
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) List(java.util.List) TimerManager(org.jbpm.process.instance.timer.TimerManager) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) 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