[英]How is Request/Reply best implemented using JMS and Spring Integration (2.2)
I have a simple request reply test implemented using the following configuration: 我有一个使用以下配置实现的简单请求回复测试:
<int:gateway id="myGateway"
service-interface="TestGateway"
default-request-channel="sendingChannel"
default-reply-channel="replyChannel"
default-reply-timeout="2000"
/>
<int:channel id="sendingChannel" />
<int:channel id="replyChannel" />
<int-jms:outbound-gateway id="myJmsGateway"
connection-factory="jmsConnectionFactory"
request-channel="sendingChannel"
request-destination-name="outQueue"
reply-channel="replyChannel"
reply-destination-name="outQueueReply"
receive-timeout="60000"
/>
and the Interface: 和接口:
public interface TestGateway {
@Gateway
public String requestReply(@Header("myHeaderKey") String headerValue, String data);
}
While the above configuration does "work" I have the following reservations. 虽然上面的配置“有效”,但我有以下保留。
The configuration feels redundant. 该配置感到多余。 Extra gateway and two extra channels required. 需要额外的网关和两个额外的通道。 Both gateways implement a reply timeout (although the int:gateway
timeout doesn't fire when connected to a int-jms:outbound-gateway
). 这两个网关都实现了回复超时(尽管int:gateway
超时在连接到int-jms:outbound-gateway
时不会触发)。
The semantics of the gateway method change depending on what is implementing the request/reply. 网关方法的语义根据实现请求/答复的方式而变化。 On Timeout the int-jms:outbound-gateway
will throw an exception, which will propagate to the user of TestGateway
. 在超时时, int-jms:outbound-gateway
将引发异常,该异常将传播到TestGateway
的用户。 If the config is changed to replace int-jms:outbound-gateway
the int:gateway
will return null. 如果更改配置以替换int-jms:outbound-gateway
则int:gateway
将返回null。
Given this the client code has to both handle null and the exception in the same way. 鉴于此,客户端代码必须以相同的方式处理null和异常。
Are there any better ways to wire up the gateways? 有没有更好的方法来连接网关? One option would be to change the int:channel's
to PollableChannel's
which solves problem 2 at the expense of an extra thread pool. 一种选择是将int:channel's
更改为PollableChannel's
,从而以额外的线程池为代价解决问题2。
You can avoid the different semantics (null Vs exception) by adding an error-channel
to the inbound gateway. 您可以通过向入站网关添加error-channel
来避免不同的语义(空Vs异常)。
It's important to understand that myGateway
isolates your client from the messaging system, you code to the interface only. 重要的是要了解myGateway
将您的客户端与消息传递系统隔离开来,您仅对接口进行编码。 Of course you could inject the JMS gateway directly but then you've added dependencies to your code. 当然,您可以直接注入JMS网关,但是之后您已经在代码中添加了依赖项。 With a messaging gateway, you can change technologies without making any changes to your client code. 使用消息传递网关,您可以更改技术而无需更改客户端代码。 You can also unit test your code by providing a test implementation of TestGateway
. 您还可以通过提供TestGateway
的测试实现来对代码进行单元测试。 This is a powerful feature of Spring Integration. 这是Spring Integration的强大功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.