Java RabbitMQ固定应答队列配置
配置rabbitMQ消息的固定回复队列时遇到问题 我有如下生产者和消费者:Java RabbitMQ固定应答队列配置,java,spring,rabbitmq,amqp,spring-amqp,Java,Spring,Rabbitmq,Amqp,Spring Amqp,配置rabbitMQ消息的固定回复队列时遇到问题 我有如下生产者和消费者: public class Producer { private static ApplicationContext context; private static RabbitTemplate template; public static void main(String[] args) { new ClassPathXmlApplicationContext("mq-pro
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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}**/
}
生产者xml配置:(在bean中…标记)
使用者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消息处理程序,但是没有成功(代码注释如上所示),我遇到以下错误:未指定默认侦听器方法:请为“defaultListenerMethod”属性指定非空值或重写“getListenerMethodName”方法。使用固定回复队列时,必须在rabbit模板上配置侦听器容器 最简单的配置是只向模板添加一个子元素
有关完整的详细信息,请参阅。谢谢您的回答,Gary!我已经检查并实现了一种不同的方法,我面临一些错误:-如果你可以在不同的线程上检查我的问题:我接受了这个答案,因为这是一种方法,但是我选择了另一种方法,使用文档中看起来更干净的示例。
<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>