【问题标题】:How to refresh spring integration poller with @RefreshScope?如何使用 @RefreshScope 刷新 spring 集成轮询器?
【发布时间】:2019-01-24 21:19:31
【问题描述】:

我有一个使用 Spring Cloud Config 来刷新其属性的 Spring Boot 应用程序。我可以使用@RefreshScope 轻松刷新我的控制器,但我不确定如何为我的poller 执行相同操作以重新启动我的 Spring 集成工作。

我的 integration-config.xml :

<context:property-placeholder location="file:///C:/workspace/config/tasky-dev.properties" />

<int:inbound-channel-adapter ref="tasksService" method="requestAllTasks" channel="initTimestampChannel">
    <int:poller fixed-rate="${start.task.rate}"></int:poller>
</int:inbound-channel-adapter>

如果我更改start.task.rate,然后点击/refresh,执行器会检测到更改,但我的poller 没有收到任何内容。有没有办法为它定义某种@RefreshScope

我的tasky-dev.properties

start.task.rate=600000

我的应用程序.java:

@SpringBootApplication
@EnableConfigServer
@ImportResource("classpath:integration-config.xml")
@EnableSwagger2
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

=======

更新:

通过设置 PeriodicTrigger 尝试 Artem 的解决方案。范围会刷新,但仅在调用轮询器时(一旦经过 fixedRate 持续时间):

@RefreshScope
@Bean
public PeriodicTrigger refreshablePeriodicTrigger() {
    PeriodicTrigger periodicTrigger = new PeriodicTrigger(fixedRate);
    periodicTrigger.setFixedRate(true);
    return periodicTrigger;
}

还有:

<int:inbound-channel-adapter ref="tasksService" method="requestAllTasks" channel="initTimestampChannel">
    <int:poller trigger="refreshablePeriodicTrigger"></int:poller>
</int:inbound-channel-adapter>

【问题讨论】:

    标签: spring spring-boot spring-integration spring-cloud spring-cloud-config


    【解决方案1】:

    嗯,方便的&lt;poller&gt; 实质上注册了一个Trigger 对象,供TaskScheduler.schedule(Runnable task, Trigger trigger) 使用。

    我可以建议您在 @Configuration 中使用 @RefreshScope 注册一个 PeriodicTrigger bean,并在 &lt;poller&gt; 定义而不是 fixed-rate 属性中使用它。

    【讨论】:

    • 我已经更新了我的帖子。这几乎就是我想要做的。使用您的解决方案,范围会刷新,但只有在调用轮询器时(一旦 fixedRate 持续时间已过),如何在刷新 fixedRate 时立即重新初始化 refreshablePeriodicTrigger
    • 嗯,你可以在刷新期间stop()&lt;int:inbound-channel-adapter&gt;,但是正在进行的任务无论如何都会完成。
    • 感谢 Artem 的所有回答。我终于意识到我所做的一切都适得其反,我至少有 20 个其他元素使用了我的 XML 中的属性,我需要对他们每个人都做同样的事情。如果我希望能够刷新我的 bean,那么只切换到 Java DSL 配置会更加容易和清洁。我认为会有一些 Spring Integration 神奇的药丸可以让我的所有 bean 都可以直接从我的 XML 刷新。
    • 是的……不幸的是,事实并非如此。 XML 被解析为一组 bean,在运行时我们的模型与 XML DSL 的模型略有不同。此外 RefreshScope 是在 XML 配置之后才引入的,看起来它无论如何都是为 Java 配置设计的
    猜你喜欢
    • 2016-08-06
    • 2014-12-06
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-02-16
    相关资源
    最近更新 更多