I'm trying to load the following cron-execution-expression:
---
####################### Cron-Job Every 2mins every day #######################
cron:
exe:
expression: 0 0/2 * * * ?
The catch is, the above cron-expression is in a Spring-Cloud-Config (Let's say Springboot Project A, running on port: 8001) github repository. named: microservice-dev.yml
Project B (port: 8002) loads all the configurations provided by Project A at startup & I'm happy with that. But how do I locate this expression?
${cron.exe.expression}
@Component
//Couldn't get it to work with Spring-Cloud-Config
@PropertySource("classpath:microservice-dev.yml")
public class MergeCachedRecordsToDBImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(MergeCachedRecordsToDBImpl.class);
//Couldn't get it to work with Spring-Cloud Config
@Scheduled(cron = "${cron.exe.expression}")
public void purgeExpired() {
LOGGER.info("Cron-Job Notification....");
LOGGER.info("Cron-Job executed at: {}", new Timestamp(new Date().getTime()));
}
}
At some point, I got it to work, but I'm not sure how? I'm trying to retrace my steps.
Now I'm getting this exception:
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'purgeExpired': Could not resolve placeholder 'cron.exe.expression' in value "${cron.exe.expression}"
Ok, got it to work again with the .yml file. Happy Days.
In the following .yml file:
src/main/resources/bootstrap.yml
has the following contents:
---
spring:
application:
name: leaderboard
profiles:
active: dev
server:
port: 8004
---
####################### Cron-Job Every 2mins every day #######################
cron:
exe:
expression: 0 0/2 * * * ?
in the code, I annotated my class and method with following annotations:
@PropertySource("classpath:bootstrap.yml")
public class CronClass{
private static final Logger LOGGER = LoggerFactory.getLogger(CronClass.class);
@Scheduled(cron = "${cron.exe.expression}")
private void cornJob(){
LOGGER.info("Cron-Job Notification....");
LOGGER.info("Cron-Job executed at: {}", new Timestamp(new Date().getTime()));
}
}
Results:
2018-09-29 11:19:00.007 INFO [leaderboard,66037f3d8052cf6b,66037f3d8052cf6b,false] 7937 --- [ scheduling-1] i.s.l.task.CronClass : Cron-Job Notification....
2018-09-29 11:19:00.007 INFO [leaderboard,66037f3d8052cf6b,66037f3d8052cf6b,false] 7937 --- [ scheduling-1] i.s.l.task.CronClass : Cron-Job executed at: 2018-09-29 11:19:00.007
2018-09-29 11:20:00.000 INFO [leaderboard,25cab214549b76a6,25cab214549b76a6,false] 7937 --- [ scheduling-1] i.s.l.task.CronClass : Cron-Job Notification....
2018-09-29 11:20:00.000 INFO [leaderboard,25cab214549b76a6,25cab214549b76a6,false] 7937 --- [ scheduling-1] i.s.l.task.CronClass : Cron-Job executed at: 2018-09-29 11:20:00.0
2018-09-29 11:21:00.000 INFO [leaderboard,c2f241d8a806fd26,c2f241d8a806fd26,false] 7937 --- [ scheduling-1] i.s.l.task.CronClass : Cron-Job Notification....
2018-09-29 11:21:00.000 INFO [leaderboard,c2f241d8a806fd26,c2f241d8a806fd26,false] 7937 --- [ scheduling-1] i.s.l.task.CronClass : Cron-Job executed at: 2018-09-29 11:21:00.0