Search in sources :

Example 26 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 27 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 28 with TimerService

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

the class ExpireJobContextTimerInputMarshaller method read.

public void read(ProtobufMarshallerReaderContext inCtx) throws IOException, ClassNotFoundException {
    InternalFactHandle factHandle = inCtx.getHandles().get(inCtx.readLong());
    long nextTimeStamp = inCtx.readLong();
    TimerService clock = inCtx.getWorkingMemory().getTimerService();
    JobContext jobctx = new ExpireJobContext(new WorkingMemoryReteExpireAction((EventFactHandle) factHandle), inCtx.getWorkingMemory());
    JobHandle handle = clock.scheduleJob(job, jobctx, PointInTimeTrigger.createPointInTimeTrigger(nextTimeStamp, null));
    jobctx.setJobHandle(handle);
}
Also used : ExpireJobContext(org.drools.core.reteoo.ObjectTypeNode.ExpireJobContext) JobHandle(org.drools.core.time.JobHandle) EventFactHandle(org.drools.core.common.EventFactHandle) JobContext(org.drools.core.time.JobContext) ExpireJobContext(org.drools.core.reteoo.ObjectTypeNode.ExpireJobContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) WorkingMemoryReteExpireAction(org.drools.core.impl.WorkingMemoryReteExpireAction) TimerService(org.drools.core.time.TimerService)

Example 29 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)

Example 30 with TimerService

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

the class GlobalQuartzDBTimerServiceTest method testContinueGlobalTestService.

/**
 * Test that illustrates that jobs are persisted and survives server restart
 * and as soon as GlobalTimerService is active jobs are fired
 * NOTE: this test is disabled by default as it requires real db (not in memory)
 * and test to be executed separately each with new jvm process
 */
@Test
@Ignore
public void testContinueGlobalTestService() throws Exception {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycle2.bpmn2"), ResourceType.BPMN2).addConfiguration("drools.timerService", "org.jbpm.process.core.timer.impl.RegisteredTimerServiceDelegate").get();
    RuntimeManager manger = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    // build GlobalTimerService instance
    TimerService globalTs = new GlobalTimerService(manger, globalScheduler);
    // and register it in the registry under 'default' key
    TimerServiceRegistry.getInstance().registerTimerService("default", globalTs);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                timerExporations.add(event.getProcessInstance().getId());
            }
        }
    };
    Thread.sleep(5000);
}
Also used : RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) ArrayList(java.util.ArrayList) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) Ignore(org.junit.Ignore) Test(org.junit.Test)

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