简体   繁体   English

如何使用JMS和Spring Integration(2.2)最好地实现请求/答复

[英]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. 虽然上面的配置“有效”,但我有以下保留。

  1. 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时不会触发)。

  2. 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-gatewayint: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。

  1. You don't need to configure reply channels; 您不需要配置回复渠道; by default the jms gateway (with no reply channel) will return the message to the inbound gateway automatically. 默认情况下,jms网关(无回复通道)将自动将消息返回到入站网关。
  2. When using direct channels, the messaging gateway's timeout only starts when the thread tries to receive any reply that was returned from the flow. 使用直接通道时,消息传递网关的超时仅在线程尝试接收从流返回的任何答复时才开始。

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.

相关问题 没有回复侦听器的Spring Integration JMS Gateway - Spring Integration JMS Gateway without reply listener 使用Spring Integration为请求/响应创建命名的回复目标 - Creating a named reply destination for request/response using Spring Integration 仅使用Java(JavaConfig)如何实现Spring集成的Content Enricher? - How is a Spring integration Content Enricher implemented using Java only (JavaConfig)? 客户端可以使用 spring kafka API 和服务器端使用 apache kafka API 来实现 spring 请求回复吗? - Can spring request reply can be implemented with client side using spring kafka API's and server side using apache kafka API? 使用Java Config的Spring Integration和JMS - Spring Integration and JMS using Java Config 如何在 spring 集成消息中设置 JMS Header - How to set JMS Header in spring integration message 在ActiveMQ中使用Spring Integration JMS实现发布订阅 - Implementing Publish Subscribe using Spring Integration JMS in ActiveMQ 使用选择器的Spring Integration JMS消息驱动的通道适配器 - spring integration JMS message driven channel adaptor using selector 骆驼JMS请求回复问题与远程MessageListener - Camel JMS Request Reply issue with Remote MessageListener 与JMS glassfish MQ的春季集成 - Spring integration with JMS glassfish MQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM