使用Quartz的每日作业调度程序

时间:2015-02-12 13:23:35

标签: java quartz-scheduler scheduler quartz-core

要求:

2015-02-11至2015-02-15 / 10:30至11:30将要执行的工作,当11:30时间到来时,它应该停止工作应该发生的第15天到来。

这里我有Quartz 1.8.6 lib,但是无法为此获得正确的实现。请帮助我们。

示例程序:

        SimpleDateFormat    formateDate = new SimpleDateFormat("mm/DD/yyyy HH:mm");         
        SchedulerFactory sf = new StdSchedulerFactory(AppConstants.QUARTZ_PROP_FILE);
        log.info("------- invoking scheduler ----------------------");

        System.out.println("------- invoking scheduler ----------------------");
        Scheduler sched = sf.getScheduler();


        java.util.Calendar startCal = java.util.Calendar.getInstance();
        startCal.setTime(formateDate.parse(startDate));


        java.util.Calendar endCal = java.util.Calendar.getInstance();
        endCal.setTime(formateDate.parse(endDate));


        System.out.println("start/end : " + startCal.getTime() + "/"+endCal.getTime());
        DailyCalendar cal = new DailyCalendar(startTime, endTime);

    //  cal.setTimeRange(startTime, endTime);
        cal.setInvertTimeRange(true);
        Calendar quartzCal = cal;

        sched.addCalendar("quartzCal", quartzCal, true, true);


        //makeDailyTrigger(tgName, 12, 15)
        JobDetail job = new JobDetail(jbName, "Admit", DemandStartJob.class);

        SimpleTrigger trigger=new SimpleTrigger(tgName);
        trigger.setStartTime(startCal.getTime());
        trigger.setEndTime(endCal.getTime());
        trigger.setCalendarName("quartzCal");
        trigger.setJobName(jbName);
        trigger.setJobGroup("Admit");
        trigger.setRepeatCount(0);
        trigger.setRepeatInterval(1);
        sched.scheduleJob(trigger);
        sched.start();

1 个答案:

答案 0 :(得分:4)

使用以下代码配置Quartz crontrigger

// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();

// Trigger the job to run now, and then repeat every 40 seconds
Trigger  trigger = newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(cronSchedule("0 42 10 * * ?"))
.forJob(myJobKey)
.build();

// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);

此处" 0 42 10 * *?"是cron表达
使用http://www.cronmaker.com/
用cron表达你的需要

如果你想停止线程,还要配置另一个在第一次工作15分钟后触发的工作。