我在为RabbitMQ消息配置固定回复队列时遇到了一些麻烦。
我有生产者和消费者,如下:
public class Producer {
private static ApplicationContext context;
private static RabbitTemplate template;
public static void main(String[] args) {
new ClassPathXmlApplicationContext("mq-producer-context.xml");
context = new ClassPathXmlApplicationContext("context.xml");
template = context.getBean(RabbitTemplate.class);
//Queue replyQueue = new Queue("ub.replyqueue");
//template.setReplyQueue(replyQueue);
}
@Scheduled(fixedRate = 1000)
public void execute() {
System.out.println("execute...");
template.convertAndSend("helloooo");
//template.convertSendAndReceive("helloooo");
}
}
消费者:
public class Consumer implements MessageListener {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("mq-consumer-context.xml");
}
public void onMessage(Message message) {
System.out.println("message received" + message);
}
/**public String handleMessage(String msg) {
return "RECEIVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}**/
}
Producer xml config :(在beans ...标签中)
<beans>
<import resource="context.xml" />
<task:scheduler id="myScheduler" pool-size="10" />
<task:annotation-driven scheduler="myScheduler" />
<bean id="producer" class="com.urbanbuz.mq.Producer"></bean>
</beans>
消费者xml配置:
<beans>
<import resource="context.xml"/>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="consumer" queue-names="ub.queue" />
</rabbit:listener-container>
<context:annotation-config />
<context:component-scan base-package="com.urbanbuz.mq" />
<aop:aspectj-autoproxy />
<bean id="consumer" class="com.urbanbuz.mq.Consumer"></bean>
</beans>
context.xml中:
<beans>
<rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="farahvhost" username="farah" password="farah" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="ub.queue" />
<rabbit:direct-exchange name="ub.exchange">
<rabbit:bindings>
<rabbit:binding queue="ub.queue"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="ub.exchange" queue="ub.queue" reply-timeout="15000" />
</beans>
文档不是很清楚。我尝试在生产者端使用convertSendAndReceive,在消费者端使用on消息处理程序但是没有用(代码如上所示注释掉),我收到此错误:未指定默认侦听器方法:指定非null值对于&#39; defaultListenerMethod&#39;属性或覆盖&#39; getListenerMethodName&#39;方法
答案 0 :(得分:0)
使用固定回复队列时,您必须在兔子模板上配置一个侦听器容器。
最简单的配置是只在模板中添加<rabbit:reply-listener />
子元素。
有关详细信息,请参阅the reference documentation。