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.
- Loading branch information
Showing
15 changed files
with
415 additions
and
82 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
70 changes: 70 additions & 0 deletions
70
...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,70 @@ | ||
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") | ||
.overrideConfigKey("quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread", | ||
"true"); | ||
|
||
@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(); | ||
} | ||
|
||
} | ||
|
||
} |
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(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
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)) | ||
.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(); | ||
} | ||
|
||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
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; | ||
|
||
/** | ||
* Annotated scheduled method may not be executed concurrently. The behavior is identical to a {@link Job} class annotated with | ||
* {@link DisallowConcurrentExecution}. Keep in mind that this annotation can be only used if | ||
* {@code quarkus.quartz.run-blocking-scheduled-method-on-quartz-thread} is set to {@code true}. | ||
* <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 { | ||
|
||
} |
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
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
Oops, something went wrong.