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
12 changed files
with
405 additions
and
128 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
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()); | ||
|
||
} |
131 changes: 89 additions & 42 deletions
131
...cheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
131 changes: 131 additions & 0 deletions
131
...t/src/test/java/io/quarkus/scheduler/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.scheduler.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
28 changes: 28 additions & 0 deletions
28
extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/DefaultInvoker.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,28 @@ | ||
package io.quarkus.scheduler.runtime; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ManagedContext; | ||
import io.quarkus.scheduler.ScheduledExecution; | ||
|
||
public abstract class DefaultInvoker implements ScheduledInvoker { | ||
|
||
@Override | ||
public CompletionStage<Void> invoke(ScheduledExecution execution) throws Exception { | ||
ManagedContext requestContext = Arc.container().requestContext(); | ||
if (requestContext.isActive()) { | ||
return invokeBean(execution); | ||
} else { | ||
try { | ||
requestContext.activate(); | ||
return invokeBean(execution); | ||
} finally { | ||
requestContext.terminate(); | ||
} | ||
} | ||
} | ||
|
||
protected abstract CompletionStage<Void> invokeBean(ScheduledExecution execution) throws Exception; | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
extensions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/DelegateInvoker.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,16 @@ | ||
package io.quarkus.scheduler.runtime; | ||
|
||
abstract class DelegateInvoker implements ScheduledInvoker { | ||
|
||
protected final ScheduledInvoker delegate; | ||
|
||
public DelegateInvoker(ScheduledInvoker delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public boolean isBlocking() { | ||
return delegate.isBlocking(); | ||
} | ||
|
||
} |
23 changes: 21 additions & 2 deletions
23
...nsions/scheduler/runtime/src/main/java/io/quarkus/scheduler/runtime/ScheduledInvoker.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 |
---|---|---|
@@ -1,11 +1,30 @@ | ||
package io.quarkus.scheduler.runtime; | ||
|
||
import io.quarkus.arc.runtime.BeanInvoker; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import io.quarkus.scheduler.ScheduledExecution; | ||
|
||
/** | ||
* Invokes a scheduled business method of a bean. | ||
*/ | ||
public interface ScheduledInvoker extends BeanInvoker<ScheduledExecution> { | ||
public interface ScheduledInvoker { | ||
|
||
/** | ||
* | ||
* @param execution | ||
* @return the result | ||
* @throws Exception | ||
*/ | ||
CompletionStage<Void> invoke(ScheduledExecution execution) throws Exception; | ||
|
||
/** | ||
* A blocking invoker is executed on the main executor for blocking tasks. | ||
* A non-blocking invoker is executed on the event loop. | ||
* | ||
* @return {@code true} if the scheduled method is blocking, {@code false} otherwise | ||
*/ | ||
default boolean isBlocking() { | ||
return true; | ||
} | ||
|
||
} |
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.