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.
Scheduler - support non-blocking scheduled methods
- resolves quarkusio#24621
- Loading branch information
Showing
18 changed files
with
609 additions
and
140 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
131 changes: 131 additions & 0 deletions
131
...ment/src/test/java/io/quarkus/quartz/test/nonblocking/NonBlockingScheduledMethodTest.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,131 @@ | ||
package io.quarkus.quartz.test.nonblocking; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.ScheduledExecution; | ||
import io.quarkus.scheduler.SuccessfulExecution; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.common.annotation.NonBlocking; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.core.Context; | ||
|
||
public class NonBlockingScheduledMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root.addClasses(Jobs.class, JobWasExecuted.class)); | ||
|
||
@Test | ||
public void testVoid() throws InterruptedException { | ||
assertTrue(Jobs.VOID_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertTrue(Jobs.VOID_ON_EVENT_LOOP.get()); | ||
assertTrue(Jobs.SUCCESS_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertEvents("every_void"); | ||
} | ||
|
||
@Test | ||
public void testUni() throws InterruptedException { | ||
assertTrue(Jobs.UNI_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertTrue(Jobs.UNI_ON_EVENT_LOOP.get()); | ||
assertTrue(Jobs.SUCCESS_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertEvents("every_uni"); | ||
} | ||
|
||
@Test | ||
public void testCompletionStage() throws InterruptedException { | ||
assertTrue(Jobs.CS_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertTrue(Jobs.CS_ON_EVENT_LOOP.get()); | ||
assertTrue(Jobs.SUCCESS_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertEvents("every_cs"); | ||
} | ||
|
||
private void assertEvents(String id) { | ||
for (SuccessfulExecution exec : Jobs.events) { | ||
if (exec.getExecution().getTrigger().getId().equals(id)) { | ||
return; | ||
} | ||
} | ||
fail("No SuccessfulExecution event fired for " + id + ": " + Jobs.events); | ||
} | ||
|
||
static class Jobs { | ||
|
||
// jobs executed | ||
static final CountDownLatch VOID_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch UNI_LATCH = new CountDownLatch(1); | ||
static final CountDownLatch CS_LATCH = new CountDownLatch(1); | ||
|
||
// jobs executed on the event loop | ||
static final AtomicBoolean VOID_ON_EVENT_LOOP = new AtomicBoolean(); | ||
static final AtomicBoolean UNI_ON_EVENT_LOOP = new AtomicBoolean(); | ||
static final AtomicBoolean CS_ON_EVENT_LOOP = new AtomicBoolean(); | ||
|
||
// sucessfull events | ||
static final CountDownLatch SUCCESS_LATCH = new CountDownLatch(3); | ||
static final List<SuccessfulExecution> events = new CopyOnWriteArrayList<>(); | ||
|
||
static void onSuccess(@Observes SuccessfulExecution event) { | ||
events.add(event); | ||
SUCCESS_LATCH.countDown(); | ||
} | ||
|
||
@NonBlocking | ||
@Scheduled(every = "0.5s", identity = "every_void", skipExecutionIf = JobWasExecuted.class) | ||
void everySecond() { | ||
VOID_ON_EVENT_LOOP.set(Context.isOnEventLoopThread()); | ||
VOID_LATCH.countDown(); | ||
} | ||
|
||
@Scheduled(every = "0.5s", identity = "every_uni", skipExecutionIf = JobWasExecuted.class) | ||
Uni<Void> everySecondUni() { | ||
UNI_ON_EVENT_LOOP.set(Context.isOnEventLoopThread()); | ||
UNI_LATCH.countDown(); | ||
return Uni.createFrom().voidItem(); | ||
} | ||
|
||
@Scheduled(every = "0.5s", identity = "every_cs", skipExecutionIf = JobWasExecuted.class) | ||
CompletionStage<Void> everySecondCompletionStage() { | ||
CompletableFuture<Void> ret = new CompletableFuture<Void>(); | ||
CS_ON_EVENT_LOOP.set(Context.isOnEventLoopThread()); | ||
CS_LATCH.countDown(); | ||
ret.complete(null); | ||
return ret; | ||
} | ||
} | ||
|
||
@Singleton | ||
static class JobWasExecuted implements Scheduled.SkipPredicate { | ||
|
||
@Override | ||
public boolean test(ScheduledExecution execution) { | ||
switch (execution.getTrigger().getId()) { | ||
case "every_void": | ||
return Jobs.VOID_LATCH.getCount() == 0; | ||
case "every_uni": | ||
return Jobs.UNI_LATCH.getCount() == 0; | ||
case "every_cs": | ||
return Jobs.CS_LATCH.getCount() == 0; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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
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
21 changes: 21 additions & 0 deletions
21
...scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerDotNames.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,21 @@ | ||
package io.quarkus.scheduler.deployment; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import io.smallrye.common.annotation.NonBlocking; | ||
|
||
class SchedulerDotNames { | ||
|
||
static final DotName SCHEDULED_NAME = DotName.createSimple(Scheduled.class.getName()); | ||
static final DotName SCHEDULES_NAME = DotName.createSimple(Scheduled.Schedules.class.getName()); | ||
static final DotName SKIP_NEVER_NAME = DotName.createSimple(Scheduled.Never.class.getName()); | ||
static final DotName SKIP_PREDICATE = DotName.createSimple(Scheduled.SkipPredicate.class.getName()); | ||
static final DotName NON_BLOCKING = DotName.createSimple(NonBlocking.class.getName()); | ||
static final DotName UNI = DotName.createSimple("io.smallrye.mutiny.Uni"); | ||
static final DotName COMPLETION_STAGE = DotName.createSimple(CompletionStage.class.getName()); | ||
static final DotName VOID = DotName.createSimple(Void.class.getName()); | ||
|
||
} |
Oops, something went wrong.