弹簧集成 JPA |使用消息网关插入行



我创建了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调用。此类组件没有回复以返回到网关调用。

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

最新更新