java - 带有事务轮询器 Java 配置的 Spring Integration JPA 入站 channel 适配器

标签 java spring-boot spring-data-jpa spring-integration

我正在尝试使用 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/

相关文章:

java - 如何避免返回零

java - 如何在 CQ5 中执行 JSP 之前调用 Servlet/Filter?

spring - 在IDE中运行Spring Boot时找不到JSP,但手动运行时可以工作

java - 我的问题是关于从 groovy 中的日期转换纪元毫秒

postgresql - 使用 JPA 而不是 native 查询在 postgres 中获取序列的下一个值

java - 将变量值从 1 个 jsp 页面传输到另一个 jsp 页面

java - Java中的快速自动装箱/自动拆箱问题

java - 使用 Spring MongoTemplate 自动生成数组中 MongoDB 子文档的 ID

java - 使用 Criteria API 加入,在实体映射 "one to one"中

java - 如何在 REST 服务中进行分页?