Skip to content

Commit

Permalink
[JBPM-10176] Detect creation of timers for notifications (#2283) (#2380)
Browse files Browse the repository at this point in the history
Co-authored-by: Alberto Fanjul <albertofanjul@gmail.com>
Co-authored-by: Alberto Fanjul <afanjula@redhat.com>
  • Loading branch information
3 people committed Jan 9, 2024
1 parent 8e917fb commit f4e97f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public static class ProcessJobContext implements JobContext {
private JobHandle jobHandle;
private Long sessionId;

private boolean newTimer;
private boolean isNew;

public ProcessJobContext(final TimerInstance timer, final Trigger trigger, final Long processInstanceId,
final InternalKnowledgeRuntime kruntime) {
Expand All @@ -408,17 +408,17 @@ public ProcessJobContext(final TimerInstance timer, final Trigger trigger, final
this.processInstanceId = processInstanceId;
this.kruntime = kruntime;
this.sessionId = timer.getSessionId();
this.newTimer = true;
this.isNew = true;
}

public ProcessJobContext(final TimerInstance timer, final Trigger trigger, final Long processInstanceId,
final InternalKnowledgeRuntime kruntime, boolean newTimer) {
final InternalKnowledgeRuntime kruntime, boolean isNew) {
this.timer = timer;
this.trigger = trigger;
this.processInstanceId = processInstanceId;
this.kruntime = kruntime;
this.sessionId = timer.getSessionId();
this.newTimer = newTimer;
this.isNew = isNew;
}

public Long getProcessInstanceId() {
Expand Down Expand Up @@ -457,9 +457,10 @@ public void setKnowledgeRuntime(InternalKnowledgeRuntime kruntime) {
public InternalWorkingMemory getWorkingMemory() {
return kruntime instanceof InternalWorkingMemory ? (InternalWorkingMemory)kruntime : null;
}

public boolean isNewTimer() {
return newTimer;

@Override
public boolean isNew() {
return isNew;
}
}

Expand Down Expand Up @@ -493,7 +494,7 @@ public void setParamaeters(Map<String, Object> paramaeters) {
}

@Override
public boolean isNewTimer() {
public boolean isNew() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ public void setPersistenceContext(TaskPersistenceContext persistenceContext) {
this.persistenceContext = persistenceContext;
}

public void scheduleNew(long taskId, long deadlineId, long delay, DeadlineType type) {
schedule(taskId, deadlineId, delay, type, true);
}

public void schedule(long taskId, long deadlineId, long delay, DeadlineType type) {
schedule(taskId, deadlineId, delay, type, false);
}

public void schedule(long taskId, long deadlineId, long delay, DeadlineType type, boolean isNew) {
Task task = persistenceContext.findTask(taskId);
String deploymentId = task.getTaskData().getDeploymentId();

Expand All @@ -99,7 +106,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, isNew), trigger);
jobHandles.put(deadlineJob.getId(), handle);

} else {
Expand Down Expand Up @@ -363,13 +370,24 @@ private static class TaskDeadlineJobContext implements NamedJobContext {
private String jobName;
private Long processInstanceId;
private String deploymentId;
private boolean isNew;

public TaskDeadlineJobContext(String jobName, Long processInstanceId, String deploymentId) {
this(jobName, processInstanceId, deploymentId, false);
}

public TaskDeadlineJobContext(String jobName, Long processInstanceId, String deploymentId, boolean isNew) {
this.jobName = jobName;
this.processInstanceId = processInstanceId;
this.deploymentId = deploymentId;
this.isNew = isNew;
}

@Override
public boolean isNew() {
return isNew;
}

@Override
public void setJobHandle(JobHandle jobHandle) {
this.jobHandle = jobHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void scheduleDeadlines(final List<? extends Deadline> 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.scheduleNew(taskId, deadline.getId(), date.getTime() - now, type);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
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.runtime.manager.impl.jpa.EntityManagerFactoryManager;
import org.jbpm.runtime.manager.impl.jpa.TimerMappingInfo;
import org.kie.internal.runtime.manager.InternalRuntimeManager;
Expand Down Expand Up @@ -76,7 +75,7 @@ public JobHandle scheduleJob(Job job, JobContext ctx, Trigger trigger) {
TimerJobInstance jobInstance = null;
// check if given timer job is marked as new timer meaning it was never scheduled before,
// if so skip the check by timer name as it has no way to exist
if (!isNewTimer(ctx)) {
if (!ctx.isNew()) {
jobInstance = getTimerJobInstance(jobName);
if (jobInstance == null) {
jobInstance = scheduler.getTimerByName(jobName);
Expand Down Expand Up @@ -245,9 +244,4 @@ public boolean isValid(GlobalJobHandle jobHandle) {
protected String getJobName(JobContext ctx, long id) {
return JobNameHelper.getJobName(ctx, id);
}

private boolean isNewTimer(JobContext ctx) {
return ctx instanceof ProcessJobContext && ((ProcessJobContext) ctx).isNewTimer();
}

}

0 comments on commit f4e97f6

Please sign in to comment.