Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject @Scheduled fixedRate value from Spring Boot application.yml file

I know I can inject the value from properties file with the following syntax:

@Scheduled(fixedRate=${myRate}) public void getSchedule(){     System.out.println("in scheduled job"); } 

However I can't guess how to accomplish the same if the configuration is in YAML file.

Thanks in advance,

like image 847
Juan Carlos González Avatar asked Dec 12 '14 14:12

Juan Carlos González


2 Answers

In my application.properties (YAML) I put this

console:     fetchMetrics: 5000 

Then in my simple Task class I push the definition :

@Scheduled(fixedRateString ="${console.fetchMetrics}", initialDelay=1000) public void fetchMetrics() {     logger.info("What's up ?"); } 

Please notice that fixedRate expects a long and you want to inject a placeholder, you will need fixedRateString

like image 131
Wajax Avatar answered Oct 04 '22 22:10

Wajax


I find it easy once done for my project.
Change fixedRate to fixedRateString and put the property key in double quotes like this:

@Scheduled(fixedRateString="${myRate}") public void getSchedule() {     System.out.println("Scheduled job"); } 
like image 35
abhishek ringsia Avatar answered Oct 04 '22 21:10

abhishek ringsia