spring-integration - 如何停止或更改 Spring Integration Poller 的延迟

标签 spring-integration spring-integration-sftp

我正在使用 Spring Integration 使用以下配置从目录中读取文件。但是,我希望在找到任何文件后停止轮询,直到服务不再重新启动为止。有什么方法可以在运行时更改轮询器延迟或在运行时启动/停止轮询器?

@Bean
public MessageChannel fileInputChannel() {
    return new DirectChannel();
}

@Bean
@InboundChannelAdapter(channel = "fileInputChannel", poller = @Poller(cron = "0 0/10 19-23,0-6 ? * *", maxMessagesPerPoll = "1"))
public MessageSource<File> fileReadingMessageSource() {
    FileReadingMessageSource source = new FileReadingMessageSource();
    File directory = new File(localFtpDirectory);
    if (clearLocalDir && directory.isDirectory() && directory.exists()) {
        LOG.info("Clear directory {} on startup of service", directory);
        Arrays.stream(directory.listFiles()).forEach(File::delete);
    }
    source.setDirectory(directory);
    source.setFilter(new LastModifiedFileFilter(remoteFileFilter));
    return source;
}

@Bean
@ServiceActivator(inputChannel = "fileInputChannel")
public MessageHandler fileHandler() {
    return new MessageHandlerService();
}

最佳答案

有这个注释可以与 @InboundChannelAdapter 一起使用:

/**
 * When used alongside an EIP annotation (and no {@code @Bean}), specifies the bean name of
 * the consumer bean with the handler bean being {@code id.handler} (for a consuming
 * endpoint) or {@code id.source} for a message source (e.g. inbound channel adapter).
 * <p>
 * When there is also a {@code @Bean} annotation, this is the name of the consumer or
 * source polling bean (the handler or source gets the normal {@code @Bean} name). When
 * using on a {@code MessageHandler @Bean}, it is recommended to name the bean
 * {@code foo.handler} when using {@code @EndpointId("foo"}. This will align with
 * conventions in the framework. Similarly, for a message source, use
 * {@code @Bean("bar.source"} and {@code @EndpointId("bar")}.
 * <p>
 * <b>This is not allowed if there are multiple EIP annotations on the same method.</b>
 *
 * @author Gary Russell
 *
 * @since 5.0.4
 */
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EndpointId {

还有一个 Cotrol 总线 EIP 实现:https://docs.spring.io/spring-integration/docs/5.0.7.RELEASE/reference/html/system-management-chapter.html#control-bus .

有了它,您可以在方便的时候发送 start()/stop() 命令消息。

关于spring-integration - 如何停止或更改 Spring Integration Poller 的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51675977/

相关文章:

java - sqlparametersource刷新数据

java - 使用 Camel 发送带有自定义 MIME 消息的邮件

java - 如何为基本身份验证配置 HttpClient?

java - Spring Integration 中的 REST 端点使消息 channel 成为多线程

java - 是否可以动态更改入站 channel 适配器的文件名正则表达式

java - 如何使用spring从sftp服务器获取zip文件

java - Spring Integration Java DSL SFTP如何在处理程序中获取远程SFTP服务器信息

java - 处理 Spring 集成文件的预期返回

spring - Spring `@Configuration` 类的排序

Spring SFTP - 上传具有不同名称的文件