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

3

11

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 {



}
Parts answered 27/10, 2017 at 8:8 Comment(5)
Look at the interface "SchedulingConfigurer" and its JavaDocSst
SchedulingConfigurer is optional annotation it may be necessary when implementing Trigger-based tasks, which are not supported by the @Scheduled annotation .but my scheduler is working fine but whenever I change cron.expression I compulsory have to restart tomcat server which I don't want is there any way of doing itParts
Have you read the examples from "@EnableScheduling" ? You can set custom triggers etc..Sst
What about refreshing the context with a config server to manage the properties? Your tomcat would still be alive and the beans refreshed with the new property. It needs verification thoughJodi
@gumol can you elaborate it .I didn't get it exactly.Parts
T
5

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.

Transcendentalistic answered 23/8, 2018 at 10:25 Comment(0)
D
2

The annotation parameter is not dynamic. So if you want to change the expression you will have to restart. This is the short answer to your question.

Here is the link that explains about Spring provided @EnableScheduling which is very interesting but still does not overcome the static nature of the parameter and hence the need for restart

BTW I wrote an Open Source library called MgntUtils that provides some infrastructure for creating scheduled tasks. It takes more effort to create a running task with that framework then just use @Scheduledannotation, but it gives you more humanly readable scheduling properties such as "5h", or "20m" for each 5 hours or 20 minutes respectively. (You can read about the solution here).
However, this solution is still not dynamic and will require restart upon parameter value change. But since it is Open source you can get the code and modify it for your needs and to make it dynamic i.e. reacting to parameter change without restart. You can get the library at Maven Central or GitHub

Down answered 20/8, 2018 at 13:45 Comment(0)
P
1

You can't update dynamically from annotations, however CronTrigger

takes the cron expression in it's constructor so you could use that directly from whatever code you have checking for property updates.

Paranoiac answered 20/8, 2018 at 14:10 Comment(2)
I request you to provide example of itParts
@Parts Check logicbig.com/how-to/code-snippets/… .Telemeter

© 2022 - 2024 — McMap. All rights reserved.