Skip to content

Commit

Permalink
Try to cancel timer always, and only search if timer cancellation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Aug 27, 2024
1 parent df8c5b4 commit e2cb609
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void executeTimerJob(Timer timer) {
try {
executeTimerJobInstance(timerJobInstance);
} catch (Exception e) {
logger.error("Error executing timer handle {}", timerJobInstance.getJobHandle(), e);
recoverTimerJobInstance(timerJob, timer, e);
}
}
Expand All @@ -139,7 +140,7 @@ else if (ejbTimerJob.getTimerJobInstance().getTrigger().hasNextFireTime() != nul
// because of the transaction, so we need to do this here.
tx = timerJobInstance -> {
logger.warn("Execution of time failed Interval Trigger failed. Skipping {}", timerJobInstance);
if (removeJob(timerJobInstance.getJobHandle(), null)) {
if (removeJob(timerJobInstance.getJobHandle(), timer, true)) {
internalSchedule(timerJobInstance);
} else {
logger.debug("Interval trigger {} was removed before rescheduling", timerJobInstance);
Expand Down Expand Up @@ -246,7 +247,11 @@ private Serializable removeTransientFields(Serializable info) {
return info;
}

public boolean removeJob(JobHandle jobHandle, Timer ejbTimer) {
public boolean removeJob(JobHandle jobHandle, Timer ejbTimer) {
return removeJob(jobHandle, ejbTimer, false);
}

public boolean removeJob(JobHandle jobHandle, Timer ejbTimer, boolean searchIfFailed) {
EjbGlobalJobHandle ejbHandle = (EjbGlobalJobHandle) jobHandle;
if (useLocalCache) {
boolean removedFromCache = localCache.remove(ejbHandle.getUuid()) != null;
Expand All @@ -257,9 +262,11 @@ public boolean removeJob(JobHandle jobHandle, Timer ejbTimer) {
try {
ejbTimer.cancel();
return true;
} catch (Throwable e) {
logger.debug("Timer cancel error due to {}", e.getMessage());
return false;
} catch (Exception e) {
logger.warn("Timer cancel error for handle {}", ejbHandle, e);
if (!searchIfFailed) {
return false;
}
}
}

Expand All @@ -271,7 +278,7 @@ public boolean removeJob(JobHandle jobHandle, Timer ejbTimer) {
try {
((TimerHandle) ejbTimerHandle).getTimer().cancel();
} catch (Throwable e) {
logger.debug("Timer cancel error due to {}", e.getMessage());
logger.warn("Timer cancel error for handle {}", ejbTimerHandle, e);
return false;
}
return true;
Expand All @@ -288,12 +295,12 @@ public boolean removeJob(JobHandle jobHandle, Timer ejbTimer) {

EjbGlobalJobHandle handle = (EjbGlobalJobHandle) job.getTimerJobInstance().getJobHandle();
if (handle.getUuid().equals(ejbHandle.getUuid())) {
logger.debug("Job handle {} does match timer and is going to be canceled", jobHandle);
logger.info("Job handle {} does match timer and is going to be canceled", jobHandle);

try {
timer.cancel();
} catch (Throwable e) {
logger.debug("Timer cancel error due to {}", e.getMessage());
logger.warn("Timer cancel error for handle {}", handle, e);
return false;
}
return true;
Expand All @@ -303,7 +310,7 @@ public boolean removeJob(JobHandle jobHandle, Timer ejbTimer) {
logger.debug("Timer {} has already expired or was canceled ", timer);
}
}
logger.debug("Job handle {} does not match any timer on {} scheduler service", jobHandle, this);
logger.info("Job handle {} does not match any timer on {} scheduler service", jobHandle, this);
return false;
}

Expand Down

0 comments on commit e2cb609

Please sign in to comment.