Spring Scheduler didnot run during DST - spring

In our Java application we have Spring scheduler and the configuration is as given below :
<task:scheduled-tasks>
<task:scheduled ref="myHealthCheck" method="getStatus" cron="0 0/5 * * * *"/>
</task:scheduled-tasks>
During DST end on November 3rd, the scheduler (which runs every 5 minutes) didnt run for one hour. Can some one help in handling this during DST start and end.
Thanks
Shaan

This may be related to the time zone configuration.
What is the default time zone of your host? Try running System.out.println(TimeZone.getDefault()) to check.
If the default time zone doesn't support DST (e.g. UTC), you can set the right time zone for the whole JVM by passing the system property:
-Duser.timezone=Europe/Kiev
Alternatively, you can specify time zone for the scheduled task directly.
Unfortunately, Spring Task XSD doesn't have a property to define a time zone (http://www.springframework.org/schema/task/spring-task-4.3.xsd).
But you can use annotations. As of Spring 4, the #Scheduled annotation has a new zone attribute (https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-annotation-support-scheduled):
You can also use the zone attribute to specify the time zone in which the cron expression is resolved.
#Scheduled(cron="*/5 * * * * MON-FRI", zone="Europe/Kiev")
public void doSomething() {
// something that should execute on weekdays only
}

Related

Spring scheduled / how zone attribute works in case of daylight saving?

Could someone can explain how zone attribute in spring #Scheduled annotation works in case of daylight saving changes ?
For example: I have task that is running every 4PM Europe/Warsaw zone, application is long running(no restarts).
Does it mean that scheduled task will be automatically updated on 30.10(DST end) ?

spring boot #scheduled triggered after the time expected

I try to trigger a task every day at 7h45 in spring boot by this way
#Scheduled(cron = "45 7 * * * ?")
public void method() {....}
And I saw this morning that it triggered at around 9h09.
I checked that server time correspond to the time displayed on my computer. Furthermore the server is running on windows
So Why this difference of time ?
The first element in Spring cron expressions are seconds.
So I assume that it was run at 9:07:45
This would be the correct expression:
0 45 7 * * ?
Check out: https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions

Spring Batch Job scheduler stops being executing after several runs

On a project we have to run a job that starts periodically (every 5 minutes on QA env now) that processes some assignments for 40k users.
We had decided to utilize Spring Batch because it fits perfectly and implemented it with pretty much default configuration (e.g. it uses SyncTaskExecutor under the hood).
Okay, so, there is a job that consists of a single step with:
out-of-box HibernatePagingItemReader
custom ItemProcessorthat performs lightweight calculations in memory
custom ItemWriter that persists data to the same PostgreSQL db via several JPQL and native queries.
The job itself is scheduled with #EnableScheduling and is being triggered every 5 mins by cron expression:
#Scheduled(cron = "${job.assignment-rules}")
void processAssignments() {
try {
log.debug("Running assignment processing job");
jobLauncher.run(assignmentProcessingJob, populateJobParameters());
} catch (JobExecutionException e) {
log.error("Job processing has failed", e);
}
}
Here is a cron expression from application.yml:
job:
assignment-rules: "0 0/5 * * * *"
The problem is that it stops being scheduled after several runs (different amount of runs every time). Let's take a look into the Spring Batch schema:
select ex.job_instance_id, ex.create_time, ex.start_time, ex.end_time, ex.status, ex.exit_code, ex.exit_message
from batch_job_execution ex inner join batch_job_instance bji on ex.job_instance_id = bji.job_instance_id
order by start_time desc, job_instance_id desc;
And then silence. Nothing special in logs.
The only thing I believe could make sense is that there are two more jobs running on that instance. And one of them is time consuming because it sends emails via SMTP.
The entire jobs schedule is:
job:
invitation-email: "0 0/10 * * * *"
assignment-rules: "0 0/5 * * * *"
rm-subordinates-count: "0 0/30 * * * *"
Colleagues, could anybody point me out the way this problem could be troubleshooted?
Thanks a lot in advance
Using the default SyncTaskExecutor to launch jobs is not safe in your use case as all jobs will be executed by a single thread. If one of the jobs takes more than 5 minutes to run, next jobs will pile up and fail to start at some point.
I would configure a JobLauncher with an asynchronous TaskExecutor implementation (like org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) in your use case. You can find an example in the Configuring a JobLauncher section (See "Figure 3. Asynchronous Job Launcher Sequence").
Apart from the task executor configured to be used by the JobLauncher of Spring Batch, you need to make sure you have the right task executor used by Spring Boot to schedule tasks (since you are using #EnableScheduling). Please refer to the Task Execution and Scheduling section for more details.

How to Design a Spring-boot scheduler as Service, so that other micro-services can use it

I want to design a scheduler as service using spring-boot. My scheduler should be generic so that other microservices can use it as they want.
I tried normal spring boot examples.
/**
* This scheduler will run on every 20 Seconds.
*/
#Scheduled(fixedRate = 20 * 1000, initialDelay = 5000)
public void scheduleTaskWithInitialDelay() {
logger.info("Fixed Rate Task With Initail Delay 20 Seconds:: Execution Time - "+dateTimeFormatter.format(LocalDateTime.now()));
}
/**
* This scheduler will run on every 10 Seconds.
*/
#Scheduled(fixedRate = 10* 1000, initialDelay = 5000)
public void scheduleTaskWithInitialDelay1() {
logger.info("Fixed Rate Task With Initail Delay 10 Seconds:: Execution Time - "+dateTimeFormatter.format(LocalDateTime.now()));
}
You need to store other microservice's requests to schedule something in your persistent. So, you have an inventory that which microservice requested the scheduling service and with delay or cron or something else.
Now, you can read all the requested configuration from the database and start scheduler for them.
This is a common use case in enterprise applications when people choose to write custom code.
Your database table should contain all the detail + what to do if scheduler reached to given time (Push data/event to some URL or something else).
Some technical detail
You schedule service should allow to
Add Schedule
Start/Stop/Update existing schedule
Callback or some other operation when scheduler meet the time
Hope, this will help.

How to set scheduler task in spring to run after a fixed interval of time and between a given interval of time

I'm trying to write a Spring cron expression to have my code execute after a fixed interval of time and between a given interval of time. I would like the code to be executed after every 20 minutes and between 6.00am to 6.00pm that is during day time.
Following is the expression for running the code every 20 min but i am not getting how to restrict it to run between a given interval of time (Can i restrict the schedular in cron expression or i will have to implement the logic in the code that is java class).
<task:scheduled-tasks>
<task:scheduled ref="commonSchedulerForSms" method="sendCommonSmsReport"
cron="0 0/20 * * * ?" />
</task:scheduled-tasks>
I am working on Spring VERSION 3.0, Servlet version 2.5 and Java version 1.6.
Thanks in Advance.
Try this expression:
0 0/20 6-17 * * ?
Fires every 20 minutes from 6 am to 5:40 pm (06:00 to 17:40)

Resources