Search in sources :

Example 16 with TimerService

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

the class JDKTimerServiceTest method testRepeatedExecutionJob.

@Test
public void testRepeatedExecutionJob() throws Exception {
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.REALTIME_CLOCK);
    TimerService timeService = TimerServiceFactory.getTimerService(config);
    Trigger trigger = new DelayedTrigger(new long[] { 100, 100, 100 });
    HelloWorldJobContext ctx = new HelloWorldJobContext("hello world", timeService);
    timeService.scheduleJob(new HelloWorldJob(), ctx, trigger);
    Thread.sleep(500);
    timeService.shutdown();
    assertEquals(3, ctx.getList().size());
}
Also used : Trigger(org.drools.core.time.Trigger) SessionConfiguration(org.drools.core.SessionConfiguration) TimerService(org.drools.core.time.TimerService) Test(org.junit.Test)

Example 17 with TimerService

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

the class JDKTimerServiceTest method testSingleExecutionJob.

@Test
public void testSingleExecutionJob() throws Exception {
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.REALTIME_CLOCK);
    TimerService timeService = TimerServiceFactory.getTimerService(config);
    Trigger trigger = new DelayedTrigger(100);
    HelloWorldJobContext ctx = new HelloWorldJobContext("hello world", timeService);
    timeService.scheduleJob(new HelloWorldJob(), ctx, trigger);
    Thread.sleep(500);
    timeService.shutdown();
    assertEquals(1, ctx.getList().size());
}
Also used : Trigger(org.drools.core.time.Trigger) SessionConfiguration(org.drools.core.SessionConfiguration) TimerService(org.drools.core.time.TimerService) Test(org.junit.Test)

Example 18 with TimerService

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

the class SlidingTimeWindow method expireFacts.

public void expireFacts(final Object context, final PropagationContext pctx, final InternalWorkingMemory workingMemory) {
    TimerService clock = workingMemory.getTimerService();
    long currentTime = clock.getCurrentTime();
    SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
    EventFactHandle handle = queue.peek();
    while (handle != null && isExpired(currentTime, handle)) {
        queue.setExpiringHandle(handle);
        queue.remove();
        if (handle.isValid()) {
            // if not expired yet, expire it
            final PropagationContext expiresPctx = createPropagationContextForFact(workingMemory, handle, PropagationContext.Type.EXPIRATION);
            ObjectTypeNode.doRetractObject(handle, expiresPctx, workingMemory);
        }
        queue.setExpiringHandle(null);
        handle = queue.peek();
    }
    // update next expiration time
    updateNextExpiration(handle, workingMemory, queue, nodeId);
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) EventFactHandle(org.drools.core.common.EventFactHandle) TimerService(org.drools.core.time.TimerService)

Example 19 with TimerService

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

the class SlidingTimeWindow method updateNextExpiration.

protected void updateNextExpiration(final InternalFactHandle fact, final InternalWorkingMemory workingMemory, final Behavior.Context context, final int nodeId) {
    TimerService clock = workingMemory.getTimerService();
    if (fact != null) {
        long nextTimestamp = ((EventFactHandle) fact).getStartTimestamp() + getSize();
        if (nextTimestamp < clock.getCurrentTime()) {
            // Past and out-of-order events should not be insert,
            // but the engine silently accepts them anyway, resulting in possibly undesirable behaviors
            workingMemory.queueWorkingMemoryAction(new BehaviorExpireWMAction(nodeId, this, context));
        } else {
            JobContext jobctx = new BehaviorJobContext(nodeId, workingMemory, this, context);
            JobHandle handle = clock.scheduleJob(job, jobctx, new PointInTimeTrigger(nextTimestamp, null, null));
            jobctx.setJobHandle(handle);
        }
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) JobContext(org.drools.core.time.JobContext) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) TimerService(org.drools.core.time.TimerService)

Example 20 with TimerService

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

the class TaskDeadlinesServiceImpl method unschedule.

