Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a timer task on a specific day (1st of every month) using Spring

We have a requirement of running a job on 1st of every month (time: 00:00:00 AM) exactly.
We are using Spring framework's ScheduledTimerTask to schedule jobs using delay and period properties. This class doesn't support running a job on specific date.

Can somebody suggest, how we can solve that problem using Spring and Java technology?

like image 987
dvrnaidu Avatar asked Aug 26 '14 08:08

dvrnaidu


People also ask

How do I schedule a task in spring boot?

Java Cron ExpressionThe @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. The @Scheduled annotation is used to trigger the scheduler for a specific time period.

How do you dynamically schedule a spring batch job?

Configure batch job scheduler To configure, batch job scheduling is done in two steps: Enable scheduling with @EnableScheduling annotation. Create method annotated with @Scheduled and provide recurrence details using cron job. Add the job execution logic inside this method.


1 Answers

If you don't have to run this job on a single node in a cluster you can use Spring Task, see: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

@Scheduled(cron="0 0 0 1 1/1 *")
public void doSomething() {
    // something that should execute on 1st day every month @ 00:00
}

For generating cron expressions try cronmaker.com

Please be aware that if you use this code in a cluster it will run on all nodes.

like image 108
rgrebski Avatar answered Sep 21 '22 21:09

rgrebski