英文:
Best practice for including data persistence inside integration flow (SpringIntegration)
问题
关于在集成流程中包含数据持久化并返回 Message 对象以便进一步处理流程的最佳实践,我有一个问题。
考虑以下流程:
@Bean
IntegrationFlow myFlow() {
return flowDefinition ->
flowDefinition
.filter(filterUnwantedMessages)
.transform(messageTransformer)
.wireTap(flow -> flow.trigger(messagePayloadPersister)) // 这里是有趣的部分
.handle(terminalHandler);
}
在大多数情况下,我在一些项目中看到的是,而不是使用 wireTap,会使用 Transformer 来持久化数据,但我不太喜欢这种做法,因为它的名称意味着消息的转换,而持久化是另一回事。
我希望找到 wireTap 的替代方法,我的一个同事提出使用 @ServiceActivator:
@Bean
IntegrationFlow myFlow() {
return flowDefinition ->
flowDefinition
.filter(filterUnwantedMessages)
.transform(messageTransformer)
.handle(messagePayloadPersister)
.handle(terminalHandler);
}
@Component
class MesssagePayloadPersister {
@ServiceActivator // 有趣,但是...
public Message handle(Message<?> msg) {
// 在某处持久化 payload..
return message;
}
}
我喜欢这个流程,看起来很清晰,但我对这个解决方案也不是100%满意,因为我在DSL中混合了Spring。
注意:org.springframework.messaging.MessageHandler 不太合适,因为 handle 方法返回 void,因此它是流程的终端部分。我需要一个返回 Message 对象的方法。
有没有办法实现这个?
英文:
My question is related to finding a best practice to include data persistence inside an integration flow while returning the Message object so that it can be further processed by the flow.
Let's consider the following flow:
@Bean
IntegrationFlow myFlow() {
return flowDefinition ->
flowDefinition
.filter(filterUnwantedMessages)
.transform(messageTransformer)
.wireTap(flow -> flow.trigger(messagePayloadPersister)) <--- here is the interesting part
.handle(terminalHandler);
}
The wide majority of cases, instead of the wireTap I have seen in some projects, a Transformer is used to persist data, which I do not particulary like, as
the name implies transformation of a message, and persistence is something else.
My wish is to find out alternatives to the wireTap, and a colleague of mine proposed using @ServiceActivator:
@Bean
IntegrationFlow myFlow() {
return flowDefinition ->
flowDefinition
.filter(filterUnwantedMessages)
.transform(messageTransformer)
.handle(messagePayloadPersister)
.handle(terminalHandler);
}
@Component
class MesssagePayloadPersister {
@ServiceActivator <--- interesting, but..
public Message handle(Message<?> msg) {
//persist the payload somewhere..
return message;
}
}
I like the flow, it looks clean now, but also I am not 100% happy with the solution, as I am mixing DSL with Spring.
Note: org.springframework.messaging.MessageHandler is not good because the handle method returns void so it is a terminal part to the flow. I need a method that returns Message object.
Is there any way to do this?
答案1
得分: 1
以下是翻译好的内容:
需要了解您将来如何处理持久化的数据。
以及您计划从消息中存储哪些信息(或者是否全部存储消息)。
查阅文档的以下部分 - 也许会给您一些想法:
对于最后一个链接,您可能需要考虑使用Java DSL中的publishSubscribeChannel()
,以便能够将数据存储在数据库中并且有第二个订阅者来继续流程。
英文:
Need to understand what you are going to do with that persisted data in the future.
And what information from the message you are going to store (or the whole message at all).
See this parts of documentation - may be something will give you some ideas:
With the last one you may need to consider to use a publishSubscribeChannel()
of the Java DSL to be able to store in the DB and have a second subscriber to continue the flow.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论