Spring Integration - 设置发送消息的上限

时间:2018-01-26 17:29:29

标签: java spring spring-integration

我正在撰写回复收到邮件的邮件服务,我想设置限制 - 每小时最多发送10封邮件。它也应该在整个小时重置。

Spring Integration是否可行?我在Spring文档中阅读了有关任务调度的内容,但我不确定这是否符合我的情况。我想我可以设置一个以固定速率发送消息的CronTrigger,但这并不像我实际想要实现的那样。

提前致谢。

1 个答案:

答案 0 :(得分:2)

我认为你走的是正确的。

您使用QeueueChannel配置PollingConsumer@ServiceActivator@Poller)。你肯定可以去那里的cron触发器:

/**
 * @return The cron expression to create the {@link CronTrigger}.
 * Can be specified as 'property placeholder', e.g. {@code ${poller.cron}}.
 */
String cron() default "";

您对max 10 sent messages/hour的要求可通过以下方式实现:

/**
 * @return The maximum number of messages to receive for each poll.
 * Can be specified as 'property placeholder', e.g. {@code ${poller.maxMessagesPerPoll}}.
 * Defaults to -1 (infinity) for polling consumers and 1 for polling inbound channel adapters.
 */
String maxMessagesPerPoll() default "";

因此,当调度程序根据配置的cron时间执行任务时,只会从maxMessagesPerPoll中提取已配置的QeueueChannel并向下游发送以进行处理(发送电子邮件)。

https://docs.spring.io/spring-integration/docs/5.0.0.RELEASE/reference/html/messaging-channels-section.html#polling-consumer中查看更多信息,并寻找Important: Poller Configuration段。