Run Quartz Scheduler Job with specific start, end date and within time constraints

AZ_

I am using Quartz-Scheduler for repetitive tasks but I am facing a trouble. In my server side my user wants to specify some date range like From 2013-09-27 with in 09:00 AM - 12:00 PM to 2013-09-30

Explanation:

Run a job from 2013-09-27 to 2013-09-30 but only between 09:00 AM - 12:00 PM

I am facing trouble in writing a Cron expression for it, furthermore my user is non-technical so my user wants me to create Cron expression automatically from both time stamp values.

Please help me out. Let me know if there is another way.

I have seen many resources on Google but I still can't find nothing.

Links:

http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-05

Does cron expression in unix/linux allow specifying exact start and end dates

Update

I have written one but it's not working

|------------------------------------------------------------------|
| Seconds | Minutes | Hours | DayOfMonth | Month | DayOfWeek | Year|
|         |         |       |            |       |           |     |
|   0     |    0    | 9-12  |   27-30    |   9   |     ?     | 2013|
|------------------------------------------------------------------|

trying to map 2013-09-27 to 2013-09-30 but only between 09:00 AM - 12:00 PM

Updated I have also tried it running with

Trigger trigger = TriggerBuilder.newTrigger().withIdentity(NAME_TRIGGER_TASK_UPDATER, GROUP_TASK_TRIGGER)
                    .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 19-22 10 ? *")).build();

but it doesn't give any error nor go into my execute method of my job

cronSchedule("0 0 9-12 ? * ?") throws invalid schedule exception.

The code below runs it without respecting the start and end date.

String startDateStr = "2013-09-27 00:00:00.0";
        String endDateStr = "2013-09-31 00:00:00.0";

        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
        Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);

        CronTrigger cronTrigger = newTrigger()
          .withIdentity("trigger1", "testJob")
          .startAt(startDate)
          .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?"))
          .endAt(endDate)
          .build();
AmeetC

What is the error you get when you say it is not working?

You can try the following code (Edit: applies to Quartz 2.2). This approach does not specify the start/end dates and year in the cron expression, instead uses the Trigger methods to specify them. (Note: I haven't tested it myself, let me know if it works for you)

Edit: I had the chance to test this code, I ran the code below and kept changing the system clock and all triggers were successful between 9 am to 12 am from start to end date.

public class CronJob {

    public static void main(String[] args) throws ParseException, SchedulerException {

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail job = newJob(TestJob.class)
            .withIdentity("cronJob", "testJob") 
            .build();

        String startDateStr = "2013-09-27 00:00:00.0";
        String endDateStr = "2013-09-31 00:00:00.0";

        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
        Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);

        CronTrigger cronTrigger = newTrigger()
          .withIdentity("trigger1", "testJob")
          .startAt(startDate)
          .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?").withMisfireHandlingInstructionDoNothing())
          .endAt(endDate)
          .build();

        scheduler.scheduleJob(job, cronTrigger);
        scheduler.start();
    }    

    public static class TestJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("this is a cron scheduled test job");
        }        
    }
}

If the above code does not work, try to replace the cronSchedule("0 0 9-12 * * ?") with cronSchedule("0 0 9-12 ? * ?")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java quartz scheduler run at specific time

From Dev

Quartz Scheduler Job run once, then gives an error

From Dev

Quartz Scheduler Job run once, then gives an error

From Dev

How to implement Quartz Scheduler with specific job in servlet?

From Dev

Make delayed job run at specific date time

From Dev

Java task scheduler run daily from start to end date

From Dev

Kendo UI Scheduler Start Date & End Date

From Dev

Quartz Scheduler Job Auto Termination

From Dev

Quartz scheduler not running the cron job

From Dev

Code start and end in specific time

From Dev

Run Quartz job synchronous

From Dev

How to start a Job with several cron-triggers using the quartz-scheduler?

From Dev

time format for Start date, End date - SSRS

From Dev

Excel Start Date with specific start Time

From Dev

Run CRON job everyday at specific time

From Dev

How to schedule the airflow job with start_date and End_date?

From Dev

Quartz Scheduler - trigger does not reference given job

From Dev

QUARTZ Job Scheduler - JobListener issue [JAVA]

From Dev

Quartz Job Scheduler Not Firing Jobs (C#)

From Dev

Dynamic job scheduling in spring boot with quartz scheduler

From Dev

Need to trigger a job as and when quartz scheduler starts

From Dev

Quartz - Shutdown Scheduler based on Job Variable

From Dev

Quartz scheduler job issuing in clustered environment

From Dev

Quartz scheduler - allow parallel job execution

From Dev

What are the differences between Rundeck and Quartz (Job Scheduler)?

From Dev

R calculate time within time interval between start End time

From Dev

R calculate time within time interval between start End time

From Dev

start/end date and time in oracle sql query

From Dev

In MySQL grab items within start_date and end_date

Related Related

  1. 1

    java quartz scheduler run at specific time

  2. 2

    Quartz Scheduler Job run once, then gives an error

  3. 3

    Quartz Scheduler Job run once, then gives an error

  4. 4

    How to implement Quartz Scheduler with specific job in servlet?

  5. 5

    Make delayed job run at specific date time

  6. 6

    Java task scheduler run daily from start to end date

  7. 7

    Kendo UI Scheduler Start Date & End Date

  8. 8

    Quartz Scheduler Job Auto Termination

  9. 9

    Quartz scheduler not running the cron job

  10. 10

    Code start and end in specific time

  11. 11

    Run Quartz job synchronous

  12. 12

    How to start a Job with several cron-triggers using the quartz-scheduler?

  13. 13

    time format for Start date, End date - SSRS

  14. 14

    Excel Start Date with specific start Time

  15. 15

    Run CRON job everyday at specific time

  16. 16

    How to schedule the airflow job with start_date and End_date?

  17. 17

    Quartz Scheduler - trigger does not reference given job

  18. 18

    QUARTZ Job Scheduler - JobListener issue [JAVA]

  19. 19

    Quartz Job Scheduler Not Firing Jobs (C#)

  20. 20

    Dynamic job scheduling in spring boot with quartz scheduler

  21. 21

    Need to trigger a job as and when quartz scheduler starts

  22. 22

    Quartz - Shutdown Scheduler based on Job Variable

  23. 23

    Quartz scheduler job issuing in clustered environment

  24. 24

    Quartz scheduler - allow parallel job execution

  25. 25

    What are the differences between Rundeck and Quartz (Job Scheduler)?

  26. 26

    R calculate time within time interval between start End time

  27. 27

    R calculate time within time interval between start End time

  28. 28

    start/end date and time in oracle sql query

  29. 29

    In MySQL grab items within start_date and end_date

HotTag

Archive