0

I followed https://stackoverflow.com/a/50216003/4770397 to start and stop springboot scheduler using rest endpoints. However, I want to stop scheduler on start up as per the requirement, I am trying to call the stopTestScheduler method on postconstructs but nothing seems to work. Can anyone please help.

public class Test {

    public static final String TESTSCHEDULER = "TestScheduler";

    @Autowired
    TestScheduler testScheduler;

    @Autowired
    private ScheduledAnnotationBeanPostProcessor postProcessor;

    public void startTestScheduler() {
        postProcessor.postProcessAfterInitialization(testScheduler, TESTSCHEDULER);
    }

    public void stopTestScheduler() {
        postProcessor.postProcessBeforeDestruction(testSCHEDULER, TESTSCHEDULER);
    }

}


@Configuration
@EnableAsync
@Slf4j
public class TestScheduler {

    @Autowired
    private TestService testService;

    @Scheduled(cron = Constants.CRONSCHEDULEREXPRESSION)
    @Async
    public void trigger() throws IllegalArgumentException, Exception {
        testService.test();
    }
}
Likitha G
  • 1
  • 4
  • Would ConditionalOnProperty and EnvironmentPostPressor help in this case? The default property in the config file could be (False), and once you are ready to autowire the scheduler, then set the property to (True)? Would be great if you can also edit your question and show your TestScheduler – JCompetence Nov 08 '21 at 08:04
  • Thank you so much! Sure, i will try it now, added TestScheduler for reference. – Likitha G Nov 08 '21 at 08:15

0 Answers0