简体   繁体   English

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

[英]How do I configure a custom trigger in Spring 3?

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"). 我需要配置一种调度算法,该算法超出了Spring的内置调度功能(基本上是“每5分钟一次,但仅在4:00h和16:00h之间”)。 It seems that implementing the org.springframework.scheduling.Trigger interface is the way to go, which seems simple enough. 似乎实现org.springframework.scheduling.Trigger接口是路要走,这似乎很简单。

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? 我无法弄清的部分( 文档中似乎没有得到回答)是:它如何与XML配置混合? 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). 似乎没有任何方法可以在任务名称空间的元素中指定自定义触发bean(除了Quartz示例)。

How do I use a custom trigger in a Spring 3 application? 如何在Spring 3应用程序中使用自定义触发器? Ideally using the Bean XML configuration. 理想情况下,使用Bean XML配置。

Take a look at DurationTrigger I wrote a year ago. 看看我一年前写的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 ) {...} 

    // ...
 }

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: 另外,您可以使用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>

Check out this JIRA as well as this Spring Integration article 查阅此JIRA和Spring Integration 文章

EDIT : 编辑

From the JIRA discussion, you can configure the DurationTrigger above, or any other custom trigger for that matter, using Spring Integration: 在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>

It is quite simple to use Spring Integration in your project, even if you did not plan to. 即使您不打算在项目中使用Spring Integration,也非常简单。 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. 您可以使用最少的调度程序,也可以最多依靠Spring Integration提供的许多其他Enterprise Integration模式。

It seems using XML to configure any but the two standard triggers is not possible in Spring 3.0. 在Spring 3.0中,似乎无法使用XML配置两个标准触发器中的任何一个。 It has been added as a new feature in the 3.1M2 release, though: https://jira.springsource.org/browse/SPR-8205 不过,它已作为3.1M2版本中的新功能添加: https ://jira.springsource.org/browse/SPR-8205

Thanks to Mark Fisher for pointing this out . 感谢Mark Fisher 指出了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM