I am defining scheduled jobs with cron style patterns in Spring, using the @Scheduled annotation.
The cron pattern is stored in a config properties file
You can also create a Bean based on condition and that Bean can have a Scheduled method.
@Component
@Configuration
@EnableScheduling
public class CustomCronComponent {
@Bean
@ConditionalOnProperty(value = "my.cron.enabled", matchIfMissing = true, havingValue = "true")
public MyCronTask runMyCronTask() {
return new MyCronTask();
}
}
and
@Component
public class MyCronTask {
@Scheduled(cron = "${my.cron.expression}")
public void run() {
String a = "";
}
}