在集成流程(Spring Integration)中包含数据持久化的最佳实践。

huangapple go评论71阅读模式
英文:

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 -&gt;
        flowDefinition
            .filter(filterUnwantedMessages)
            .transform(messageTransformer)            
            .wireTap(flow -&gt; flow.trigger(messagePayloadPersister)) &lt;--- 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 -&gt;
        flowDefinition
            .filter(filterUnwantedMessages)
            .transform(messageTransformer)            
            .handle(messagePayloadPersister)
            .handle(terminalHandler);
  }

  
  @Component
  class MesssagePayloadPersister {
	@ServiceActivator  &lt;--- interesting, but..
	public Message handle(Message&lt;?&gt; 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

以下是翻译好的内容:

需要了解您将来如何处理持久化的数据。

以及您计划从消息中存储哪些信息(或者是否全部存储消息)。

查阅文档的以下部分 - 也许会给您一些想法:

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/system-management.html#message-store

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/system-management.html#metadata-store

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/message-transformation.html#claim-check

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/core.html#persistent-queuechannel-configuration

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/jdbc.html#jdbc-outbound-channel-adapter

对于最后一个链接,您可能需要考虑使用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:

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/system-management.html#message-store

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/system-management.html#metadata-store

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/message-transformation.html#claim-check

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/core.html#persistent-queuechannel-configuration

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/jdbc.html#jdbc-outbound-channel-adapter

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.

huangapple
  • 本文由 发表于 2020年10月9日 17:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64277129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定