【问题标题】:how should a message look like to be a response消息应该如何成为响应
【发布时间】:2015-06-15 09:10:29
【问题描述】:

我已阅读并遵循http://docs.spring.io/spring-integration/reference/html/ip.html#ip-correlation 中的示例

我有一个弹簧集成服务器

<int-ip:tcp-connection-factory
    id="socketserver"
    type="server"
    port="30124"
    using-nio="true"
    mapper="mapper"
    deserializer="jsonSerializer"
    serializer="jsonSerializer"
    single-use="false"/>

使用上面的映射器link:

<bean id="mapper"
  class="org.springframework.integration.ip.tcp.connection.MessageConvertingTcpMessageMapper">
<constructor-arg name="messageConverter">
    <bean class="org.springframework.integration.support.converter.MapMessageConverter">
        <property name="headerNames">
            <list>
                <value>correlationId</value>
                <value>sequenceNumber</value>
                <value>sequenceSize</value>
            </list>
        </property>
    </bean>
</constructor-arg>

带有入站和出站适配器

<int-ip:tcp-inbound-channel-adapter id="inboundServer"
    channel="inputChannel"
    connection-factory="socketserver"/>

<int-ip:tcp-outbound-channel-adapter id="outboundServer"
    channel="outputChannel"
    connection-factory="socketserver"/>

<int:channel id="inputChannel"/>
<int:channel id="outputChannel"/>

<int:service-activator input-channel="inputChannel"
           output-channel="outputChannel"
           ref="echoService"
           method="test"/>

<bean id="echoService" class="com.example.HelloReply" />

我正在向已连接的客户端发送消息

Map<String, Object> headers = new HashMap<String, Object>();
Map msgMap = new HashMap();
msgMap.put("message", "test");
headers.put(IpHeaders.CONNECTION_ID, <the connection id>);
headers.put("correlationId", "boo");
headers.put("sequenceNumber", 100);
headers.put("sequenceSize", 5);
Message<Map> msg = new GenericMessage<Map>(msgMap, headers);

MessagingTemplate template = new MessagingTemplate();

Message reply = template.sendAndReceive(outputChannel, msg);

最后一行永远阻塞,因为我不知道如何从我的旧客户端 (telnet) 正确回复该消息。

发送的消息是:

{"headers":{"sequenceNumber":101,"sequenceSize":5,"correlationId":"boo"},"payload":{"message":"test"}}

我认为这将是correlationId和相同的sequenceNumber匹配所以我尝试从客户端响应到服务器

{"headers":{"sequenceNumber":101,"sequenceSize":5,"correlationId":"boo"},"payload":{"result":"OK"}}

但它被解释为新的入站消息,不被识别为初始发送消息的答案。

那么 sendAndReceive 期望什么,是否有针对该特殊用例的一些文档/规范(sendAndReceive 与非 Spring 客户端的通信)。

【问题讨论】:

    标签: java tcp spring-integration


    【解决方案1】:

    什么意思

    频道“sendAndReceive”

    您使用哪些 API?除非您使用网关,否则您负责回复关联。

    TCP Client-Server Multiplex Sample 展示了一种在不使用网关时关联回复的技术(聚合器)。

    显示您的完整配置(编辑问题,不要添加评论;渲染效果不佳)。

    编辑

    框架无法自动为您进行关联(使用template.sendAndReceive())。它对您的自定义标头一无所知,并且无论如何,无法在此消息的模板中获取对自动回复队列的引用。

    您必须编写自己的代码来进行关联。

    template.send(...);
    // wait for incoming message with the same correlation id
    

    例如,您可以使用Map&lt;String, BlockingQueue&gt;,由相关ID 和poll 作为响应键。

    通过&lt;service-activator/&gt;put 调用类中的另一个方法,回复到BlockingQueue 的相关ID。您也可以使用QueueChannels 的地图。

    这个用例(阻塞等待某个异步事件的线程)已经出现了好几次,我们是considering adding a component to make things easier

    【讨论】:

    • 感谢您的回复。我编辑了这个问题,希望现在能更好地理解。我知道我有责任回复,但我不知道该回复什么。
    • 啊,好吧,那我误解了文档。我认为有一种标准化的方式在发送和接收中具有正确的标头。非常感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多