Search in sources :

Example 1 with TimerService

use of org.drools.core.time.TimerService in project drools by kiegroup.

the class PhreakTimerNode method doLeftDeletes.

public void doLeftDeletes(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TimerService timerService = agenda.getWorkingMemory().getTimerService();
    TupleList leftTuples = tm.getInsertOrUpdateLeftTuples();
    TupleList deletes = tm.getDeleteLeftTuples();
    if (!deletes.isEmpty()) {
        for (LeftTuple leftTuple = (LeftTuple) deletes.getFirst(); leftTuple != null; ) {
            LeftTuple next = (LeftTuple) leftTuple.getNext();
            srcLeftTuples.addDelete(leftTuple);
            if (log.isTraceEnabled()) {
                log.trace("Timer Add Postponed Delete {}", leftTuple);
            }
            leftTuple.clear();
            leftTuple = next;
        }
        deletes.clear();
    }
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext pctx = leftTuple.getPropagationContext();
        Object obj = leftTuple.getContextObject();
        if (obj instanceof DefaultJobHandle) {
            timerService.removeJob((DefaultJobHandle) obj);
        } else if (obj instanceof TupleKey && pctx.getReaderContext() != null) {
            pctx.getReaderContext().removeTimerNodeScheduler(timerNode.getId(), (TupleKey) obj);
        }
        pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
        if (leftTuple.getMemory() != null) {
            // it gets removed either way.
            leftTuples.remove(leftTuple);
            if (pctx.getFactHandle().isExpired()) {
                // a expire clashes with insert or update, allow it to propagate once, will handle the expire the second time around
                doPropagateChildLeftTuple(sink, trgLeftTuples, stagedLeftTuples, leftTuple);
                tm.getDeleteLeftTuples().add(leftTuple);
                // make sure it's dirty, so it'll evaluate again
                pmem.doLinkRule(agenda);
                if (log.isTraceEnabled()) {
                    log.trace("Timer Postponed Delete {}", leftTuple);
                }
            }
        }
        if (leftTuple.getMemory() == null) {
            // if it's != null, then it's already been postponed, and the existing child propagated
            // only has one child
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            if (childLeftTuple != null) {
                childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
                RuleNetworkEvaluator.deleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                if (log.isTraceEnabled()) {
                    log.trace("Timer Delete {}", leftTuple);
                }
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : TupleList(org.drools.core.util.index.TupleList) PropagationContext(org.drools.core.spi.PropagationContext) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey) LeftTuple(org.drools.core.reteoo.LeftTuple) TimerService(org.drools.core.time.TimerService) DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Example 2 with TimerService

use of org.drools.core.time.TimerService in project drools by kiegroup.

the class PhreakTimerNode method doLeftUpdates.

public void doLeftUpdates(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, SegmentMemory smem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    Timer timer = timerNode.getTimer();
    // Variables may have changed for ExpressionIntervalTimer, so it must be rescheduled
    TimerService timerService = agenda.getWorkingMemory().getTimerService();
    long timestamp = timerService.getCurrentTime();
    String[] calendarNames = timerNode.getCalendarNames();
    Calendars calendars = agenda.getWorkingMemory().getCalendars();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        DefaultJobHandle jobHandle = (DefaultJobHandle) leftTuple.getContextObject();
        if (jobHandle != null) {
            // jobHandle can be null, if the time fired straight away, and never ended up scheduling a job
            timerService.removeJob(jobHandle);
        }
        scheduleLeftTuple(timerNode, tm, pmem, smem, sink, agenda, timer, timerService, timestamp, calendarNames, calendars, leftTuple, trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : TimerNodeTimer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.TimerNodeTimer) Timer(org.drools.core.time.impl.Timer) Calendars(org.kie.api.runtime.Calendars) LeftTuple(org.drools.core.reteoo.LeftTuple) TimerService(org.drools.core.time.TimerService) DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Example 3 with TimerService

use of org.drools.core.time.TimerService in project drools by kiegroup.

the class PhreakTimerNode method doLeftInserts.

public void doLeftInserts(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, SegmentMemory smem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    Timer timer = timerNode.getTimer();
    TimerService timerService = agenda.getWorkingMemory().getTimerService();
    long timestamp = timerService.getCurrentTime();
    String[] calendarNames = timerNode.getCalendarNames();
    Calendars calendars = agenda.getWorkingMemory().getCalendars();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        scheduleLeftTuple(timerNode, tm, pmem, smem, sink, agenda, timer, timerService, timestamp, calendarNames, calendars, leftTuple, trgLeftTuples, null);
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : TimerNodeTimer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.TimerNodeTimer) Timer(org.drools.core.time.impl.Timer) Calendars(org.kie.api.runtime.Calendars) LeftTuple(org.drools.core.reteoo.LeftTuple) TimerService(org.drools.core.time.TimerService)

Example 4 with TimerService

use of org.drools.core.time.TimerService in project jbpm by kiegroup.

the class RuntimeManagerFactoryImpl method initTimerService.

protected void initTimerService(RuntimeEnvironment environment, RuntimeManager manager) {
    if (environment instanceof SchedulerProvider) {
        GlobalSchedulerService schedulerService = ((SchedulerProvider) environment).getSchedulerService();
        if (schedulerService != null) {
            TimerService globalTs = new GlobalTimerService(manager, schedulerService);
            String timerServiceId = manager.getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX;
            // and register it in the registry under 'default' key
            TimerServiceRegistry.getInstance().registerTimerService(timerServiceId, globalTs);
            ((SimpleRuntimeEnvironment) environment).addToConfiguration("drools.timerService", "new org.jbpm.process.core.timer.impl.RegisteredTimerServiceDelegate(\"" + timerServiceId + "\")");
            if (!schedulerService.isTransactional()) {
                schedulerService.setInterceptor(new TransactionAwareSchedulerServiceInterceptor(environment, manager, schedulerService));
            }
        }
    }
}
Also used : GlobalSchedulerService(org.jbpm.process.core.timer.GlobalSchedulerService) SchedulerProvider(org.jbpm.runtime.manager.api.SchedulerProvider) TransactionAwareSchedulerServiceInterceptor(org.jbpm.runtime.manager.impl.tx.TransactionAwareSchedulerServiceInterceptor) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Example 5 with TimerService

use of org.drools.core.time.TimerService in project jbpm by kiegroup.

the class PerCaseRuntimeManager method disposeRuntimeEngine.

@Override
public void disposeRuntimeEngine(RuntimeEngine runtime) {
    if (isClosed()) {
        throw new IllegalStateException("Runtime manager " + identifier + " is already closed");
    }
    try {
        if (canDispose(runtime)) {
            removeLocalRuntime(runtime);
            Long ksessionId = ((RuntimeEngineImpl) runtime).getKieSessionId();
            releaseAndCleanLock(ksessionId, runtime);
            if (runtime instanceof Disposable) {
                // special handling for in memory to not allow to dispose if there is any context in the mapper
                if (mapper instanceof InMemoryMapper && ((InMemoryMapper) mapper).hasContext(ksessionId)) {
                    return;
                }
                ((Disposable) runtime).dispose();
            }
            if (ksessionId != null) {
                TimerService timerService = TimerServiceRegistry.getInstance().get(getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
                if (timerService != null) {
                    if (timerService instanceof GlobalTimerService) {
                        ((GlobalTimerService) timerService).clearTimerJobInstances(ksessionId);
                    }
                }
            }
        }
    } catch (Exception e) {
        releaseAndCleanLock(runtime);
        removeLocalRuntime(runtime);
        throw new RuntimeException(e);
    }
}
Also used : Disposable(org.kie.internal.runtime.manager.Disposable) InMemoryMapper(org.jbpm.runtime.manager.impl.mapper.InMemoryMapper) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Aggregations

TimerService (org.drools.core.time.TimerService)32 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)11 JobHandle (org.drools.core.time.JobHandle)9 Test (org.junit.Test)9 LeftTuple (org.drools.core.reteoo.LeftTuple)6 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)5 ArrayList (java.util.ArrayList)4 EventFactHandle (org.drools.core.common.EventFactHandle)4 PropagationContext (org.drools.core.spi.PropagationContext)4 JobContext (org.drools.core.time.JobContext)4 Trigger (org.drools.core.time.Trigger)4 DefaultJobHandle (org.drools.core.time.impl.DefaultJobHandle)4 Timer (org.drools.core.time.impl.Timer)4 TimerJobInstance (org.drools.core.time.impl.TimerJobInstance)4 Calendars (org.kie.api.runtime.Calendars)4 KieSession (org.kie.api.runtime.KieSession)4 UserTransaction (javax.transaction.UserTransaction)3 SessionConfiguration (org.drools.core.SessionConfiguration)3 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3