From 104988985e99108f7a749e11e9b92d0b480c2147 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 3 Sep 2021 16:29:50 +0300 Subject: [PATCH] Fix issue with skip predicates in scheduled methods Before this fix, the implementations where actually eligible for bean removal Fixes: #19900 (cherry picked from commit 361494784de7ae7aa73e755be57d3a417113f2da) --- .../deployment/SchedulerProcessor.java | 6 +++++ .../test/ConditionalExecutionTest.java | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/extensions/scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java b/extensions/scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java index 490927bc4c653..f2e1825b9007d 100644 --- a/extensions/scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java +++ b/extensions/scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java @@ -96,6 +96,7 @@ public class SchedulerProcessor { 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 Type SCHEDULED_EXECUTION_TYPE = Type.create(DotName.createSimple(ScheduledExecution.class.getName()), Kind.CLASS); @@ -466,4 +467,9 @@ private Throwable validateScheduled(CronParser parser, AnnotationInstance schedu return null; } + @BuildStep + UnremovableBeanBuildItem unremoveableSkipPredicates() { + return new UnremovableBeanBuildItem(new UnremovableBeanBuildItem.BeanTypeExclusion(SKIP_PREDICATE)); + } + } diff --git a/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/ConditionalExecutionTest.java b/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/ConditionalExecutionTest.java index 6f7e0b9eab131..f04cd3eff6a4b 100644 --- a/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/ConditionalExecutionTest.java +++ b/extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/ConditionalExecutionTest.java @@ -1,11 +1,13 @@ package io.quarkus.scheduler.test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import javax.enterprise.event.Observes; import javax.inject.Singleton; @@ -44,17 +46,25 @@ public void testExecution() { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } + + assertTrue(OtherIsDisabled.TESTED.get()); + assertEquals(0, Jobs.OTHER_COUNT.get()); } static class Jobs { static final CountDownLatch COUNTER = new CountDownLatch(1); + static final AtomicInteger OTHER_COUNT = new AtomicInteger(0); @Scheduled(identity = "foo", every = "1s", skipExecutionIf = IsDisabled.class) void doSomething() throws InterruptedException { COUNTER.countDown(); } + @Scheduled(identity = "other-foo", every = "1s", skipExecutionIf = OtherIsDisabled.class) + void doSomethingElse() throws InterruptedException { + OTHER_COUNT.incrementAndGet(); + } } @Singleton @@ -77,4 +87,17 @@ void onSkip(@Observes SkippedExecution event) { } } + + @Singleton + public static class OtherIsDisabled implements Scheduled.SkipPredicate { + + static final AtomicBoolean TESTED = new AtomicBoolean(false); + + @Override + public boolean test(ScheduledExecution execution) { + TESTED.set(true); + return true; + } + + } }