I want to add spring annotation @Scheduled to spring bean and start task in method in another class. There is only one way to start task in spring reference - Scheduling-Tasks by @EnableScheduling. How to start it without @SpringBootApplication and spring boot runner.
@Component
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("The time is now {}" + dateFormat.format(new Date()));
}
}
@SpringBootApplication
@EnableScheduling
public class SpringSheduleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSheduleApplication.class, args);
}
}
@Component
public class ShedullerStarter {
public void start(){
ScheduledTasks tasks = new ScheduledTasks();;
try {
// some code here
} finally {
// start annotation
tasks.reportCurrentTime();
}
}
}