简体   繁体   English

Spring集成流程与常规服务和适配器

[英]Spring integration flow vs regular service and adapter

I had some legacy code that had SOAP services. 我有一些带有SOAP服务的旧代码。 Now I am building Rest API for some objects that may call one or more SOAP operation. 现在,我正在为可能调用一个或多个SOAP操作的某些对象构建Rest API。 I was looking into Spring Integration. 我一直在研究Spring Integration。 From the docs 来自文档

In addition to wiring together fine-grained components, Spring Integration provides a wide selection of channel adapters and gateways to communicate with external systems. 除了将细粒度的组件连接在一起之外,Spring Integration还提供了多种通道适配器和网关来与外部系统进行通信。

Above statement sounds enticing. 以上声明听起来很诱人。 I was writing rest microservice controller, Validation service, Rest request to SOAP request mapper and SOAP client. 我正在编写Rest微服务控制器,Validation服务,REST请求到SOAP请求映射器和SOAP客户端。 I some cases when there are multiple calls, there is even more code I had to write and I did write the code in many cases. 在某些情况下,当有多个调用时,我不得不编写更多的代码,并且在很多情况下确实编写了代码。

Spring Integration at high level looked like a framework oriented for Async messages. 高层的Spring Integration看起来像是面向异步消息的框架。 My problem is that the call need to be more or less a synchronous call and performance is critical. 我的问题是该呼叫或多或少需要是同步呼叫,而性能至关重要。 Had anyone used Spring integration for this problem and can you share your experiences. 有没有人使用Spring integration解决此问题,您能否分享您的经验。

To complement Artem's answer it's worth to note that if you're going to use one of Spring Integration DSL s (Java, Groovy or Scala) then (the synchronous) DirectChannel will be picked by default by Spring Integration to wire up the endpoints of your integration flow. 作为对Artem 答案的补充,值得注意的是,如果您要使用Spring Integration DSL之一 (Java,Groovy或Scala),则默认情况下,Spring Integration将选择(同步) DirectChannel您的端点整合流程。 This means that as long as your endpoints stay synchronous and you rely on default channels between them, the whole integration flow stay synchronous as well. 这意味着只要您的端点保持同步并且您依赖它们之间的默认通道,整个集成流程也将保持同步。

For instance (in Java DSL ): 例如(在Java DSL中 ):

  @Bean
  public IntegrationFlow syncFlow() {
    return IntegrationFlows
        .from(/* get a REST message from microservice */)
        // here the DirectChannel is used by default 
        .filter(/* validate (and filter out) incorrect messages */)
        // here the DirectChannel is used by default too
        .transform(/* map REST to SOAP */)
        // guess what would be here?
        .handle(/* send a message with SOAP client */)
        .get();
  }

This absolutely doesn't mean you tied up with synchronous flow forever. 这绝对不意味着您将永远与同步流捆绑在一起。 At any step you can go async or parallel. 您可以在任何步骤进行异步或并行处理。 For example, if you decide to send SOAP messages in parallel all you need to do is to specify appropriate channel before SOAP client invocation: 例如,如果决定并行发送SOAP消息,那么您要做的就是在SOAP客户端调用之前指定适当的通道:

  @Bean
  public IntegrationFlow syncFlow() {
        // ... the same as above ...
        .transform(/* map REST to SOAP */)
        .channel(c -> c.executor(Executors.newCachedThreadPool()))  // see (1)
        .handle(/* send a message with SOAP client */)
        .get();
  }

(1) From this point on the downstream flow will be processed in parallel thanks to use of ExecutorChannel . (1)从现在开始,由于使用了ExecutorChannel,将并行处理下游流。

Note that message endpoints may also behave asynchronously depending on their logic . 请注意,消息端点还可能根据其逻辑异步运行。

I've used Spring Integration for building synchronous integration flows in my home and work projects and it's proven to be a very powerful yet flexible solution. 我已经使用Spring Integration在我的家庭和工作项目中构建同步集成流程,并且事实证明这是一个非常强大而灵活的解决方案。

One of the first class citizens in Spring Integration is MessageChannel abstraction. Spring Integration中的头等公民之一是MessageChannel抽象。 The simplest, synchronous, and therefore direct method invocation is DirectChannel . 最简单,同步且因此直接的方法调用是DirectChannel

Not sure what makes you think that everything in Spring Integration is async. 不知道是什么让您认为Spring Integration中的所有内容都是异步的。 Actually it is always direct unless you tell to be async. 实际上,除非您告诉自己是异步的,否则它总是直接的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM