我正在尝试使用 spring 集成 jpa-inbound-channel-adapter 从数据库中获取记录并对它们执行一组操作。我还需要确保同时运行的实例在任何给定时间点都不会多次获得相同的记录。
正如我检查的,下面的文档介绍了如何配置 jpa-inbound-channel-adapter 来处理事务,
<int-jpa:inbound-channel-adapter
channel="inboundChannelAdapterOne"
entity-manager="em"
auto-startup="true"
jpa-query="select s from Student s"
expect-single-result="true"
delete-after-poll="true">
<int:poller fixed-rate="2000" >
<int:transactional propagation="REQUIRED"
transaction-manager="transactionManager"/>
</int:poller>
</int-jpa:inbound-channel-adapter>
我还没有找到任何方法可以在 Spring Boot 应用程序中使用 Java 配置来实现相同的目的(没有 xml 配置)。我可以看到 Java 配置示例,但没有一个是事务性的。任何指示都会有所帮助。
最佳答案
参见the configuration for the test cases .
只需将 .transactional()
添加到端点:
@Bean
public IntegrationFlow pollingAdapterFlow(EntityManagerFactory entityManagerFactory) {
return IntegrationFlows
.from(Jpa.inboundAdapter(entityManagerFactory)
.entityClass(StudentDomain.class)
.maxResults(1)
.expectSingleResult(true),
e -> e.poller(p -> p.trigger(new OnlyOnceTrigger())
.transactional()))
.channel(c -> c.queue("pollingResults"))
.get();
}
关于java - 带有事务轮询器 Java 配置的 Spring Integration JPA 入站 channel 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57213448/