Quartz 与 Spring 集成

发布于 2024-10-14 12:03:24 字数 1791 浏览 3 评论 0原文

我有一个网络应用程序,我正在尝试在春季以编程方式启动 Quartz 调度程序。我有一个服务类,我在其中创建 SchedulerFactory 的实例,然后获取调度程序。

代码如下。

@Service("auctionWinnerService")
public class NormalAuctionWinnerServiceImpl implements AuctionWinnerService {

    public static final String NORMAL_AUCTION = "NORMAL AUCTION";
    public static int NORMAL_AUCTION_COUNTER = 0;
    private SchedulerFactory schedulerFactory;
    private Scheduler scheduler;

    public void declareWinner(int auctionId, Map<String, Object> parameterMap) {
        System.out.println("INSIDE declareWinner of NormalAuctionWinner");
        schedulerFactory = new StdSchedulerFactory();
        try {
            scheduler = schedulerFactory.getScheduler();
            System.out.println("GOT SCHEDULER : "+scheduler);
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }

        JobDetail jd = new JobDetail();
        jd.setName(NORMAL_AUCTION+" JOB "+NORMAL_AUCTION_COUNTER);
        jd.setJobClass(NormalAuctionWinnerJob.class);

        /** CREATE CRON TRIGGER INSTANCE **/
        CronTrigger t = new CronTrigger();
        t.setName(NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER);
        t.setGroup("Normal Auction");
        Date d = new Date();
        Date d1 = new Date();
        d1.setMinutes(d.getMinutes()+5);

        t.setStartTime(d);
        t.setEndTime(d1);
        try {
            t.setCronExpression("10 * * * * ? *");
            scheduler.scheduleJob(jd, t);
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

SchedulerFactory 和调度程序已实例化,但我的作业未运行。 有人能指出我在这里缺少什么吗?

另外,我只需要一个 Factory 实例和一个调度程序实例。我尝试制作静态但没有成功。任何朝这个方向的指示都会有所帮助。

谢谢

I have a web application and I am trying to start Quartz scheduler programmatically in spring. I have a service class where I create an instance of SchedulerFactory and then get a scheduler.

The code is as follows.

@Service("auctionWinnerService")
public class NormalAuctionWinnerServiceImpl implements AuctionWinnerService {

    public static final String NORMAL_AUCTION = "NORMAL AUCTION";
    public static int NORMAL_AUCTION_COUNTER = 0;
    private SchedulerFactory schedulerFactory;
    private Scheduler scheduler;

    public void declareWinner(int auctionId, Map<String, Object> parameterMap) {
        System.out.println("INSIDE declareWinner of NormalAuctionWinner");
        schedulerFactory = new StdSchedulerFactory();
        try {
            scheduler = schedulerFactory.getScheduler();
            System.out.println("GOT SCHEDULER : "+scheduler);
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }

        JobDetail jd = new JobDetail();
        jd.setName(NORMAL_AUCTION+" JOB "+NORMAL_AUCTION_COUNTER);
        jd.setJobClass(NormalAuctionWinnerJob.class);

        /** CREATE CRON TRIGGER INSTANCE **/
        CronTrigger t = new CronTrigger();
        t.setName(NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER);
        t.setGroup("Normal Auction");
        Date d = new Date();
        Date d1 = new Date();
        d1.setMinutes(d.getMinutes()+5);

        t.setStartTime(d);
        t.setEndTime(d1);
        try {
            t.setCronExpression("10 * * * * ? *");
            scheduler.scheduleJob(jd, t);
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

The schedulerFactory and scheduler are instantiated but my jobs do not run.
Could someone point out what am I missing here?

Also I need only one instance of Factory and one scheduler instance. I tried making the static but it didn't work. Any pointers in this direction will be helpful.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ双果 2024-10-21 12:03:24

除非您对 Quartz 的专有功能有特定要求,否则我建议摆脱它并使用 Spring 的内部调度能力。从 Spring 3 开始,这包括对 cron 类型表达式的支持,与 Quartz 的 cron 触发器非常相似。

除了简化应用程序及其配置之外,它本质上比 Quartz 更可靠,并且通过 TaskScheduler 接口提供更简单的 API 供编程使用。

Unless you have a specific requirement on Quartz's proprietary functionality, I recommend getting rid of it and using Spring's internal scheduling capability. As of Spring 3, this includes support for cron-type expressions, very similar to Quartz's cron trigger.

As well as bringing simplicity to your application and its config, it's inherently more reliable than Quartz, and provides an easier API for programmatic usage, via the TaskScheduler interface.

北方的巷 2024-10-21 12:03:24

首先,你对 Quartz 或 cron 触发器表达式了解多少?我可能弄错了,但是 10 * * * * ? * 将使触发器每分钟每隔 10 秒触发一次,但我从未见过这样的表达,它可能根本不触发。

您是否想创建一个每 10 秒触发一次的触发器?在这种情况下,使用像这样的简单触发器:

new SimpleTrigger((NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER),
                   "Normal Auction",
                   d,
                   d1,
                   SimpleTrigger.REPEAT_INDEFINITELY,
                   10000L);

编辑:

好的,所以如果这是您的要求,您需要一个在拍卖结束时仅触发一次的触发器。为此,请使用如下所示的 SimpleTrigger:

new SimpleTrigger((NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER),
               "Normal Auction",
               d1,
               null,
               0,
               0L);

在这种情况下,开始日期并不重要,只要将其设置为在适当的时间(结束时间)触发并且仅触发一次即可。

作为补充说明,不要计算这样的日期。我建议您尝试Joda Time 库。非常简单且众所周知的替代笨拙的标准日期/日历 API。

First of all, how well do you know Quartz or cron trigger expressions? I may be mistaken, but 10 * * * * ? * will make the trigger fire every 10th second of every minute, but I've never seen such expression, it may not fire at all.

Are you trying to create a trigger to fire every 10 seconds? In this case, use a simple trigger like this:

new SimpleTrigger((NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER),
                   "Normal Auction",
                   d,
                   d1,
                   SimpleTrigger.REPEAT_INDEFINITELY,
                   10000L);

Edit:

Ok, so if that's you requirement, you need a trigger that fill fire just once, at the end time of the auction. For that, use a SimpleTrigger like this:

new SimpleTrigger((NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER),
               "Normal Auction",
               d1,
               null,
               0,
               0L);

The start date in this case does not matter, as long as you set it to fire on the appropriate time (the end time) and just once.

And as an additional note, do not calculate dates like that. I suggest you to try Joda Time library. Really simple and well known replacement for the clumsy standard Date/Calendar API.

安稳善良 2024-10-21 12:03:24

您忘记启动调度程序了! scheduler.start();

...
try {
   t.setCronExpression("10 * * * * ? *");
scheduler.scheduleJob(jd, t);
scheduler.start();
} catch (SchedulerException e) {
      e.printStackTrace();
} catch (ParseException e) {
      e.printStackTrace();
}

我已经证明了这一点,在添加缺少的语句后,(并用虚拟替换作业)它对我有用/

You have forgotten to start the scheduler! scheduler.start();

...
try {
   t.setCronExpression("10 * * * * ? *");
scheduler.scheduleJob(jd, t);
scheduler.start();
} catch (SchedulerException e) {
      e.printStackTrace();
} catch (ParseException e) {
      e.printStackTrace();
}

I have proved this, after adding the missing statement, (and replacing the job with an dummy) it worked for me/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文