[英]RabbitMQ fixed reply queue configuration
I'm having some trouble configuring a fixed reply queue to my rabbitMQ message. 我在为RabbitMQ消息配置固定答复队列时遇到一些麻烦。
I have the producer and consumer as follows: 我的生产者和消费者如下:
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");
}
}
Consumer: 消费者:
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: (in beans... tag) 生产者xml配置:(以bean ...标记)
<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>
consumer xml config: 消费者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: 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>
Documentation was not really clear. 文档不是很清楚。 I tried using a convertSendAndReceive on the producer side and an on message handler on the consumer side however that did not work (code commented out as shown above), I got this error: No default listener method specified: Either specify a non-null value for the 'defaultListenerMethod' property or override the 'getListenerMethodName' method. 我尝试在生产者端使用convertSendAndReceive并在消费者端使用on消息处理程序,但是该方法不起作用(代码注释如上所示),我收到此错误:未指定默认侦听器方法:要么指定一个非null值设置为'defaultListenerMethod'属性,或覆盖'getListenerMethodName'方法。
When using a fixed reply queue, you have to configure a listener container on the rabbit template. 使用固定答复队列时,必须在Rabbit模板上配置侦听器容器。
The simplest configuration is to just add a <rabbit:reply-listener />
child element to the template. 最简单的配置是将<rabbit:reply-listener />
子元素添加到模板中。
See the reference documentation for complete details. 有关完整的详细信息,请参见参考文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.