Search code examples
javaspring-mvctomcat8spring-scheduled

How to change cron expression of spring scheduler from properties file without restarting tomcat server?


I am using spring scheduler.This works fine but when I changed cron.expression value in application.properties every time I need to restart tomcat server.Is there any way that I can make it dynamic like automatically changes will reflect ? I did google also but didnt got any solution for my app.I have given code snippet as follows:

application.properties

cron.expression=0 58 23 * * ?

@Scheduled(cron = "${cron.expression}", zone = "IST")
public void sendEmail() throws Exception {



}

Solution

  • It's an easy task if you are working on spring-cloud. Spring has a project called 'spring cloud config' which helps you to achieve the externalize the properties and whenever there is a change, that change will push to your service. Have a look at setting up spring cloud config

    You may find many samples for same. Let me know if you are looking for code.