Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get next run time Spring Scheduling?

I am working on a scheduling project which executes multiple jobs at regular intervals. I am using a cron scheduling as in the example below. The jobs are getting executed successfully no problems. However for a requirement I want to calculate and persist the next run time of the Scheduled job in DB. Is there a solution to get next and previous fire times of jobs for the configuration below?

Example configuration:

import java.util.Date;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class DemoServiceBasicUsageCron
{   
    @Scheduled(cron="1,3,30,32 * * * * ?")
    public void demoServiceMethod()
    {
        System.out.println("Curent date time is - "+ new Date());
    }

}
like image 642
Sathish Prakasam Avatar asked May 03 '17 17:05

Sathish Prakasam


People also ask

How to run a scheduled task using @scheduled in spring?

We can run a scheduled task using Spring's @Scheduled annotation but based on the properties fixedDelay and fixedRate the nature of execution changes. The fixedDelay property makes sure that there is a delay of n millisecond between the finish time of an execution of a task and the start time of the next execution of the task.

How do you create a scheduled job in spring?

We explicitly enable scheduling by adding the @EnableScheduling annotation to a Spring configuration class. We can make the scheduling conditional on a property so that we can enable and disable scheduling by setting the property. We create scheduled jobs by decorating a method with the @Scheduled annotation.

What is scheduling in Spring Boot?

Spring Boot - Scheduling. Scheduling is a process of executing the tasks for the specific time period. Spring Boot provides a good support to write a scheduler on the Spring applications.

How do I schedule a method in a spring bean?

We can turn any method in a Spring bean for scheduling by adding the @Scheduled annotation to it. The @Scheduled is a method-level annotation applied at runtime to mark the method to be scheduled. It takes one attribute from cron, fixedDelay, or fixedRate for specifying the schedule of execution in different formats.


1 Answers

You can use CronExpression for Spring Framework 5.3 and newer. Use CronSequenceGenerator for older versions. Both of them have same methods. But CronSequenceGenerator is deprecated.

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

@Component
public class MyJob {

    public static final String CRON_EXPRESSION = "0 0 5 * * *";

    @PostConstruct
    public void init() {
        //Update: Resolve compile time error for static method `parse`
        CronExpression cronTrigger = CronExpression.parse(CRON_EXPRESSION);

        LocalDateTime next = cronTrigger.next(LocalDateTime.now());

        System.out.println("Next Execution Time: " + next);
    }

    @Scheduled(cron = CRON_EXPRESSION)
    public void run() {
        // Your custom code here
    }
}
like image 83
bhdrk Avatar answered Sep 17 '22 03:09

bhdrk