Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monthly jobs on every 30th day using Quartz

guys,

I have monthly jobs scheduled(using Quartz) by users. Users provide starting date f or first job to run, it could be any day of month 1-31

My question is how to schedule this using cron trigger, having in mind that not all month have 31,30,29th days. In such case job should run closest previous day of the month. So, lets say April has only 30 days, so job has to run on 30th of April.

Can it be done using single cron trigger? Or should it be combination of triggers? I tried to play with CronExpression to see how it handles such cases:

CronExpression ce = new CronExpression("0 0 0 30 JAN-DEC ? *");
Date nextValidTime = ce.getNextValidTimeAfter(//**27th of February**//);

I've got nextValidTime equal to 30th of March, so cron just "skipped" February. Any help would be highly appreciated. Thanks in advance.

like image 524
ilu Avatar asked Jul 11 '12 22:07

ilu


People also ask

How do you schedule multiple jobs using quartz?

If you want to schedule multiple jobs in your console application you can simply call Scheduler. ScheduleJob (IScheduler) passing the job and the trigger you've previously created: IJobDetail firstJob = JobBuilder. Create<FirstJob>() .

How many jobs can quartz handle?

The actual number of jobs that can be running at any moment in time is limited by the size of the thread pool. If there are five threads in the pool, no more than five jobs can run at a time.

How do I get job details in quartz?

new JobKey("jobName", "jobGroupName"); As long as your job name and job group name is the same with which you created your job, you will be able to get your job detail.

How does Quartz scheduling work?

Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.


1 Answers

The 'L' character is allowed for the day-of-month and day-of-week fields. This character > is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". You can also specify an offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the calendar month. When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing/unexpected results.

http://quartz-scheduler.org/api/2.0.0/org/quartz/CronExpression.html

new CronExpression("0 0 0 L JAN-DEC ? *");

Edit:

I would just do something like this then

Calendar tCalendar = Calendar.getInstance();
tCalendar.set(2009, Calendar.FEBRUARY/*int*/, 1); // for example Feb, 2009 -- day doesn't matter here
if(userSelectedDay > tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) ){
    //Fix user day
    //fixedDay = tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)

    // Or, for that month 
    //new CronExpression("0 0 0 L JAN-DEC ? *");
}
like image 55
Sully Avatar answered Sep 19 '22 00:09

Sully