gpt4 book ai didi

java - Spring AMQP : Send reply from POJO listener failure because of missing reply-to property

转载 作者:行者123 更新时间:2023-11-30 03:00:42 26 4
gpt4 key购买 nike

我想编写一个简单的代码示例,它将消息发送到交换器,使用监听器接收消息并发回消息。

这就是我发送消息的方式:

private static void pingpong(Object messageListener) throws Exception {
ConnectionFactory cf = new CachingConnectionFactory("localhost");

// set up the queue, exchange, binding on the broker
RabbitAdmin admin = new RabbitAdmin(cf);
Queue queue = new Queue(QUEUE);
admin.declareQueue(queue);
TopicExchange exchange = new TopicExchange(EXCHANGE);
admin.declareExchange(exchange);
admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo.*"));

// set up the listener and container
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
container.setMessageListener(messageListener);
container.setQueueNames(QUEUE);
container.start();

// send something and receive result.
RabbitTemplate template = new RabbitTemplate(cf);
Object result = template.convertSendAndReceive(EXCHANGE, "foo.bar", "ping");
System.out.println("Received: " + result);
}

监听器的代码:

public static class PingPongReceiver {
public String handleMessage(String in) {
System.out.println("Received: " + in);
return "pong";
}
}

我是这样调用它的:

pingpong(new MessageListenerAdapter(new PingPongReceiver()));

但是发送回复失败,但有以下异常(exception)情况:

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:865)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:760)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:680)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:93)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:183)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1345)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:661)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1096)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1080)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$800(SimpleMessageListenerContainer.java:93)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1190)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.amqp.rabbit.listener.adapter.ReplyFailureException: Failed to send reply with payload 'pong'
at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.handleResult(AbstractAdaptableMessageListener.java:213)
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:296)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:757)
... 10 more
Caused by: org.springframework.amqp.AmqpException: Cannot determine ReplyTo message property value: Request message does not contain reply-to property, and no default response Exchange was set.
at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.getReplyToAddress(AbstractAdaptableMessageListener.java:289)
at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.handleResult(AbstractAdaptableMessageListener.java:209)
... 12 more

我在弄清楚如何使用匿名、非持久队列发送回复时遇到问题。我错过了什么?

最佳答案

你所描述的毫无意义; RabbitTemplate.convertAndReceive() 设置replyTo(Direct reply-to,如果代理支持,否则是临时队列)。

我刚刚复制并粘贴了您的代码,它对我来说效果很好......

Received: ping
Received: pong

也许队列中有一条旧的、过时的消息,没有回复?

您可以使用管理 UI 清除队列。

我还用

测试了你的代码
Queue queue = new AnonymousQueue();
...
container.setQueueNames(queue.getName());

而且效果也很好。

顺便说一句,当手动创建Spring Bean时,建议您调用afterPropertiesSet(如果该bean是初始化bean)。

container.afterPropertiesSet();

最后,您应该停止容器并销毁连接:

container.stop();
cf.destroy();

关于java - Spring AMQP : Send reply from POJO listener failure because of missing reply-to property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com