一条消息应该如何看起来像是一个响应
how should a message look like to be a response
我已阅读并遵循 http://docs.spring.io/spring-integration/reference/html/ip.html#ip-correlation
中的示例
我有一个 spring-集成服务器
<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 期望什么,是否有一些 documentation/specification 用于该特殊用例(与非 spring 客户端的 sendAndReceive 通信)。
你说的
是什么意思
channels "sendAndReceive"
?
您正在使用哪些 API?除非您使用网关,否则您将负责回复相关性。
TCP Client-Server Multiplex Sample 展示了一种在不使用网关时关联回复的技术(聚合器)。
显示您的完整配置(编辑问题,不要添加评论;它呈现效果不佳)。
编辑:
框架无法自动为您进行关联(template.sendAndReceive()
)。它对您的自定义 headers 一无所知,而且无论如何,都无法在该消息的模板中获取对自动回复 queue 的引用。
您必须编写自己的代码来进行关联。
template.send(...);
// wait for incoming message with the same correlation id
例如,您可以使用 Map<String, BlockingQueue>
,由相关 ID 和 poll
键控作为响应。
通过 <service-activator/>
调用 class 中的另一个方法,put
对相关 ID 的 BlockingQueue
回复。您还可以使用 QueueChannel
s.
的地图
这个用例(阻塞线程等待一些异步事件)已经出现好几次了,我们 considering adding a component to make things easier。
我已阅读并遵循 http://docs.spring.io/spring-integration/reference/html/ip.html#ip-correlation
中的示例我有一个 spring-集成服务器
<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 期望什么,是否有一些 documentation/specification 用于该特殊用例(与非 spring 客户端的 sendAndReceive 通信)。
你说的
是什么意思channels "sendAndReceive"
?
您正在使用哪些 API?除非您使用网关,否则您将负责回复相关性。
TCP Client-Server Multiplex Sample 展示了一种在不使用网关时关联回复的技术(聚合器)。
显示您的完整配置(编辑问题,不要添加评论;它呈现效果不佳)。
编辑:
框架无法自动为您进行关联(template.sendAndReceive()
)。它对您的自定义 headers 一无所知,而且无论如何,都无法在该消息的模板中获取对自动回复 queue 的引用。
您必须编写自己的代码来进行关联。
template.send(...);
// wait for incoming message with the same correlation id
例如,您可以使用 Map<String, BlockingQueue>
,由相关 ID 和 poll
键控作为响应。
通过 <service-activator/>
调用 class 中的另一个方法,put
对相关 ID 的 BlockingQueue
回复。您还可以使用 QueueChannel
s.
这个用例(阻塞线程等待一些异步事件)已经出现好几次了,我们 considering adding a component to make things easier。