我以这种方式使用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
。