Search in sources :

Example 1 with JobHandle

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

the class PseudoClockSchedulerTest method removeExistingJobWhenMultipleQueued.

@Test
public void removeExistingJobWhenMultipleQueued() {
    final Date triggerTime_1 = new Date(1000);
    final Date triggerTime_2 = new Date(2000);
    when(mockTrigger_1.hasNextFireTime()).thenReturn(triggerTime_1);
    when(mockTrigger_2.hasNextFireTime()).thenReturn(triggerTime_2);
    JobHandle jobHandle_1 = scheduler.scheduleJob(mockJob_1, this.mockContext_1, mockTrigger_1);
    JobHandle jobHandle_2 = scheduler.scheduleJob(mockJob_2, this.mockContext_2, mockTrigger_2);
    assertThat(scheduler.getTimeToNextJob(), is(triggerTime_1.getTime()));
    scheduler.removeJob(jobHandle_1);
    assertThat(scheduler.getTimeToNextJob(), is(triggerTime_2.getTime()));
    scheduler.removeJob(jobHandle_2);
    assertThat(scheduler.getTimeToNextJob(), is(-1L));
    verify(mockTrigger_1, atLeastOnce()).hasNextFireTime();
    verify(mockTrigger_2, atLeastOnce()).hasNextFireTime();
}
Also used : JobHandle(org.drools.core.time.JobHandle) Date(java.util.Date) Test(org.junit.Test)

Example 2 with JobHandle

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

the class PseudoClockSchedulerTest method removeExistingJob.

@Test
public void removeExistingJob() {
    final Date triggerTime = new Date(1000);
    when(mockTrigger_1.hasNextFireTime()).thenReturn(triggerTime);
    JobHandle jobHandle = scheduler.scheduleJob(mockJob_1, this.mockContext_1, mockTrigger_1);
    assertThat(scheduler.getTimeToNextJob(), is(triggerTime.getTime()));
    scheduler.removeJob(jobHandle);
    assertThat(scheduler.getTimeToNextJob(), is(-1L));
    verify(mockTrigger_1, atLeastOnce()).hasNextFireTime();
}
Also used : JobHandle(org.drools.core.time.JobHandle) Date(java.util.Date) Test(org.junit.Test)

Example 3 with JobHandle

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

the class TaskDeadlinesServiceImpl method unschedule.

public void unschedule(long taskId, Deadline deadline, 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) {
        TaskDeadlineJob deadlineJob = new TaskDeadlineJob(taskId, deadline.getId(), type, deploymentId, task.getTaskData().getProcessInstanceId());
        logger.debug("unscheduling timer job for deadline {} {} and task {}  using timer service {}", deadlineJob.getId(), deadline.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
        deadline.setEscalated(true);
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) Deadlines(org.kie.internal.task.api.model.Deadlines) InternalTask(org.kie.internal.task.api.model.InternalTask) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Example 4 with JobHandle

use of org.drools.core.time.JobHandle 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 5 with JobHandle

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

the class EventFactHandle method unscheduleAllJobs.

public void unscheduleAllJobs(InternalWorkingMemory workingMemory) {
    if (!jobs.isEmpty()) {
        synchronized (jobs) {
            TimerService clock = workingMemory.getTimerService();
            while (!jobs.isEmpty()) {
                JobHandle job = jobs.removeFirst();
                clock.removeJob(job);
            }
        }
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) TimerService(org.drools.core.time.TimerService)

Aggregations

JobHandle (org.drools.core.time.JobHandle)10 Date (java.util.Date)5 TimerService (org.drools.core.time.TimerService)5 Trigger (org.drools.core.time.Trigger)4 IntervalTrigger (org.drools.core.time.impl.IntervalTrigger)4 CronTrigger (org.drools.core.time.impl.CronTrigger)3 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)3 Task (org.kie.api.task.model.Task)3 InternalTask (org.kie.internal.task.api.model.InternalTask)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 Test (org.junit.Test)2 Deadlines (org.kie.internal.task.api.model.Deadlines)2 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 JobContext (org.drools.core.time.JobContext)1 PointInTimeTrigger (org.drools.core.time.impl.PointInTimeTrigger)1 Deadline (org.kie.internal.task.api.model.Deadline)1 DeadlineSummary (org.kie.internal.task.api.model.DeadlineSummary)1