From 16c9843187e1952b304b92fc5d0721873dab580d Mon Sep 17 00:00:00 2001 From: Alberto Fanjul Date: Fri, 5 May 2023 08:30:13 +0200 Subject: [PATCH] [RHPAM-4698] Detect creation of timers for notifications --- .../instance/timer/TimerJobContext.java | 24 +++++++++++++++++++ .../process/instance/timer/TimerManager.java | 5 ++-- .../task/commands/InitDeadlinesCommand.java | 2 +- .../task/impl/TaskDeadlinesServiceImpl.java | 17 ++++++++++--- .../impl/util/DeadlineSchedulerHelper.java | 2 +- .../ejb/timer/EjbSchedulerService.java | 5 ++-- 6 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerJobContext.java diff --git a/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerJobContext.java b/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerJobContext.java new file mode 100644 index 0000000000..ba21712165 --- /dev/null +++ b/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerJobContext.java @@ -0,0 +1,24 @@ +/* + * Copyright 2017 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jbpm.process.instance.timer; + +/** + * + */ +public interface TimerJobContext { + boolean isNewTimer(); +} diff --git a/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerManager.java b/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerManager.java index 609c9fb986..db9dbc9a5e 100644 --- a/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerManager.java +++ b/jbpm-flow/src/main/java/org/jbpm/process/instance/timer/TimerManager.java @@ -375,7 +375,7 @@ public void execute(JobContext c) { } - public static class ProcessJobContext implements JobContext { + public static class ProcessJobContext implements JobContext, TimerJobContext { private static final long serialVersionUID = 476843895176221627L; private Long processInstanceId; @@ -444,7 +444,8 @@ public void setKnowledgeRuntime(InternalKnowledgeRuntime kruntime) { public InternalWorkingMemory getWorkingMemory() { return kruntime instanceof InternalWorkingMemory ? (InternalWorkingMemory)kruntime : null; } - + + @Override public boolean isNewTimer() { return newTimer; } diff --git a/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/commands/InitDeadlinesCommand.java b/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/commands/InitDeadlinesCommand.java index 26c8466f3a..eab21e3d7c 100644 --- a/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/commands/InitDeadlinesCommand.java +++ b/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/commands/InitDeadlinesCommand.java @@ -72,7 +72,7 @@ private void processDeadlines(String queryName, DeadlineType deadlineType, TaskP : persistenceContext.queryWithParametersInTransaction(queryName + "ByDeployment", Collections.singletonMap("deploymentId", deploymentId), resultClass); long now = System.currentTimeMillis(); for (DeadlineSummary summary : resultList) { - deadlineService.schedule(summary.getTaskId(), summary.getDeadlineId(), summary.getDate().getTime() - now, deadlineType); + deadlineService.schedule(summary.getTaskId(), summary.getDeadlineId(), summary.getDate().getTime() - now, deadlineType, false); } } diff --git a/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/TaskDeadlinesServiceImpl.java b/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/TaskDeadlinesServiceImpl.java index 163737a007..5d222a4637 100644 --- a/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/TaskDeadlinesServiceImpl.java +++ b/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/TaskDeadlinesServiceImpl.java @@ -37,6 +37,7 @@ import org.jbpm.process.core.timer.NamedJobContext; import org.jbpm.process.core.timer.TimerServiceRegistry; import org.jbpm.process.core.timer.impl.GlobalTimerService; +import org.jbpm.process.instance.timer.TimerJobContext; import org.jbpm.services.task.commands.ExecuteDeadlinesCommand; import org.jbpm.services.task.deadlines.NotificationListener; import org.jbpm.services.task.utils.ClassUtil; @@ -83,7 +84,7 @@ public void setPersistenceContext(TaskPersistenceContext persistenceContext) { } - public void schedule(long taskId, long deadlineId, long delay, DeadlineType type) { + public void schedule(long taskId, long deadlineId, long delay, DeadlineType type, boolean newTimer) { Task task = persistenceContext.findTask(taskId); String deploymentId = task.getTaskData().getDeploymentId(); @@ -99,7 +100,7 @@ public void schedule(long taskId, long deadlineId, long delay, DeadlineType type 0, null, null ) ; - JobHandle handle = timerService.scheduleJob(deadlineJob, new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId), trigger); + JobHandle handle = timerService.scheduleJob(deadlineJob, new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId, newTimer), trigger); jobHandles.put(deadlineJob.getId(), handle); } else { @@ -356,20 +357,30 @@ public String getId() { } } - private static class TaskDeadlineJobContext implements NamedJobContext { + private static class TaskDeadlineJobContext implements NamedJobContext, TimerJobContext { private static final long serialVersionUID = -6838102884655249845L; private JobHandle jobHandle; private String jobName; private Long processInstanceId; private String deploymentId; + private boolean newTimer; public TaskDeadlineJobContext(String jobName, Long processInstanceId, String deploymentId) { + this(jobName, processInstanceId, deploymentId, false); + } + + public TaskDeadlineJobContext(String jobName, Long processInstanceId, String deploymentId, boolean newTimer) { this.jobName = jobName; this.processInstanceId = processInstanceId; this.deploymentId = deploymentId; + this.newTimer = newTimer; } + @Override + public boolean isNewTimer() { + return newTimer; + } @Override public void setJobHandle(JobHandle jobHandle) { this.jobHandle = jobHandle; diff --git a/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/util/DeadlineSchedulerHelper.java b/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/util/DeadlineSchedulerHelper.java index 276d3d20c4..0467beffbf 100644 --- a/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/util/DeadlineSchedulerHelper.java +++ b/jbpm-human-task/jbpm-human-task-core/src/main/java/org/jbpm/services/task/impl/util/DeadlineSchedulerHelper.java @@ -138,7 +138,7 @@ public static void scheduleDeadlines(final List deadlines, f for (Deadline deadline : deadlines) { if (Boolean.FALSE.equals(deadline.isEscalated())) { Date date = deadline.getDate(); - deadlineService.schedule(taskId, deadline.getId(), date.getTime() - now, type); + deadlineService.schedule(taskId, deadline.getId(), date.getTime() - now, type, true); } } } diff --git a/jbpm-services/jbpm-services-ejb/jbpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/EjbSchedulerService.java b/jbpm-services/jbpm-services-ejb/jbpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/EjbSchedulerService.java index a6bc947c2b..8b34b15172 100644 --- a/jbpm-services/jbpm-services-ejb/jbpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/EjbSchedulerService.java +++ b/jbpm-services/jbpm-services-ejb/jbpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/EjbSchedulerService.java @@ -48,7 +48,7 @@ import org.jbpm.process.core.timer.impl.DelegateSchedulerServiceInterceptor; import org.jbpm.process.core.timer.impl.GlobalTimerService; import org.jbpm.process.core.timer.impl.GlobalTimerService.GlobalJobHandle; -import org.jbpm.process.instance.timer.TimerManager.ProcessJobContext; +import org.jbpm.process.instance.timer.TimerJobContext; import org.jbpm.runtime.manager.impl.jpa.EntityManagerFactoryManager; import org.jbpm.runtime.manager.impl.jpa.TimerMappingInfo; import org.kie.internal.runtime.manager.InternalRuntimeManager; @@ -267,7 +267,6 @@ protected String getJobName(JobContext ctx, long id) { } private boolean isNewTimer(JobContext ctx) { - return ctx instanceof ProcessJobContext && ((ProcessJobContext) ctx).isNewTimer(); + return ctx instanceof TimerJobContext && ((TimerJobContext) ctx).isNewTimer(); } - }