栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在特定的开始,结束日期和时间限制内运行Quartz Scheduler Job

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在特定的开始,结束日期和时间限制内运行Quartz Scheduler Job

当您说它不起作用时,您会得到什么错误?

您可以尝试以下代码( 编辑: 适用于Quartz
2.2)。此方法未在cron表达式中指定开始/结束日期和年份,而是使用Trigger方法指定它们。(注意:我自己尚未测试过,请告诉我它是否对您有用)

编辑: 我有机会测试此代码,我在下面的代码中运行并不断更改系统时钟,并且从开始到结束日期的上午9点至12点之间,所有触发器均成功。

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");        } }}

如果上面的代码不能正常工作,尝试更换

cronSchedule("0 0 9-12 * * ?")
cronSchedule("0 0 9-12 ? *?")



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号