Search in sources :

Example 1 with DeadlineSummary

use of org.kie.internal.task.api.model.DeadlineSummary in project jbpm by kiegroup.

the class ExecuteReminderCommand method execute.

@Override
public Void execute(Context context) {
    TaskContext ctx = (TaskContext) context;
    UserInfo userInfo = (UserInfo) context.get(EnvironmentName.TASK_USER_INFO);
    TaskPersistenceContext persistenceContext = ctx.getPersistenceContext();
    try {
        Task task = persistenceContext.findTask(taskId);
        TaskData taskData = task.getTaskData();
        List<DeadlineSummary> resultList = null;
        resultList = getAlldeadlines(persistenceContext, taskData);
        TaskEventSupport taskEventSupport = ctx.getTaskEventSupport();
        if (resultList == null || resultList.size() == 0) {
            if (taskData.getActualOwner() == null)
                return null;
            if (taskData != null) {
                // check if task is still in valid status
                if (DeadlineType.START.isValidStatus(taskData.getStatus()) || DeadlineType.END.isValidStatus(taskData.getStatus())) {
                    taskEventSupport.fireBeforeTaskNotified(task, ctx);
                    logger.debug("Sending an Email");
                    Map<String, Object> variables = getVariables(ctx, persistenceContext, task, taskData);
                    Notification notification = buildDefaultNotification(taskData, task);
                    NotificationListenerManager.get().broadcast(new NotificationEvent(notification, task, variables), userInfo);
                    taskEventSupport.fireAfterTaskNotified(task, ctx);
                }
            }
        } else {
            for (DeadlineSummary deadlineSummary : resultList) {
                executedeadLine(ctx, persistenceContext, task, deadlineSummary, taskData);
            }
        }
    } catch (Exception e) {
        logger.error("Error when executing deadlines", e);
    }
    return null;
}
Also used : Task(org.kie.api.task.model.Task) DeadlineSummary(org.kie.internal.task.api.model.DeadlineSummary) UserInfo(org.kie.internal.task.api.UserInfo) NotificationEvent(org.kie.internal.task.api.model.NotificationEvent) Notification(org.kie.internal.task.api.model.Notification) EmailNotification(org.kie.internal.task.api.model.EmailNotification) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) TaskData(org.kie.api.task.model.TaskData) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport)

Example 2 with DeadlineSummary

use of org.kie.internal.task.api.model.DeadlineSummary in project jbpm by kiegroup.

the class InitDeadlinesCommand method execute.

@Override
public Void execute(Context context) {
    TaskContext ctx = (TaskContext) context;
    TaskPersistenceContext persistenceContext = ctx.getPersistenceContext();
    TaskDeadlinesService deadlineService = ctx.getTaskDeadlinesService();
    try {
        long now = System.currentTimeMillis();
        List<DeadlineSummary> resultList = persistenceContext.queryInTransaction("UnescalatedStartDeadlines", ClassUtil.<List<DeadlineSummary>>castClass(List.class));
        for (DeadlineSummary summary : resultList) {
            long delay = summary.getDate().getTime() - now;
            deadlineService.schedule(summary.getTaskId(), summary.getDeadlineId(), delay, DeadlineType.START);
        }
        resultList = persistenceContext.queryInTransaction("UnescalatedEndDeadlines", ClassUtil.<List<DeadlineSummary>>castClass(List.class));
        for (DeadlineSummary summary : resultList) {
            long delay = summary.getDate().getTime() - now;
            deadlineService.schedule(summary.getTaskId(), summary.getDeadlineId(), delay, DeadlineType.END);
        }
    } catch (Exception e) {
        logger.error("Error when executing deadlines", e);
    }
    return null;
}
Also used : TaskDeadlinesService(org.kie.internal.task.api.TaskDeadlinesService) DeadlineSummary(org.kie.internal.task.api.model.DeadlineSummary) List(java.util.List) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 3 with DeadlineSummary

use of org.kie.internal.task.api.model.DeadlineSummary 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

DeadlineSummary (org.kie.internal.task.api.model.DeadlineSummary)3 List (java.util.List)2 Task (org.kie.api.task.model.Task)2 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 JobHandle (org.drools.core.time.JobHandle)1 TimerService (org.drools.core.time.TimerService)1 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)1 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)1 TaskData (org.kie.api.task.model.TaskData)1 TaskDeadlinesService (org.kie.internal.task.api.TaskDeadlinesService)1 UserInfo (org.kie.internal.task.api.UserInfo)1 Deadline (org.kie.internal.task.api.model.Deadline)1 Deadlines (org.kie.internal.task.api.model.Deadlines)1 EmailNotification (org.kie.internal.task.api.model.EmailNotification)1 InternalTask (org.kie.internal.task.api.model.InternalTask)1 Notification (org.kie.internal.task.api.model.Notification)1 NotificationEvent (org.kie.internal.task.api.model.NotificationEvent)1