EJB 3.0 TimerService - 在每月的第一天运行计时器

发布于 2024-12-23 06:15:33 字数 373 浏览 3 评论 0原文

在 EJB 3.0 中,TimerService.createTimer(initialDuration, IntervalDuration, TimerID) 方法仅接受 initialDurationintervalDuration 参数。

问题是我想在每个月的 1 号运行计时器,但无法使用 EJB 3.1 TimerService 中的 cron 表达式设置计时器。
一些解决方法是每天运行并检查 @Timeout 方法中的日期(如果是“第一个月”),但这不是合适的方法。

我在网上浏览了很多示例/教程,但没有运气。还有其他方法可以实现这个场景吗?

In EJB 3.0 TimerService.createTimer(initialDuration, intervalDuration, TimerID) method is accepting only initialDuration and intervalDuration arguments.

The problem is I want to run timer on 1st of every month and I can not set my timer using cron expression like in EJB 3.1 TimerService.
Some workaround is like run on daily base and check the date inside @Timeout method if its '1st of month' but that is not appropriate way.

I went through many example/tutorials online but no luck. Is there any other way to implement this scenario?

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

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

发布评论

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

评论(2

短暂陪伴 2024-12-30 06:15:33

我建议采用以下方法:

  • @Timeout 方法中使用 JodaTime 来计算下一个时间点。因此,每个计时器都会在其 @Timeout 方法中安排下一次调用。这样做的优点是,您可以构建一个管理对话框,提供系统中计划计时器的概述,如果您每天运行计时器,则无法做到这一点。我用它来实现一个相当复杂的、用户可配置的计时器配置(Outlook 风格,即“每两个月的第三个上午 9:00 运行一次)”。即使使用 JodaTime,我最终还是编写了大量代码,不过,就你的情况来说,基本上不应该那么复杂。

timerService.createTimer( new DateTime( System.currentTimeMillis()).plusMonths(1).withTimeAtStartOfDay().toDate(), null );

  • 放弃 TimerService 并使用 Quartz 直接用于调度。然后您可以将 cron 表达式直接输入到 API 中。 Quartz 通常可以与 Java EE 很好地集成,即使在复杂的集群环境中也是如此。

I suggest the following approaches:

  • In the @Timeout method use JodaTime to calculate the next point-in-time. So every timer schedules the next call in its @Timeout method . This has the advantage that you are able to build an admin dialog that provides an overview of the scheduled timers in the system which you can't do if you just run the timer daily. I used this to implement a fairly complex, user-configurable timer configuration (Outlook style, i.e. something like "run this every two months on the third at 9:00 AM). Even with JodaTime, I ended up writing a lot of code, though. However, in your case it shouldn't be that complicated. Basically like

timerService.createTimer( new DateTime( System.currentTimeMillis()).plusMonths(1).withTimeAtStartOfDay().toDate(), null );

  • Abandon TimerService and use Quartz directly for scheduling. Then you can feed cron expressions directly into the API. Quartz generally integrates nicely with Java EE, even in complex clustered environments.
箜明 2024-12-30 06:15:33
    try {
        Scheduler scheduler;
        scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();

        JobDetail job = new JobDetail("job name", "job group", (your class which will implement job interface).class);
        CronTrigger trigger = new CronTrigger("trigger name", "trigger group", "0 0 0 1 * ?");
        scheduler.scheduleJob(job, trigger);
    } catch (Exception e) {
        e.printStackTrace();
    }

使用上面的代码它将在每个月的 1 号运行计时器。

    try {
        Scheduler scheduler;
        scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();

        JobDetail job = new JobDetail("job name", "job group", (your class which will implement job interface).class);
        CronTrigger trigger = new CronTrigger("trigger name", "trigger group", "0 0 0 1 * ?");
        scheduler.scheduleJob(job, trigger);
    } catch (Exception e) {
        e.printStackTrace();
    }

Use above code it will run timer at 1st of every month.

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