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();
}
}