【发布时间】:2015-06-12 08:50:59
【问题描述】:
我找不到用于轮询 JPA 源以获取入站数据的有用示例。我知道如何在 XML 中做到这一点,但不知道如何在 DSL 中做到这一点。
简而言之,我想做的是定期轮询 JPA 存储库以获取记录,然后将记录放入一个流中,该流将执行通常的过滤/转换/执行。
亲切的问候
大卫·史密斯
【问题讨论】:
标签: spring-data-jpa spring-integration spring-dsl
我找不到用于轮询 JPA 源以获取入站数据的有用示例。我知道如何在 XML 中做到这一点,但不知道如何在 DSL 中做到这一点。
简而言之,我想做的是定期轮询 JPA 存储库以获取记录,然后将记录放入一个流中,该流将执行通常的过滤/转换/执行。
亲切的问候
大卫·史密斯
【问题讨论】:
标签: spring-data-jpa spring-integration spring-dsl
你是对的:Spring Integration Java DSL 中还没有 JPA 组件支持。请随时就此事提出JIRA(JavaDSL 组件),我们会处理此需求。也欢迎contribute!
同时,我可以帮助您弄清楚如何在没有高级 API 的情况下做到这一点。
<int-jpa:inbound-channel-adapter> 基于 JpaPollingChannelAdapter 和 JpaExecutor 对象(正是我们将用于 DSL API 的对象)。您只需为JpaExecutor 配置@Bean 并像这样使用它:
@Bean
public JpaExecutor jpaExecutor(EntityManagerFactory entityManagerFactory) {
JpaExecutor jpaExecutor = new JpaExecutor(entityManagerFactory);
jpaExecutor.setJpaQuery("from Foo");
....
return jpaExecutor;
}
@Bean
public IntegrationFlow jpaFlow(JpaExecutor jpaExecutor) {
return IntegrationFlows.from(new JpaPollingChannelAdapter(jpaExecutor))
.split()
.transform()
....
}
对于现有的 DSL 组件 API,其他一切都将由框架完成。
更新
如何在以编程方式创建 JpaPollingChannelAdapter 时提供 auto-startup= 属性?另外,是否可以使用控制总线获取此 bean 并调用 .start()、.stop()?
看,加里的回答。 Lifecycle 控件是Endpoint 的责任,在我们的例子中是SourcePollingChannelAdapter。因此,您应该指定第二个 Lambda 参数,配置 .autoStartup() 和 .id() 以便能够为您的 JpaPollingChannelAdapter 注入 SourcePollingChannelAdapter 并根据您的目的使用它进行操作。 id 真的可以在运行时从control-bus 使用到start()/stop()。
是的,我同意 JpaPollingChannelAdapter 是该类的不幸名称,因为它实际上是 MessageSource 实现。
【讨论】:
JpaPollingChannelAdapter 时如何提供auto-startup= 属性?另外,是否可以使用控制总线获取此 bean 并调用 .start()、.stop()?
将JpaPollingChannelAdapter 连接为@Bean 并使用
IntegrationFlows.from(jpaMessageSource(),
c -> c.poller(Pollers.fixedDelay(1000)))
.transform(...)
...
有关配置选项,请参阅DSL Reference。
这个在顶部附近(带有不同的消息来源)。
【讨论】: