mstfdz
mstfdz

Reputation: 2796

How to run quartz job for every working days of month

I need to run a job which needs to run first working day of each month, also i need to remove the holidays and get the first working day for each month.

FE : first of may is holiday, so that job needs to run on second day of may.

for the other jobs i use below code.

 public CronTriggerFactoryBean testBean() {
    CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
    bean.setJobDetail(test().getObject());
    bean.setName("test");
    bean.setGroup("test");
    bean.setCronExpression("0 45 3 ? * *");
    return bean;
}

Is there any way to solve this problem?

Upvotes: 2

Views: 3457

Answers (2)

Nikolas
Nikolas

Reputation: 44398

CRON doesn't and probably will never support any mechanism recognizing whether the holiday at the current or given location is. The holidays are inconsistent among countries and sometimes even among provinces (usually geographically large countries).

This feature would be very complicated for such simple-to-use designated expressions such as CRON is.

The only possible thing you can do to trigger every day from Monday to Friday (2-6) regardless of the holidays and perform some additional holiday check within the given job. In case the 2-6 or MON-FRI expression doesn't work, use 2,3,4,5,6.

bean.setCronExpression("0 45 3 ? * 2-6"); 
// At 03:45:00am, every day between Monday and Friday, every month
 

Disclaimer: CRON's week starts from Sunday (1).

Upvotes: 4

dresselhaus
dresselhaus

Reputation: 164

Realizing that requirement via cron expression(s) would probably too complicated. I would go with a cron expression that runs Monday to Friday, e. g.

0 3 45 * * MON-FRI

and check if the current day is a holiday inside the job by verifying against a list of dates.

Upvotes: 2

Related Questions