如何在 spring 集成中动态更改 InboundChannelAdapter 的轮询器 cron

How to dynamically change the poller cron for InboundChannelAdapter in spring integration

我看了很多,这是我的配置,我怎样才能动态地改变 poller cron?例如,当应用程序是 运行 并且我更改了数据库中的轮询器 cron 时,它应该由 InboundChannelAdapter 中的轮询器拾取。

注意:我不使用 spring 云配置,所以 @RefreshScope 不是一个真正的选项

@Bean
@InboundChannelAdapter(channel = "sftpStreamChannel", poller = @Poller(cron = "${pollerCron}", maxMessagesPerPoll = "-1"))
public MessageSource<InputStream> sftpMessageSource()
{
    SftpStreamingMessageSource source = new SftpStreamingMessageSource(template());
    source.setRemoteDirectory(sftpRemoteDirectory);
    source.setFilter(abSftpFileFilter());
    return source;
}

您不能动态更改 cron 表达式;该框架确实提供了一个DynamicPeriodicTrigger,可用于在运行时更改固定延迟或固定速率(但更改直到下一次轮询才会生效)。

您可能还会发现智能轮询器可能适合您的用例 - 请参阅 "Smart" Polling,其中轮询器可以决定是否继续进行轮询。

您也可以创建自己的 Trigger 来包装 CronTrigger 并委托给它;这将允许您在运行时更改它。但是,同样,更改要到下一次投票才会生效。