Search in sources :

Example 1 with Trigger

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

the class PhreakTimerNode method scheduleLeftTuple.

private void scheduleLeftTuple(final TimerNode timerNode, final TimerNodeMemory tm, final PathMemory pmem, final SegmentMemory smem, final LeftTupleSink sink, final InternalAgenda agenda, final Timer timer, final TimerService timerService, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    InternalWorkingMemory wm = agenda.getWorkingMemory();
    if (leftTuple.getPropagationContext().getReaderContext() == null) {
        final Trigger trigger = createTrigger(timerNode, wm, timer, timestamp, calendarNames, calendars, leftTuple);
        // regular propagation
        scheduleTimer(timerNode, tm, smem, sink, wm, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, trigger);
    } else {
        // de-serializing, so we need to correlate timers before scheduling them
        Scheduler scheduler = new Scheduler() {

            @Override
            public void schedule(Trigger t) {
                scheduleTimer(timerNode, tm, smem, sink, wm, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, t);
                evaluate(pmem, agenda, sink, tm, trgLeftTuples);
            }

            @Override
            public Trigger getTrigger() {
                return createTrigger(timerNode, wm, timer, timestamp, calendarNames, calendars, leftTuple);
            }
        };
        TupleKey key = PersisterHelper.createTupleKey(leftTuple);
        leftTuple.getPropagationContext().getReaderContext().addTimerNodeScheduler(timerNode.getId(), key, scheduler);
        leftTuple.setContextObject(key);
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Trigger(org.drools.core.time.Trigger) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)

Example 2 with Trigger

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

the class ProtobufOutputMarshaller method writeTrigger.

public static ProtobufMessages.Trigger writeTrigger(Trigger trigger, MarshallerWriteContext outCtx) {
    if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        ProtobufMessages.Trigger.CronTrigger.Builder _cron = ProtobufMessages.Trigger.CronTrigger.newBuilder().setStartTime(cronTrigger.getStartTime().getTime()).setRepeatLimit(cronTrigger.getRepeatLimit()).setRepeatCount(cronTrigger.getRepeatCount()).setCronExpression(cronTrigger.getCronEx().getCronExpression());
        if (cronTrigger.getEndTime() != null) {
            _cron.setEndTime(cronTrigger.getEndTime().getTime());
        }
        if (cronTrigger.getNextFireTime() != null) {
            _cron.setNextFireTime(cronTrigger.getNextFireTime().getTime());
        }
        if (cronTrigger.getCalendarNames() != null) {
            for (String calendarName : cronTrigger.getCalendarNames()) {
                _cron.addCalendarName(calendarName);
            }
        }
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.CRON).setCron(_cron.build()).build();
    } else if (trigger instanceof IntervalTrigger) {
        IntervalTrigger intTrigger = (IntervalTrigger) trigger;
        ProtobufMessages.Trigger.IntervalTrigger.Builder _interval = ProtobufMessages.Trigger.IntervalTrigger.newBuilder().setStartTime(intTrigger.getStartTime().getTime()).setRepeatLimit(intTrigger.getRepeatLimit()).setRepeatCount(intTrigger.getRepeatCount()).setPeriod(intTrigger.getPeriod());
        if (intTrigger.getEndTime() != null) {
            _interval.setEndTime(intTrigger.getEndTime().getTime());
        }
        if (intTrigger.getNextFireTime() != null) {
            _interval.setNextFireTime(intTrigger.getNextFireTime().getTime());
        }
        if (intTrigger.getCalendarNames() != null) {
            for (String calendarName : intTrigger.getCalendarNames()) {
                _interval.addCalendarName(calendarName);
            }
        }
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.INTERVAL).setInterval(_interval.build()).build();
    } else if (trigger instanceof PointInTimeTrigger) {
        PointInTimeTrigger pinTrigger = (PointInTimeTrigger) trigger;
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.POINT_IN_TIME).setPit(ProtobufMessages.Trigger.PointInTimeTrigger.newBuilder().setNextFireTime(pinTrigger.hasNextFireTime().getTime()).build()).build();
    } else if (trigger instanceof CompositeMaxDurationTrigger) {
        CompositeMaxDurationTrigger cmdTrigger = (CompositeMaxDurationTrigger) trigger;
        ProtobufMessages.Trigger.CompositeMaxDurationTrigger.Builder _cmdt = ProtobufMessages.Trigger.CompositeMaxDurationTrigger.newBuilder();
        if (cmdTrigger.getMaxDurationTimestamp() != null) {
            _cmdt.setMaxDurationTimestamp(cmdTrigger.getMaxDurationTimestamp().getTime());
        }
        if (cmdTrigger.getTimerCurrentDate() != null) {
            _cmdt.setTimerCurrentDate(cmdTrigger.getTimerCurrentDate().getTime());
        }
        if (cmdTrigger.getTimerTrigger() != null) {
            _cmdt.setTimerTrigger(writeTrigger(cmdTrigger.getTimerTrigger(), outCtx));
        }
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.COMPOSITE_MAX_DURATION).setCmdt(_cmdt.build()).build();
    }
    throw new RuntimeException("Unable to serialize Trigger for type: " + trigger.getClass());
}
Also used : CompositeMaxDurationTrigger(org.drools.core.time.impl.CompositeMaxDurationTrigger) CronTrigger(org.drools.core.time.impl.CronTrigger) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Trigger(org.drools.core.time.Trigger) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) CompositeMaxDurationTrigger(org.drools.core.time.impl.CompositeMaxDurationTrigger) CronTrigger(org.drools.core.time.impl.CronTrigger) Builder(org.drools.core.marshalling.impl.ProtobufMessages.ProcessData.Builder) ByteString(com.google.protobuf.ByteString) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger)

