-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a new
ApplicationNotRunning
predicate for @Scheduled
The `@Scheduled` annotation's `skipExecutionIf` attribute can be used to skip individual executions in a schedule. Currently, Quarkus only provides the `Never` implementation, which is also the default (never skips an execution). Since the scheduler is initialized very early (priority `PLATFORM_BEFORE`) it can happen that executions get scheduled before the application has been fully started. An application can solve this by itself waiting to get notified with the `StartupEvent` or it can now use this new predicate: `@Scheduled(..., skipExecutionIf = ApplicationNotRunning.class)`.
- Loading branch information
1 parent
b31e400
commit 8c694ed
Showing
3 changed files
with
89 additions
and
1 deletion.
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
61 changes: 61 additions & 0 deletions
61
...eployment/src/test/java/io/quarkus/scheduler/test/ApplicationNotRunningPredicateTest.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,61 @@ | ||
package io.quarkus.scheduler.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.interceptor.Interceptor; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Priority; | ||
import io.quarkus.runtime.StartupEvent; | ||
import io.quarkus.scheduler.FailedExecution; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.SuccessfulExecution; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ApplicationNotRunningPredicateTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().withApplicationRoot((jar) -> jar.addClasses(Jobs.class)); | ||
|
||
static final CountDownLatch SUCCESS_LATCH = new CountDownLatch(1); | ||
static SuccessfulExecution successfulExecution; | ||
static FailedExecution failedExecution; | ||
|
||
@Test | ||
public void testTriggerErrorStatus() throws InterruptedException { | ||
assertTrue(SUCCESS_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertNull(failedExecution); | ||
} | ||
|
||
void observeSuccessfulExecution(@Observes SuccessfulExecution successfulExecution) { | ||
ApplicationNotRunningPredicateTest.successfulExecution = successfulExecution; | ||
SUCCESS_LATCH.countDown(); | ||
} | ||
|
||
void observeFailedExecution(@Observes FailedExecution failedExecution) { | ||
ApplicationNotRunningPredicateTest.failedExecution = failedExecution; | ||
} | ||
|
||
static class Jobs { | ||
|
||
volatile boolean preStart; | ||
|
||
void started(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent event) { | ||
preStart = true; | ||
} | ||
|
||
@Scheduled(every = "0.2s", skipExecutionIf = Scheduled.ApplicationNotRunning.class) | ||
void scheduleAfterStarted() { | ||
if (!preStart) { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
} | ||
} |
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