public void unschedule(long taskId, DeadlineType type) {
    Task task = persistenceContext.findTask(taskId);
    String deploymentId = task.getTaskData().getDeploymentId();
    Deadlines deadlines = ((InternalTask) task).getDeadlines();
    TimerService timerService = TimerServiceRegistry.getInstance().get(deploymentId + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    if (timerService != null && timerService instanceof GlobalTimerService) {
        if (type == DeadlineType.START) {
            List<Deadline> startDeadlines = deadlines.getStartDeadlines();
            List<DeadlineSummary> resultList = (List<DeadlineSummary>) persistenceContext.queryWithParametersInTransaction("UnescalatedStartDeadlinesByTaskId", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummary>>castClass(List.class));
            for (DeadlineSummary summary : resultList) {
                TaskDeadlineJob deadlineJob = new TaskDeadlineJob(summary.getTaskId(), summary.getDeadlineId(), DeadlineType.START, deploymentId, task.getTaskData().getProcessInstanceId());
                logger.debug("unscheduling timer job for deadline {} {} and task {}  using timer service {}", deadlineJob.getId(), summary.getDeadlineId(), taskId, timerService);
                JobHandle jobHandle = jobHandles.remove(deadlineJob.getId());
                if (jobHandle == null) {
                    jobHandle = ((GlobalTimerService) timerService).buildJobHandleForContext(new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId));
                }
                timerService.removeJob(jobHandle);
                // mark the deadlines so they won't be rescheduled again
                for (Deadline deadline : startDeadlines) {
                    if (deadline.getId() == summary.getDeadlineId()) {
                        deadline.setEscalated(true);
                    }
                }
            }
        } else if (type == DeadlineType.END) {
            List<Deadline> endDeadlines = deadlines.getStartDeadlines();
            List<DeadlineSummary> resultList = (List<DeadlineSummary>) persistenceContext.queryWithParametersInTransaction("UnescalatedEndDeadlinesByTaskId", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummary>>castClass(List.class));
            for (DeadlineSummary summary : resultList) {
                TaskDeadlineJob deadlineJob = new TaskDeadlineJob(summary.getTaskId(), summary.getDeadlineId(), DeadlineType.END, deploymentId, task.getTaskData().getProcessInstanceId());
                logger.debug("unscheduling timer job for deadline {} and task {}  using timer service {}", deadlineJob.getId(), taskId, timerService);
                JobHandle jobHandle = jobHandles.remove(deadlineJob.getId());
                if (jobHandle == null) {
                    jobHandle = ((GlobalTimerService) timerService).buildJobHandleForContext(new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId));
                }
                timerService.removeJob(jobHandle);
                // mark the deadlines so they won't be rescheduled again
                for (Deadline deadline : endDeadlines) {
                    if (deadline.getId() == summary.getDeadlineId()) {
                        deadline.setEscalated(true);
                    }
                }
            }
        }
    } else {
        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) {
            return;
        }
        Iterator<ScheduledFuture<ScheduledTaskDeadline>> it = knownFutures.iterator();
        while (it.hasNext()) {
            ScheduledFuture<ScheduledTaskDeadline> scheduled = it.next();
            try {
                if (!scheduled.isDone() && !scheduled.isCancelled()) {
                    scheduled.cancel(true);
                }
            } catch (Exception e) {
                logger.error("Error while cancelling scheduled deadline task for Task with id {} -> {}", taskId, e);
            }
        }
    }
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) InternalTask(org.kie.internal.task.api.model.InternalTask) DeadlineSummary(org.kie.internal.task.api.model.DeadlineSummary) Deadline(org.kie.internal.task.api.model.Deadline) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) ScheduledFuture(java.util.concurrent.ScheduledFuture) JobHandle(org.drools.core.time.JobHandle) Deadlines(org.kie.internal.task.api.model.Deadlines) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Aggregations

TimerService (org.drools.core.time.TimerService)23 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)11 Test (org.junit.Test)9 JobHandle (org.drools.core.time.JobHandle)5 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)5 ArrayList (java.util.ArrayList)4 Trigger (org.drools.core.time.Trigger)4 TimerJobInstance (org.drools.core.time.impl.TimerJobInstance)4 KieSession (org.kie.api.runtime.KieSession)4 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)4 UserTransaction (javax.transaction.UserTransaction)3 SessionConfiguration (org.drools.core.SessionConfiguration)3 LeftTuple (org.drools.core.reteoo.LeftTuple)3 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)3 Task (org.kie.api.task.model.Task)3 Disposable (org.kie.internal.runtime.manager.Disposable)3 SessionNotFoundException (org.kie.internal.runtime.manager.SessionNotFoundException)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Properties (java.util.Properties)2