SpringBoot整合RocketMQ发送并接收消费者返回一个String报错:CODE: 10006 DESC: send request message to <TestTopic> OK,

运行时报错展示

Received Message : 同步消息,返回string
2021-03-10 10:31:29.959 ERROR 10312 --- [nio-8080-exec-1] o.a.r.spring.core.RocketMQTemplate       : send request message failed. destination:TestTopic, message:GenericMessage [payload=同步消息,返回string, headers={id=3776255d-2730-5de2-b818-e2d3e128e6d6, timestamp=1615343486718}] 
2021-03-10 10:31:29.968 ERROR 10312 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.messaging.MessagingException: CODE: 10006  DESC: send request message to  OK, but wait reply message timeout, 3000 ms.; nested exception is org.apache.rocketmq.client.exception.RequestTimeoutException: CODE: 10006  DESC: send request message to  OK, but wait reply message timeout, 3000 ms.] with root cause

org.apache.rocketmq.client.exception.RequestTimeoutException: CODE: 10006  DESC: send request message to  OK, but wait reply message timeout, 3000 ms.
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.waitResponse(DefaultMQProducerImpl.java:1495) ~[rocketmq-client-4.7.1.jar:4.7.1]
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.request(DefaultMQProducerImpl.java:1371) ~[rocketmq-client-4.7.1.jar:4.7.1]
	at org.apache.rocketmq.client.producer.DefaultMQProducer.request(DefaultMQProducer.java:589) ~[rocketmq-client-4.7.1.jar:4.7.1]
	at org.apache.rocketmq.spring.core.RocketMQTemplate.sendAndReceive(RocketMQTemplate.java:230) ~[rocketmq-spring-boot-2.1.1.jar:2.1.1]
	at 

问题代码
1.生产者

@RequestMapping("/testReceiveStr")
public String testReceiveStr(){
	String receive = rocketMQTemplate.sendAndReceive(topic, "同步消息,返回string",
                                                        String.class);
    System.out.printf("同步消息-返回一个String类型的结果:%s%n",receive);
    return "success";
}

2.消费者

@Component
@RocketMQMessageListener(consumerGroup = "MyConsumerGroup", topic = "TestTopic",
        consumeMode = ConsumeMode.CONCURRENTLY)
public class Consumer implements RocketMQListener {
    @Override
    public void onMessage(String message) {
        System.out.println("Received Message : "+message);
    }
}

问题分析
消费者那边收到了消息,报这个错是因为消费者没有进行回执,导致生产者那边等待消息超时报错。所以在要求消费者有回执消息的场景下,消费者一定要选择正确。
消费者继承的接口中选择RocketMQReplyListener,进行实现。

问题处理

@Component
@RocketMQMessageListener(consumerGroup = "MyConsumerGroup", topic = "TestTopic",
        consumeMode = ConsumeMode.CONCURRENTLY)
public class Consumer implements RocketMQReplyListener {

    @Override
    public Object onMessage(Object message) {
        System.out.println("Received Message : "+message);
        return message;
    }
}

运行测试
在这里插入图片描述

你可能感兴趣的:(bug处理,java,spring,boot)