用spring spring和rabbit mq编写样本生产者和消费者

时间:2014-02-12 16:36:43

标签: rabbitmq spring-integration

我尝试编写样本生产者&使用SpringIntegration和RabbitMQ的消费者。

这是我的rabbit.xml配置文件:

<rabbit:connection-factory id="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="queue.request" />
<rabbit:queue name="queue.response" />
<rabbit:direct-exchange name="exchange.main">
    <rabbit:bindings>
        <rabbit:binding queue="queue.request" key="binding.queue.request"/>
        <rabbit:binding queue="queue.response" key="binding.queue.response" />
    </rabbit:bindings>
</rabbit:direct-exchange>

producer.xml文件:

<context:annotation-config />
<import resource="classpath:META-INF/zcore/integration/rabbit.xml" />
<int:channel id="request" />
<int:channel id="response" />
<int-amqp:outbound-channel-adapter channel="request" amqp-template="amqpTemplate" 
    exchange-name="exchange.main" routing-key="binding.queue.request"       
<int-amqp:inbound-channel-adapter channel="response" queue-names="queue.response"
     connection-factory="connectionFactory" />  
<int:gateway id="baseGateway" service-interface="org.zcoreframework.integration.gateway.BaseGateway"
    default-request-channel="request" default-reply-channel="response" />   

consumer.xml文件:

<import resource="classpath:META-INF/zcore/integration/rabbit.xml" />
<context:annotation-config />
<context:component-scan base-package="org.zcoreframework" />
<int:channel id="request" /> 
<int:channel id="response" />
<int-amqp:inbound-channel-adapter channel="request" queue-names="queue.request"
     connection-factory="connectionFactory" />  
<int-amqp:outbound-channel-adapter channel="response" amqp-template="amqpTemplate" 
    exchange-name="exchange.main" routing-key="binding.queue.response" />
<int:service-activator ref="messageConsumer" method="onMessage" input-channel="request"
    output-channel="response" />

我为发送消息编写了这段剪切代码并获得响应:

@Autowired
BaseGateway baseGateway;

@Test
public void testHelloWorld() {

        CallMethodMessage callMethodMessage = new CallMethodMessage();
        callMethodMessage.setMethod("test");
        callMethodMessage.setArgs(null);
        System.out.print("send & receive ");            
        ReturnModel returnModel = this.baseGateway.SendWait(callMethodMessage);         
        //this.baseGateway.FireForget(callMethodMessage);
}

这是我的界面

public interface BaseGateway {

    @Gateway
    public void FireForget(CallMethodMessage method);

    @Gateway
    public ReturnModel SendWait(CallMethodMessage method);

}

好了,现在我分别运行消费者,然后第一个用生命方式FireForget运行生产者,一切正常,cosumer得到它并打印一条消息但是当我用SendWait方法运行时,cosumer得到它但不返回任何东西和生产者等待得到答复,我该如何处理这个问题?

1 个答案:

答案 0 :(得分:2)

回复

没有相关性
<int-amqp:outbound-channel-adapter channel="request" amqp-template="amqpTemplate" 
    exchange-name="exchange.main" routing-key="binding.queue.request"       
<int-amqp:inbound-channel-adapter channel="response" queue-names="queue.response"
     connection-factory="connectionFactory" />  

使用出站网关;您可以使用<reply-listener/>discussed in the Spring AMQP documentation的兔子模板对其进行配置。

在消费者端使用入站网关。