Anuradha
Anuradha

Reputation: 107

to schedule a java program using quartz scheduler

I need to run a particular program once in a week,once in a month and once in a while

CronTrigger trigger = newTrigger()
        .withIdentity("trigger1", "group1")
        .withSchedule(cronSchedule("0/20 * * * * ?"))
        .build();

What all changes should be done in the above code?particularly in this part("0/20 * * * * ?")) please help

Upvotes: 1

Views: 755

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

If you don't want to use cron expression you can use the build in feature of Quartz to build the trigger, but i still believe cron expression are always small and if you are comfortable with them is always the way to go

Weekly

trigger=newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(date).withSchedule(calendarIntervalSchedule()
.withIntervalInWeeks(weekly interval in int)).build();

Monthly

trigger=newTrigger().withIdentity(cronTriggerDTO.getTiggerId(), "simpleGroup")
.startAt(date).withSchedule(monthlyOnDayAndHourAndMinute(DAY_OF_MONTH, HOUR_OF_DAY,MINUTE))
.build();

Once

trigger=newTrigger().withIdentity(cronTriggerDTO.getTiggerId(),"simpleGroup")
.startAt(date).forJob("myjob", "mygroup").build();

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156459

You'll need to switch between multiple schedules depending on the user's choice:

// Once a month (the first day at midnight)
0 0 0 1 * ? 

// Once a week (Sunday at midnight)
0 0 0 * * 1

// On a specific date (November 10, 2012)
0 0 0 10 11 ? 2012

You can of course change the zeroes to other values if you want to change the time. See a full guide here.

Upvotes: 2

Related Questions