从Rabbit Template交换消息

时间:2013-05-05 15:05:06

标签: java spring rabbitmq

我以这种方式使用rabbitTemplate

<bean id="rabbitConnFactory" class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
    <constructor-arg>
        <value>localhost</value>
    </constructor-arg>
</bean>

<!-- Spring AMQP Template -->
<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
    <property name="connectionFactory" ref="rabbitConnFactory" />
</bean>

<!-- Spring AMQP Admin -->
<bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
    <constructor-arg ref="rabbitConnFactory" />
</bean>

发送到交流:

rabbitTemplate.setExchange(exchange);
rabbitTemplate.convertAndSend(message);

从交换中阅读:

rabbitTemplate.setExchange(exchange);
rabbitTemplate.setQueue(exchange);                //?
Object msg = rabbitTemplate.receiveAndConvert();
return msg;

(我需要设置Exchange运行时,因为我是从数据库中获取的)

msg始终为null

1 个答案:

答案 0 :(得分:0)

根据documentation,如果队列中没有任何内容,则receiveAndConvert方法可以返回null