【问题标题】:Spring integration Jpa | To insert row with MessagingGatewaySpring 集成 Jpa |使用 MessagingGateway 插入行
【发布时间】:2020-07-06 13:47:33
【问题描述】:

我创建了 MessagingGateway 和 2 个流。我得到了正确的清单。当我创建人时,程序会休眠。 为什么?我该如何解决这个问题?

MyService myService = context.getBean(MyService.class);
System.out.println("persons = " + myService.getPersons());
System.out.println("person = " + myService.save(new Person(0, "Alex")));

@MessagingGateway
public interface MyService {
    @Gateway(requestChannel = "flow1.input")
    @Payload("new java.util.Date()")
    Collection<Person> getPersons();

    @Gateway(requestChannel = "flow2.input")
    Person save(Person person);
}

@Bean
public IntegrationFlow flow1(EntityManagerFactory entityManagerFactory) {
    return f -> f
            .handle(Jpa.retrievingGateway(entityManagerFactory)
                    .jpaQuery("from Person")
            );
}

 @Bean
 public IntegrationFlow flow2(EntityManagerFactory entityManagerFactory) {
        return f -> f
                .handle(Jpa.outboundAdapter(entityManagerFactory)
                                .entityClass(Person.class)
                                .persistMode(PersistMode.PERSIST),
                        e -> e.transactional(true));
 }

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    因为出站通道适配器是一个one-way 调用。此类组件没有回复您的网关调用。

    请参阅文档:https://docs.spring.io/spring-integration/docs/current/reference/html/endpoint-summary.html#endpoint-summary 和理论:

    https://www.enterpriseintegrationpatterns.com/patterns/messaging/ChannelAdapter.html https://www.enterpriseintegrationpatterns.com/patterns/messaging/MessagingGateway.html

    您应该考虑将您的网关方法合同Person save(Person person); 更改为此void save(Person person);。当网关为void 时,意味着没有预期的回复,只要发送成功,您的程序就会退出此块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2014-11-14
      • 1970-01-01
      • 2021-07-05
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多