Abhishek Shah
Abhishek Shah

Reputation: 1424

Cron : Execute job based first N days of month only

I have implemented one scheduler job which is required to execute only for first N days. Is it possible to make any particular cron expression to achieve this functionality?

Normally, I am using http://www.cronmaker.com/. However, It looks like, the tool is not supported for this kind of cron expression.

Can anyone please provide your thoughts?

Upvotes: 3

Views: 2362

Answers (1)

Sangam Belose
Sangam Belose

Reputation: 4506

Use something like this:

@Scheduled(cron="0 0 17 1-5 1/1 ? ")

This cron expression will execute on every month 1 to 5 days at 5 PM. ( you can specify what time you want to run that job on that day.)

below is the sample spring boot class by which you can verify the solution.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class SchedulerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchedulerApplication.class, args);
    }

    @Scheduled(cron="0 0 17 1-5 1/1 ? ")
    public void sampleScheduled() {

        System.out.println("Just testing the scheduler");
    }
}

Upvotes: 2

Related Questions