1

I am using quartz scheduler for scheduling jobs.I have a case where i want to execute a job every day night(9:00 PM) to next day morning(06:00 AM).How can i achieve this.Currently i am initializing trigger like this

      Trigger trigger2 = newTrigger()
    .withIdentity("trigger1", "group1")
    .startNow()
    .withSchedule(simpleSchedule()
            .withIntervalInSeconds(10)
            .repeatForever())            
    .build();

What modification i need to make to satisfy the requirement?

vmb
  • 2,878
  • 15
  • 60
  • 90

3 Answers3

1

If you have opt for the Quartz CronExpression you can use an expression like this 0 * 21-23,0-5 ? * * that fire a job every minute every day from 00:00 AM to 05:59 AM and from 9:00 PM to 23:59 PM, so:

trigger = newTrigger()
    .withIdentity("trigger7", "group1")
    .withSchedule(cronSchedule("0 * 21-23,0-5 ? * *"))
    .build();

Remember to import the import static org.quartz.CronScheduleBuilder.cronSchedule

The frequency (in this example every minute) depends on your requirement.

Cristian Porta
  • 4,393
  • 3
  • 19
  • 26
1

If your need is to run a job ONCE every day you need to only specify the start time of the job:

newTrigger().withSchedule(
      CronScheduleBuilder.dailyAtHourAndMinute(21,0)).build();

Quartz scheduler can't help you if the scheduled job (database processing) takes many hours and it might get over the 6AM time limit. Quartz only starts the job. You should stop yourself the running job at 6AM. For example suppose the job is a method:

public void doSomeDBOperations() {
    while(have more data to process) {
        if(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) == 6) {
           break;
        }

        //insert data
    }
}
dcernahoschi
  • 14,968
  • 5
  • 37
  • 59
0

Here is a ref you may use this to schedule time using quartz. Java – Job Scheduling in web application with quartz API

This part might help you

JobDetail jDetail = new JobDetail("Newsletter", "NJob", MyJob.class);

        //"0 0 12 * * ?" Fire at 12pm (noon) every day
        //"0/2 * * * * ?" Fire at every 2 seconds every day

 CronTrigger crTrigger = new CronTrigger("cronTrigger", "NJob", "0/2 * * * * ?");
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
  • I dont want to use crontrigger – vmb Jan 08 '13 at 04:40
  • If you really don't want to use the crontrigger, you can schedule two different 24 hour intervals with start times at 9pm and 6am. – Joshua Martell Jan 08 '13 at 04:48
  • ya .. i will use crontrigger..then how will be the CronExpression...i am confused with cronExpression..can anyone pls help – vmb Jan 08 '13 at 12:29
  • What type of task you are going to do in the scheduler so that you need to stop it by yourself ??? – NoNaMe Jan 08 '13 at 12:46
  • i was dealing with processing data in database and inserting to new table.i want to do it only when database traffic is low..This operation may take long time.that is why i want to specify a time range – vmb Jan 09 '13 at 04:04