如何在 Spring 3 中配置自定义触发器?

发布于 2024-12-08 21:34:29 字数 418 浏览 0 评论 0原文

我需要配置一个调度算法,该算法超出了 Spring 内置调度的能力(基本上“每 5 分钟一次,但仅在 4:00h 到 16:00h 之间”)。看来实现 org.springframework.scheduling.Trigger 接口是可行的方法,这看起来很简单。

我无法弄清楚的部分似乎没有在 文档是:它如何与 XML 配置混合?似乎没有任何方法可以在任务命名空间的元素中指定自定义触发器 bean(除了 Quartz 示例)。

如何在 Spring 3 应用程序中使用自定义触发器?理想情况下使用 Bean XML 配置。

I need to configure a scheduling algorithm that is beyond the capabilities of Spring's in-build scheduling (basically "every 5 minutes, but only between 4:00h and 16:00h"). It seems that implementing the org.springframework.scheduling.Trigger interface is the way to go, which seems simple enough.

The part I can't figure out and that doesn't seem to be answered in the documentation is: how does this mix with the XML configuration? There doesn't seem to be any way of specifying a custom trigger bean in the elements of the task namespace (apart from the Quartz example).

How do I use a custom trigger in a Spring 3 application? Ideally using the Bean XML configuration.

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

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

发布评论

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

评论(2

下壹個目標 2024-12-15 21:34:29

看看 DurationTrigger 我一年前写的。

public class DurationTrigger implements Trigger {

    /**
     * <p> Create a trigger with the given period, start and end time that define a time window that a task will be
     *     scheduled within.</p>
     */
    public DurationTrigger( Date startTime, Date endTime, long period ) {...} 

    // ...
 }

以下是如何使用此触发器安排此类任务:

Trigger trigger = new DurationTrigger( startTime, endTime, period );
ScheduledFuture task = taskScheduler.schedule( packageDeliveryTask, trigger );

或者,您可以使用 CronTrigger / cron 表达式:

<!-- Fire every minute starting at 2:00 PM and ending at 2:05 PM, every day -->

<task:scheduled-tasks>
    <task:scheduled ref="simpleProcessor" method="process" cron="0 0-5 14 * * ?"/>
</task:scheduled-tasks>

查看此 JIRA 以及 Spring Integration 文章

编辑< /strong>:

根据 JIRA 讨论,您可以使用 Spring Integration 配置上面的 DurationTrigger 或任何其他自定义触发器:

<inbound-channel-adapter id="yourChannelAdapter"
                         channel="yourChannel">
    <poller trigger="durationTrigger"/>
</inbound-channel-adapter>

<beans:bean id="durationTrigger" class="org.gitpod.scheduler.trigger.DurationTrigger">
    <beans:constructor-arg value="${start.time}"/>
    <beans:constructor-arg value="${end.time}"/>
    <beans:constructor-arg value="${period}"/>
</beans:bean>

在项目中使用 Spring Integration 非常简单,即使你没有计划 到。您可以尽可能少地使用上述调度部分,也可以尽可能依赖 Spring Integration 提供的许多其他企业集成模式。

Take a look at DurationTrigger I wrote a year ago.

public class DurationTrigger implements Trigger {

    /**
     * <p> Create a trigger with the given period, start and end time that define a time window that a task will be
     *     scheduled within.</p>
     */
    public DurationTrigger( Date startTime, Date endTime, long period ) {...} 

    // ...
 }

Here is how you would schedule such a task with this trigger:

Trigger trigger = new DurationTrigger( startTime, endTime, period );
ScheduledFuture task = taskScheduler.schedule( packageDeliveryTask, trigger );

Alternatively, you can use a CronTrigger / cron expression:

<!-- Fire every minute starting at 2:00 PM and ending at 2:05 PM, every day -->

<task:scheduled-tasks>
    <task:scheduled ref="simpleProcessor" method="process" cron="0 0-5 14 * * ?"/>
</task:scheduled-tasks>

Check out this JIRA as well as this Spring Integration article

EDIT:

From the JIRA discussion, you can configure the DurationTrigger above, or any other custom trigger for that matter, using Spring Integration:

<inbound-channel-adapter id="yourChannelAdapter"
                         channel="yourChannel">
    <poller trigger="durationTrigger"/>
</inbound-channel-adapter>

<beans:bean id="durationTrigger" class="org.gitpod.scheduler.trigger.DurationTrigger">
    <beans:constructor-arg value="${start.time}"/>
    <beans:constructor-arg value="${end.time}"/>
    <beans:constructor-arg value="${period}"/>
</beans:bean>

It is quite simple to use Spring Integration in your project, even if you did not plan to. You can use as little as the above scheduling piece, or as much as relying on many other Enterprise Integration patterns that Spring Integration has available.

眼趣 2024-12-15 21:34:29

看来在 Spring 3.0 中使用 XML 来配置除两个标准触发器之外的任何触发器都是不可能的。不过,它已作为新功能添加到 3.1M2 版本中: https://jira.springsource .org/browse/SPR-8205

感谢 Mark Fisher 的指出这一点

It seems using XML to configure any but the two standard triggers is not possible in Spring 3.0. It has been added as a new feature in the 3.1M2 release, though: https://jira.springsource.org/browse/SPR-8205

Thanks to Mark Fisher for pointing this out.

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