我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;
(我需要设置交换运行时,因为我是从数据库中获取的)
但msg
总是null
。