我找不到用于轮询 JPA 源以获取入站数据的有用示例。我知道如何在 XML 中做到这一点,但无法弄清楚如何在 DSL 中做到这一点。
简而言之,我想做的是定期轮询 JPA 存储库以获取记录,然后将记录放入一个流中,该流将执行通常的过滤/转换/执行。
亲切的问候
大卫史密斯
我找不到用于轮询 JPA 源以获取入站数据的有用示例。我知道如何在 XML 中做到这一点,但无法弄清楚如何在 DSL 中做到这一点。
简而言之,我想做的是定期轮询 JPA 存储库以获取记录,然后将记录放入一个流中,该流将执行通常的过滤/转换/执行。
亲切的问候
大卫史密斯
您是对的:Spring Integration Java DSL 中还没有 JPA 组件支持。随意就此事提出JIRA(JavaDSL
组件),我们会处理这个需求。也可以随意贡献!
同时,我可以帮助您弄清楚如何在没有高级 API 的情况下做到这一点。
它<int-jpa:inbound-channel-adapter>
基于JpaPollingChannelAdapter
andJpaExecutor
对象(正是我们将用于 DSL API 的对象)。您只需要像这样配置@Bean
和JpaExecutor
使用它:
@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
是一种责任。因此,您应该指定第二个 Lambda 参数,配置and以便能够为您注入并根据您的目的使用它进行操作。这真的可以在运行时使用 from to 。Endpoint
SourcePollingChannelAdapter
.autoStartup()
.id()
SourcePollingChannelAdapter
JpaPollingChannelAdapter
id
control-bus
start()/stop()
是的,我同意JpaPollingChannelAdapter
这个类的名字很不幸,因为它实际上是一个MessageSource
实现。
将 a 连接JpaPollingChannelAdapter
为 a@Bean
并使用
IntegrationFlows.from(jpaMessageSource(),
c -> c.poller(Pollers.fixedDelay(1000)))
.transform(...)
...
有关配置选项,请参阅DSL 参考。
这个在顶部附近(具有不同的消息源)。