【发布时间】:2020-12-19 16:54:41
【问题描述】:
我尝试将集成咖啡馆演示转换为Java 8 DSL,官方Spring Integration Samples中有一些现有示例。
我想结合这些例子的所有优点。
- Java 8 DSL 代替 XML
- AMQP 将工作流拆分为较小的流程。
- 使用 Jackson 处理 JSON 格式的负载。
代码(不按预期工作,有些问题)是here。
在官方 XML 示例中,很容易在 Amqp 网关中设置请求/回复通道。但是转到 Java 8 DSL,缺少选项。
并且在申请的时候会报错“需要回复通道或者输出通道”。
顺便说一句,有没有更好的选项来调试/单元测试 Spring 集成应用程序?
更新1:我在写coldDrinks流程的时候很困惑。
XML 来自原始 cafe-amqp 项目。
<!-- To receive an AMQP Message from a Queue, and respond to its reply-to address, configure an inbound-gateway. -->
<int-amqp:inbound-gateway
id="coldDrinksBarista"
request-channel="coldJsonDrinks"
queue-names="cold-drinks"
connection-factory="rabbitConnectionFactory" />
<int:chain input-channel="coldJsonDrinks">
<int:json-to-object-transformer type="org.springframework.integration.samples.cafe.OrderItem"/>
<int:service-activator method="prepareColdDrink">
<bean class="org.springframework.integration.samples.cafe.xml.Barista"/>
</int:service-activator>
<int:object-to-json-transformer content-type="text/x-json"/>
</int:chain>
如何有效地将其转换为 Java DSL。 我在内联添加了我的想法
@Bean
public IntegrationFlow coldDrinksFlow(AmqpTemplate amqpTemplate) {
return IntegrationFlows
.from("coldDrinks")
.handle(
Amqp.outboundGateway(amqpTemplate)
.exchangeName(TOPIC_EXCHANGE_CAFE_DRINKS)
.routingKey(ROUTING_KEY_COLD_DRINKS)
)
.log("coldDrinksFlow")
.channel(preparedDrinksChannel())
.get();
}
@Bean
public IntegrationFlow coldDrinksBaristaFlow(ConnectionFactory connectionFactory, Barista barista) {
return IntegrationFlows
.from(Amqp.inboundGateway(connectionFactory, QUEUE_COLD_DRINKS)
.configureContainer(
c -> c.receiveTimeout(10000)
)// If setup replyChannel the below `handle` is not worked as expected.
)
.handle(OrderItem.class, (payload, headers) -> (Drink) barista.prepareColdDrink(payload))
//If adding a channel here, the flow will NOT return back the `coldDrinksFlow` will cause another exception, "its requiresReply is set to true..."
.get();
}
在我之前的经验中,我想通过协议(HTTP、FTP 等)作为小流中的边缘(开始时入站,结束时出站)来打破整个流程。入站/出站网关很容易设置为工作而无需设置回复通道等,默认情况下它应该使用其内置协议而不是通道通过原始路由回复。在我的inboundGateway RSocket example 中,我没有在那里设置回复通道,但是消息返回到 roscket 路由并由client side(outboudGateway) 接收。
更新:终于成功了,检查here。我在尝试使用 Amqp 发送和接收对象消息时遇到的一个问题,抛出了一些类强制转换异常,使用 handle 等 MessageHandler 时,标头中的 TypeId 没有改变,有像基于 xml 的 cafe-amqp 那样转换 bwteen json/object 以使其最终起作用。这里缺少什么?
【问题讨论】:
标签: java spring spring-boot spring-integration spring-integration-dsl