forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#44224 from mkouba/issue-44048
Quartz: introduce Nonconcurrent
- Loading branch information
Showing
16 changed files
with
499 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...uartz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentJobDefinitionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.quartz.QuartzScheduler; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentJobDefinitionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root.addClasses(Jobs.class)) | ||
.overrideConfigKey("quarkus.scheduler.start-mode", "forced"); | ||
|
||
@Inject | ||
QuartzScheduler scheduler; | ||
|
||
@Test | ||
public void testExecution() throws InterruptedException { | ||
scheduler.newJob("foo") | ||
.setTask(se -> { | ||
Jobs.NONCONCURRENT_COUNTER.incrementAndGet(); | ||
try { | ||
if (!Jobs.CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
if (Jobs.NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
Jobs.NONCONCURRENT_LATCH.countDown(); | ||
} | ||
}) | ||
.setInterval("1s") | ||
.setNonconcurrent() | ||
.schedule(); | ||
|
||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...artz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentOnQuartzThreadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.quartz.Nonconcurrent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentOnQuartzThreadTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root.addClasses(Jobs.class)) | ||
.overrideConfigKey("quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread", | ||
"true"); | ||
|
||
@Test | ||
public void testExecution() throws InterruptedException { | ||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Nonconcurrent | ||
@Scheduled(identity = "foo", every = "1s") | ||
void nonconcurrent() throws InterruptedException { | ||
NONCONCURRENT_COUNTER.incrementAndGet(); | ||
if (!CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
if (NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
NONCONCURRENT_LATCH.countDown(); | ||
} | ||
} | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...quartz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentProgrammaticTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.quartz.DisallowConcurrentExecution; | ||
import org.quartz.Job; | ||
import org.quartz.JobBuilder; | ||
import org.quartz.JobDetail; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
import org.quartz.SchedulerException; | ||
import org.quartz.SimpleScheduleBuilder; | ||
import org.quartz.Trigger; | ||
import org.quartz.TriggerBuilder; | ||
|
||
import io.quarkus.quartz.QuartzScheduler; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.Scheduler; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentProgrammaticTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(Jobs.class)) | ||
.overrideConfigKey("quarkus.scheduler.start-mode", "halted"); | ||
|
||
@Inject | ||
QuartzScheduler scheduler; | ||
|
||
@Test | ||
public void testExecution() throws SchedulerException, InterruptedException { | ||
JobDetail job = JobBuilder.newJob(Jobs.class) | ||
.withIdentity("foo", Scheduler.class.getName()) | ||
.build(); | ||
Trigger trigger = TriggerBuilder.newTrigger() | ||
.withIdentity("foo", Scheduler.class.getName()) | ||
.startNow() | ||
.withSchedule(SimpleScheduleBuilder.simpleSchedule() | ||
.withIntervalInSeconds(1) | ||
.repeatForever()) | ||
.build(); | ||
scheduler.getScheduler().scheduleJob(job, trigger); | ||
|
||
scheduler.resume(); | ||
|
||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
@DisallowConcurrentExecution | ||
static class Jobs implements Job { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
@Override | ||
public void execute(JobExecutionContext context) throws JobExecutionException { | ||
Jobs.NONCONCURRENT_COUNTER.incrementAndGet(); | ||
try { | ||
if (!Jobs.CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
if (Jobs.NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
Jobs.NONCONCURRENT_LATCH.countDown(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
extensions/quartz/deployment/src/test/java/io/quarkus/quartz/test/NonconcurrentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.quarkus.quartz.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.quartz.Nonconcurrent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonconcurrentTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root.addClasses(Jobs.class)); | ||
|
||
@Test | ||
public void testExecution() throws InterruptedException { | ||
assertTrue(Jobs.NONCONCURRENT_LATCH.await(10, TimeUnit.SECONDS), | ||
String.format("nonconcurrent() executed: %sx", Jobs.NONCONCURRENT_COUNTER.get())); | ||
} | ||
|
||
static class Jobs { | ||
|
||
static final CountDownLatch NONCONCURRENT_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CONCURRENT_LATCH = new CountDownLatch(5); | ||
|
||
static final AtomicInteger NONCONCURRENT_COUNTER = new AtomicInteger(0); | ||
|
||
@Nonconcurrent | ||
@Scheduled(identity = "foo", every = "1s") | ||
void nonconcurrent() throws InterruptedException { | ||
NONCONCURRENT_COUNTER.incrementAndGet(); | ||
if (!CONCURRENT_LATCH.await(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("nonconcurrent() execution blocked too long..."); | ||
} | ||
if (NONCONCURRENT_COUNTER.get() == 1) { | ||
// concurrent() executed >= 5x and nonconcurrent() 1x | ||
NONCONCURRENT_LATCH.countDown(); | ||
} | ||
} | ||
|
||
@Scheduled(identity = "bar", every = "1s") | ||
void concurrent() throws InterruptedException { | ||
CONCURRENT_LATCH.countDown(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
extensions/quartz/runtime/src/main/java/io/quarkus/quartz/Nonconcurrent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.quartz; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import org.quartz.DisallowConcurrentExecution; | ||
import org.quartz.Job; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.SkippedExecution; | ||
|
||
/** | ||
* A scheduled method annotated with this annotation may not be executed concurrently. The behavior is identical to a | ||
* {@link Job} class annotated with {@link DisallowConcurrentExecution}. | ||
* <p> | ||
* If {@code quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread} is set to | ||
* {@code false} the execution of a scheduled method is offloaded to a specific Quarkus thread pool but the triggering Quartz | ||
* thread is blocked until the execution is finished. Therefore, make sure the Quartz thread pool is configured appropriately. | ||
* <p> | ||
* If {@code quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread} is set to {@code true} the scheduled method is | ||
* invoked on a thread managed by Quartz. | ||
* <p> | ||
* Unlike with {@link Scheduled.ConcurrentExecution#SKIP} the {@link SkippedExecution} event is never fired if a method | ||
* execution is skipped by Quartz. | ||
* | ||
* @see DisallowConcurrentExecution | ||
*/ | ||
@Target(METHOD) | ||
@Retention(RUNTIME) | ||
public @interface Nonconcurrent { | ||
|
||
} |
Oops, something went wrong.