Skip to content

Commit

Permalink
Quartz - Do not recreate job instances for re-fires
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jul 30, 2024
1 parent 261cc87 commit f2ea2b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public void testDependentBeanJobDestroyed() throws SchedulerException, Interrupt

@Test
public void testDependentBeanJobWithRefire() throws SchedulerException, InterruptedException {
// 5 one-off jobs should trigger construction/execution/destruction 10 times in total
CountDownLatch execLatch = service.initExecuteLatch(10);
CountDownLatch constructLatch = service.initConstructLatch(10);
CountDownLatch destroyedLatch = service.initDestroyedLatch(10);
// 5 one-off jobs should trigger construction/execution/destruction 5 times in total
CountDownLatch execLatch = service.initExecuteLatch(5);
CountDownLatch constructLatch = service.initConstructLatch(5);
CountDownLatch destroyedLatch = service.initDestroyedLatch(5);
for (int i = 0; i < 5; i++) {
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger" + i, "myRefiringGroup")
Expand All @@ -104,10 +104,10 @@ public void testDependentBeanJobWithRefire() throws SchedulerException, Interrup
assertTrue(constructLatch.await(2, TimeUnit.SECONDS), "Latch count: " + constructLatch.getCount());
assertTrue(destroyedLatch.await(2, TimeUnit.SECONDS), "Latch count: " + destroyedLatch.getCount());

// repeating job triggering three times; we expect six beans to exist for that due to refires
execLatch = service.initExecuteLatch(6);
constructLatch = service.initConstructLatch(6);
destroyedLatch = service.initDestroyedLatch(6);
// repeating job triggering three times; re-fires should NOT recreate the bean instance
execLatch = service.initExecuteLatch(3);
constructLatch = service.initConstructLatch(3);
destroyedLatch = service.initDestroyedLatch(3);
JobDetail job = JobBuilder.newJob(RefiringJob.class)
.withIdentity("myRepeatingJob", "myRefiringGroup")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public CdiAwareJob(Instance<? extends Job> jobInstance) {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Instance.Handle<? extends Job> handle = jobInstance.getHandle();
boolean refire = false;
try {
handle.get().execute(context);
} catch (JobExecutionException e) {
refire = e.refireImmediately();
throw e;
} finally {
if (handle.getBean().getScope().equals(Dependent.class)) {
if (refire != true && handle.getBean().getScope().equals(Dependent.class)) {
handle.destroy();
}
}
Expand Down

0 comments on commit f2ea2b0

Please sign in to comment.