Spring 集成 Jpa |使用 MessagingGateway 插入行

Spring integration Jpa | To insert row with MessagingGateway

我创建了 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));
 }

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

查看文档: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 时,这意味着没有预期的回复,只要发送成功,您的程序就会退出此块。