【问题标题】:Are channels or gateways what I'm looking for?我正在寻找渠道或网关吗?
【发布时间】:2014-06-06 12:21:30
【问题描述】:

我刚刚开始掌握 Spring Integration,想知道我对此的解释是否正确。

据我了解,通道是单向的,网关是双向的。

网关的双向性是请求响应吗?还是网关允许全双工通信?

基本上,我必须编写一个应用程序,其中两个组件必须进行通信,但不是简单的请求/响应模式的通信,它更多的是一个请求,然后是多个响应,每个响应都必须依次处理,然后转发到第三个系统。

我可以使用网关执行此操作,还是应该设置请求通道和响应通道?

如果这没有意义,我很乐意详细说明。

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    是的,您的理解是正确的,是的,网关是请求/响应。

    如果您想要组件之间的异步通信(例如,一个请求的回复不止一个),那么,是的,您需要一对通道。

    但是,您可以通过使用“无效”网关和入站通道适配器来避免将消息传递基础架构暴露给您的组件。网关方法返回 void(无响应)并且出站通道适配器调用您的 pojo“响应”方法。

    编辑:

    此示例通过网关将数组发送到 Spring Integration 流中,并将各个元素作为一系列“响应”返回...

    public class Foo {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("foo-context.xml", Foo.class);
            context.getBean(Sender.class).send(new String[] {"foo", "bar", "baz"});
            context.close();
        }
    
        public interface Sender {
    
            void send (String[] foo);
        }
    
        public static class Bar {
    
            public void receive(String reply) {
                System.out.println(reply);
            }
    
        }
    
    }
    

    使用 foo-context.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:int="http://www.springframework.org/schema/integration"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.0.xsd">
    
        <int:gateway service-interface="foo.Foo$Sender"
            default-request-channel="split" />
    
        <int:channel id="split" />
    
        <int:splitter input-channel="split" output-channel="replies" />
    
        <int:channel id="replies" />
    
        <int:outbound-channel-adapter channel="replies" method="receive">
            <bean class="foo.Foo$Bar"/>
        </int:outbound-channel-adapter>
    
    </beans>
    

    【讨论】:

    • 你能提供一个如何做到这一点的例子吗?当涉及到 Spring 集成时,我遭受了一点 TMI 的困扰,我的头脑有些混乱。也是星期五,这是一个漫长的一周。
    • 我的意思是outbound-channel-adapter - 我的答案中添加了示例。
    • 我错过了什么,拆分和响应是由 Spring Integration 完成的吗?对不起,如果我有点昏暗。
    • 是的;这只是一个简单的例子,展示了如何从一个请求中接收多个回复;在这种情况下,我们只是将数组拆分为多个部分。真正的应用程序可能会更复杂。
    • 谢谢我得到了这个工作,并在此基础上用我自己的类替换了适配器。双向通信,我们开始!
    猜你喜欢
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2012-02-15
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    相关资源
    最近更新 更多