Example 3 with Trigger

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

the class TaskDeadlinesServiceImpl method schedule.

public void schedule(long taskId, long deadlineId, long delay, DeadlineType type) {
    Task task = persistenceContext.findTask(taskId);
    String deploymentId = task.getTaskData().getDeploymentId();
    TimerService timerService = TimerServiceRegistry.getInstance().get(deploymentId + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    if (timerService != null && timerService instanceof GlobalTimerService) {
        TaskDeadlineJob deadlineJob = new TaskDeadlineJob(taskId, deadlineId, type, deploymentId, task.getTaskData().getProcessInstanceId());
        Trigger trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, -1, delay, 0, null, null);
        JobHandle handle = timerService.scheduleJob(deadlineJob, new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId), trigger);
        logger.debug("scheduling timer job for deadline {} and task {}  using timer service {}", deadlineJob.getId(), taskId, timerService);
        jobHandles.put(deadlineJob.getId(), handle);
    } else {
        ScheduledFuture<ScheduledTaskDeadline> scheduled = scheduler.schedule(new ScheduledTaskDeadline(taskId, deadlineId, type, deploymentId, task.getTaskData().getProcessInstanceId()), delay, TimeUnit.MILLISECONDS);
        List<ScheduledFuture<ScheduledTaskDeadline>> knownFutures = null;
        if (type == DeadlineType.START) {
            knownFutures = startScheduledTaskDeadlines.get(taskId);
        } else if (type == DeadlineType.END) {
            knownFutures = endScheduledTaskDeadlines.get(taskId);
        }
        if (knownFutures == null) {
            knownFutures = new CopyOnWriteArrayList<ScheduledFuture<ScheduledTaskDeadline>>();
        }
        knownFutures.add(scheduled);
        if (type == DeadlineType.START) {
            startScheduledTaskDeadlines.put(taskId, knownFutures);
        } else if (type == DeadlineType.END) {
            endScheduledTaskDeadlines.put(taskId, knownFutures);
        }
    }
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) ScheduledFuture(java.util.concurrent.ScheduledFuture) JobHandle(org.drools.core.time.JobHandle) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Trigger(org.drools.core.time.Trigger) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Example 4 with Trigger

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

the class ProtobufOutputMarshaller method writeTrigger.

public static ProtobufMessages.Trigger writeTrigger(Trigger trigger, MarshallerWriteContext outCtx) {
    if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        ProtobufMessages.Trigger.CronTrigger.Builder _cron = ProtobufMessages.Trigger.CronTrigger.newBuilder().setStartTime(cronTrigger.getStartTime().getTime()).setRepeatLimit(cronTrigger.getRepeatLimit()).setRepeatCount(cronTrigger.getRepeatCount()).setCronExpression(cronTrigger.getCronEx().getCronExpression());
        if (cronTrigger.getEndTime() != null) {
            _cron.setEndTime(cronTrigger.getEndTime().getTime());
        }
        if (cronTrigger.getNextFireTime() != null) {
            _cron.setNextFireTime(cronTrigger.getNextFireTime().getTime());
        }
        if (cronTrigger.getCalendarNames() != null) {
            for (String calendarName : cronTrigger.getCalendarNames()) {
                _cron.addCalendarName(calendarName);
            }
        }
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.CRON).setCron(_cron.build()).build();
    } else if (trigger instanceof IntervalTrigger) {
        IntervalTrigger intTrigger = (IntervalTrigger) trigger;
        ProtobufMessages.Trigger.IntervalTrigger.Builder _interval = ProtobufMessages.Trigger.IntervalTrigger.newBuilder().setStartTime(intTrigger.getStartTime().getTime()).setRepeatLimit(intTrigger.getRepeatLimit()).setRepeatCount(intTrigger.getRepeatCount()).setPeriod(intTrigger.getPeriod());
        if (intTrigger.getEndTime() != null) {
            _interval.setEndTime(intTrigger.getEndTime().getTime());
        }
        if (intTrigger.getNextFireTime() != null) {
            _interval.setNextFireTime(intTrigger.getNextFireTime().getTime());
        }
        if (intTrigger.getCalendarNames() != null) {
            for (String calendarName : intTrigger.getCalendarNames()) {
                _interval.addCalendarName(calendarName);
            }
        }
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.INTERVAL).setInterval(_interval.build()).build();
    } else if (trigger instanceof PointInTimeTrigger) {
        PointInTimeTrigger pitTrigger = (PointInTimeTrigger) trigger;
        Date nextFireTime = pitTrigger.hasNextFireTime();
        // There is no reason to serialize a timer when it has no future execution time.
        if (nextFireTime != null) {
            return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.POINT_IN_TIME).setPit(ProtobufMessages.Trigger.PointInTimeTrigger.newBuilder().setNextFireTime(nextFireTime.getTime()).build()).build();
        } else {
            return null;
        }
    } else if (trigger instanceof CompositeMaxDurationTrigger) {
        CompositeMaxDurationTrigger cmdTrigger = (CompositeMaxDurationTrigger) trigger;
        ProtobufMessages.Trigger.CompositeMaxDurationTrigger.Builder _cmdt = ProtobufMessages.Trigger.CompositeMaxDurationTrigger.newBuilder();
        if (cmdTrigger.getMaxDurationTimestamp() != null) {
            _cmdt.setMaxDurationTimestamp(cmdTrigger.getMaxDurationTimestamp().getTime());
        }
        if (cmdTrigger.getTimerCurrentDate() != null) {
            _cmdt.setTimerCurrentDate(cmdTrigger.getTimerCurrentDate().getTime());
        }
        if (cmdTrigger.getTimerTrigger() != null) {
            ProtobufMessages.Trigger timerTrigger = writeTrigger(cmdTrigger.getTimerTrigger(), outCtx);
            if (timerTrigger != null) {
                _cmdt.setTimerTrigger(timerTrigger);
            }
        }
        return ProtobufMessages.Trigger.newBuilder().setType(ProtobufMessages.Trigger.TriggerType.COMPOSITE_MAX_DURATION).setCmdt(_cmdt.build()).build();
    }
    throw new RuntimeException("Unable to serialize Trigger for type: " + trigger.getClass());
}
Also used : CompositeMaxDurationTrigger(org.drools.core.time.impl.CompositeMaxDurationTrigger) CronTrigger(org.drools.core.time.impl.CronTrigger) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Trigger(org.drools.core.time.Trigger) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) CompositeMaxDurationTrigger(org.drools.core.time.impl.CompositeMaxDurationTrigger) CronTrigger(org.drools.core.time.impl.CronTrigger) Builder(org.drools.serialization.protobuf.ProtobufMessages.ProcessData.Builder) ByteString(com.google.protobuf.ByteString) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Date(java.util.Date)

Example 5 with Trigger

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

the class PhreakTimerNode method scheduleLeftTuple.

private void scheduleLeftTuple(final TimerNode timerNode, final TimerNodeMemory tm, final PathMemory pmem, final SegmentMemory smem, final LeftTupleSink sink, final ActivationsManager activationsManager, final Timer timer, final TimerService timerService, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    ReteEvaluator reteEvaluator = activationsManager.getReteEvaluator();
    if (leftTuple.getPropagationContext().getReaderContext() == null) {
        final Trigger trigger = createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
        // regular propagation
        scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, trigger);
    } else {
        // de-serializing, so we need to correlate timers before scheduling them
        Scheduler scheduler = new Scheduler() {

            @Override
            public void schedule(Trigger t) {
                scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, t);
                evaluate(pmem, activationsManager, sink, tm, trgLeftTuples);
            }

            @Override
            public Trigger getTrigger() {
                return createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
            }
        };
        TupleKey key = TupleKey.createTupleKey(leftTuple);
        leftTuple.getPropagationContext().getReaderContext().addTimerNodeScheduler(timerNode.getId(), key, scheduler);
        leftTuple.setContextObject(key);
    }
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Trigger(org.drools.core.time.Trigger) TupleKey(org.drools.core.marshalling.TupleKey)

Aggregations

Trigger (org.drools.core.time.Trigger)14 Date (java.util.Date)6 IntervalTrigger (org.drools.core.time.impl.IntervalTrigger)6 CronTrigger (org.drools.core.time.impl.CronTrigger)5 Test (org.junit.Test)5 JobHandle (org.drools.core.time.JobHandle)4 TimerService (org.drools.core.time.TimerService)4 SessionConfiguration (org.drools.core.SessionConfiguration)3 ByteString (com.google.protobuf.ByteString)2 TupleKey (org.drools.core.marshalling.TupleKey)2 CompositeMaxDurationTrigger (org.drools.core.time.impl.CompositeMaxDurationTrigger)2 PointInTimeTrigger (org.drools.core.time.impl.PointInTimeTrigger)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 TupleKey (org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)1 Builder (org.drools.core.marshalling.impl.ProtobufMessages.ProcessData.Builder)1 PhreakTimerNode (org.drools.core.phreak.PhreakTimerNode)1 Builder (org.drools.serialization.protobuf.ProtobufMessages.ProcessData.Builder)1 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)1