How can I configure the time zone for a Spring based #Scheduled cron job?
Background:
I have a job that executes once a day, say 2 PM, using Spring's #Scheduled annotation:
#Scheduled(cron = "0 0 14 * * *")
public void execute() {
// do scheduled job
}
The problem is that 2 PM differs between different servers, because Spring uses on TimeZone.getDefault() internally. Moreover, the JavaDoc of TimeZone.getDefault() states that:
Gets the default TimeZone for this host. The source of the default TimeZone may vary with implementation.
In other words, the time zone is not determined. It may depend on JVM implementation, server time zone configuration, server location, and / or other unknown factors. Consequently, the cron job triggers on different times on different servers, unless there is a way to explicitly set which time zone that should be used?
I am using Spring 3.2.2.
Update
As of Spring 4, Spring Jira issue SPR-10456 has been resolved. Consequently, the #Scheduled annotation has a new zone attribute for exactly this purpose.
It turned out that I could not use the #Scheduled annotation, but I implemented a work-around. In the JavaDoc of the SchedulingConfigurer it is stated that:
[SchedulingConfigurer is] Typically used for setting a specific TaskScheduler bean to be used when executing scheduled tasks or for registering scheduled tasks in a programmatic fashion as opposed to the declarative approach of using the #Scheduled annotation.
Next, I changed the cron job to implement the Runnable interface and then updated my configuration file to implement the SchedulingConfigurer, see below:
#Configuration
#EnableScheduling
#ComponentScan("package.that.contains.the.runnable.job.bean")
public class JobConfiguration implements SchedulingConfigurer {
private static final String cronExpression = "0 0 14 * * *";
private static final String timeZone = "CET";
#Autowired
private Runnable cronJob;
#Bean
CronTrigger cronTrigger() {
return new CronTrigger(cronExpression, TimeZone.getTimeZone(timeZone));
}
#Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addCronTask(new CronTask(job, cronTrigger()));
}
}
Please read the JavaDoc of the #EnableScheduling for more information.
Update
As of Spring 4, Spring Jira issue SPR-10456 has been resolved. Consequently, the #Scheduled annotation has a new zone attribute for exactly this purpose, e.g.
#Scheduled(cron = "0 0 14 * * *", zone = "CET")
public void execute() {
// do scheduled job
}
There is element zone in annotation #Scheduled, starting from version 4.0.
You can insert a timezone as a string that can be accepted by java.util.TimeZone.
Your code should be like this:
#Scheduled(cron = "0 0 14 * * *", zone = "GMT-5")
public void execute() {
// do scheduled job
}
"Zone" is gonna be the desired country's timezone.
Here is a nice tutorial about scheduled tasks with Spring:
https://www.baeldung.com/cron-expressions
You can also use time zone with #Scheduled tag in spring-boot like this :
#Scheduled(cron = "0 0 14 * * *" , zone = "GMT+5:00")
public void execute() {
// do the scheduled job
}
I doubt you want different jobs or parts of application to use different time zones. Assuming you want to have it all consistent and DRY, either configure OS on all servers to have consistent time zone, or set user.timezone Java system property for all of the application servers. Centrally manage configuration (OS, application server), and for that puppet and chef can be very useful.
Related
I am coming from a (mostly) Python Django/Celery background and starting with Spring Boot.
I am having a hard time to understand how do you separate the roles.
For example when having a Django/Celery project, I will have one one side the web backends started as gunicorn, and on the other side the workers started as celery (so, different commands, but pointing at the same code).
But on Spring Boot, you only have a single entrypoint, and as soon as the scheduler is injected, the jobs will start being processed.
What is the correct way do separate those like in a Django/Celery application ?
Should I put almost all my code as a library and then create 2 final applications, one that will setup the #DispatcherServlet and another one that will setup #EnableScheduling, or is there some kind of configuration to be injected at runtime ?
In my opinion, if 'the web' and 'the scheduler' are both the important function in the application, then we don't need to separate them as long as you are creating a monolithic application.
Because you are using spring boot, so #DispatcherServlet and all the other web component that a web application needed will be injected and configured automatically. The only thing you have to do is creating some class that annotated with #Controller or #RestController and set up the #RequestMapping methods inside those class.
How about Scheduler? you need to add #EnableScheduling in one of the #Configuration class first, then create Scheduler class in scheduler package like below code sample.
You can use cron property to set up the specify execute time just like Linux crontab. The jobs will start being processed only if the cron time is up.
#Component
public class PlatformScheduler {
#Autowired
private BatchService batchService;
#Scheduled(cron = "0 0 12 * * *")
public void dailyInitialize() {
clearCompletedBatches();
queryBatchesToRunToday();
}
#Scheduled(fixedRate = 10000, initialDelay = 10000)
private void harvestCompletedBatches() {
batchService.harvestCompletedBatches();
}
#Scheduled(fixedRate = 10000, initialDelay = 10000)
private void executeWaitingBatches() {
batchService.executeWaitingBatches(new DateTime());
}
}
The most simple project hierarchy will be like below, 'the web' and 'the scheduler' can be in the same project safely and share the same #Service components without harm.
I have feature togglz in my spring boot application. There are some features which are successfully work for the moment. So now I need to do some operation right in the time when one of my feature is activated.
Is there any way to do that?
By using Spring Scheduling you can resolve your problem.
step 1: Enable scheduling for this annotation #EnableScheduling at class level
step 2: You can use any pattern like, hourly, minute, fixed delay,..etc.
#Scheduled(fixedRate = 10000)
#Scheduled(cron = "0 * * * * MON-FRI")`enter code here`
void checTogglzk() {
// ...
}
I would implement Custom ActivationStrategy and call your method within it.
Example how to do it in toggz doc: https://www.togglz.org/documentation/activation-strategies.html
I have around 10 jobs scheduled with #Scheduled and a hardcoded cron expression like this:
#Scheduled(cron = "* * 1 * * *")
public void testMethod(){
doSomething();
}
Now i want to be able to through the database update this cron expression and reschedule the specific job in runtime.
Does anyone knows how to do this?
Thanks
If you want to configure the scheduling of job at runtime, I don't think you can use the annotation #Scheduled.
You can use your own scheduler instead from Spring documentation :
scheduler.schedule(task, new CronTrigger("0 15 9-17 * * MON-FRI"));
Then, if you want to change the configuration, you can cancel the scheduling and create a new one.
TaskScheduler return a ScheduledFuture that you should save somewhere and it can be cancelled with cancel(...) method.
I think that #Scheduled no support this feature (must be interesting implement that). For advance scheduling feature you need to use quartz or other scheduler solution. My answer is based on Quartz Solution:
#Component
class ReschedulerComponent{
#Autowired
private SchedulerFactoryBean schedulerFactoryBean;
public void reSchedule(){
Trigger oldTriger = schedulerFactoryBean.getScheduler().getTrigger("my_custom_trigger");
Trigger myNewTrigger = TriggerBuilder
.newTrigger()
.forJob(jobDetail) // Name of your job
.withIdentity("my_custom_trigger")
.startAt(myNewDATE)
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow())
.build();
schedulerFactoryBean.getScheduler().rescheduleJob(oldTriger.getKey(), myNewTrigger);
}
}
Quick introduction: https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html
This can be done by specifying the cron expression in your property place holder as mentioned below. Add below code in #configuration class.
#Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new ClassPathResource("test.properties"));
return properties;
}
Now test.properties will be available in your placeholder. Test.properties shown below
variable.name.inside.properties= 00 39 05 * * *
Then inside your scheduler class add
#Scheduled(cron = "${variable.name.inside.properties}")
public void testMethod(){
doSomething();
}
I think you should look at this resource
You can programmatically create scheduled jobs. So if you annotate your method with #PostConstruct it should pick it when the application starts and run at the scheduled time
https://www.programcreek.com/java-api-examples/index.php?api=org.quartz.impl.triggers.SimpleTriggerImpl
http://www.quartz-scheduler.org/api/2.2.1/org/quartz/impl/triggers/SimpleTriggerImpl.html
If you want to configure schedule of the job, so that you don't need to change the code, I suggest you to extract value in property stored in some configuration.properties, then access to it in code with #Value.
UPD: found this topic, maybe you find it useful
Spring Scheduler change cron expression dynamically
I want to use Spring Framework #Cachable annotation and European Central Bank API from where i will get the rates agains EUR. I saw in spring docs that #Cachable annotation is used to cache some data which will be always the same... but is there a way to reload the method which caches the value for the rate (on every 5 hours maybe or...).
try to use #Scheduled
#Scheduled(fixedRate = "0 0 */5 * * *")
#CacheEvict(value = { CACHE_NAME })
public void clearCache() {
}
The format of cron is
second, minute, hour, day, month, weekday
Do not forget to add #EnableScheduling to your config
I have a simple spring application which has a Cron job like this.
#Service
#EnableScheduling
public class sampleClass {
#Scheduled(cron = "0 0/30 * * * *")
public void demoServiceMethod()
{
//executes something
}
}
This works fine and executes every 30 minutes. Now i'm looking for a way that if the server is down at a certain time and if it misses this scheduling, a way to find out, so I can trigger another process to cover the server being down. I'm quite new to spring scheduling and the answer might be obvious, but I could not find it. So thanks in advance.