使用RabbitMQ接收消息报错问题处理

使用RabbitMQ接收消息报错问题处理,第1张

在处理统一身份认证平台推送的RabbitMQ消息时,监听器代码如下:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "xxxx.xxxx.xxxx")
public class ParkSubConsumerTest {

    @RabbitHandler
    public void showMsg(String message) { //此处换成用 JSONObject 或者 对方实际传递的类型 接收都会报错
        System.out.println("xxxx.xxxx.xxxx收到的消息内容为:" + message);
    }

}

发现监听消息代码报错如下:

2022-05-09 13:53:50.966  WARN 20228 --- [ntContainer#0-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.

org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:146) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1656) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1575) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1563) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1554) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1498) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:968) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:914) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access00(SimpleMessageListenerContainer.java:83) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1289) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1195) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: org.springframework.amqp.AmqpException: No method found for class [B
	at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:185) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getMethodFor(DelegatingInvocableHandler.java:317) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.getMethodFor(HandlerAdapter.java:110) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandlerAndProcessResult(MessagingMessageListenerAdapter.java:192) ~[spring-rabbit-2.3.10.jar:2.3.10]
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:137) ~[spring-rabbit-2.3.10.jar:2.3.10]
	... 11 common frames omitted

代码一直报错 org.springframework.amqp.AmqpException: No method found for class [B

查阅相关资料,发现问题为:生产者发送消息的类型与消费者接收消息的类型不一致。生产者发送的为 "contentType": "application/json" ,而消费者接受的类型为 "contentType": "text/plain"

有多种解决方法,此处列举三种。

1. 生产者发送String类型

生产者发送String类型("contentType": "text/plain")的消息:

	rabbitTemplate.convertAndSend(RabbitConfig.MIS_EXCHANGE, RabbitConfig.MEMBER_REWARD_ROUTING, jsonObject.toString());

消费者接受String类型的消息:

@Component
@RabbitListener(queues = { RabbitConfig.MEMBER_REWARD_QUEUE })
public class Receiver {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("Receiver : " + msg);
        JSONObject jsonObject = JSON.parseObject(msg);
        ...
    }

}
2. 生产者发送JSONObject类型

生产者发送JSONObject类型("contentType": "application/x-java-serialized-object")的消息:

	rabbitTemplate.convertAndSend(RabbitConfig.MIS_EXCHANGE, RabbitConfig.MEMBER_REWARD_ROUTING, jsonObject);

消费者接受jsonObject类型的消息:

@Component
@RabbitListener(queues = { RabbitConfig.MEMBER_REWARD_QUEUE })
public class Receiver {

    @RabbitHandler
	public void process(JSONObject jsonObject) {
		System.out.println("Receiver : " + jsonObject.toJSONString());
		...
    }

}
3. 消费者设置默认处理方法

不管生产者怎么生产消息。通过 rabbitTemplate.convertAndSendamqp-clientrabbitmq 管理后台 等发送均可。

消费者设置默认处理:

@Component
@RabbitListener(queues = { RabbitConfig.MEMBER_REWARD_QUEUE })
public class Receiver {

    @RabbitHandler(isDefault = true)
	public void process(Message message) {
        // message.getBody() 为消息体
        // 如果客户端发送的是String, new String(message.getBody()) 得到客户端发送的String
        // 如果客户端发送的是Object, 此时接收到的是序列化后的Object,在这里需要反序列化得到Object
        ...
    }

}

此方法是我实际采用的方法,我的代码如下:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "xxxx.xxxx.xxxx")
public class ParkSubConsumerTest {

    @RabbitHandler(isDefault = true)
    public void showMsg(Message message) {
        System.out.println("xxxx.xxxx.xxxx收到的消息内容为:\n" + JSON.toJSONString(message));
        Object parse = JSONObject.parse(message.getBody());
        System.out.println("消息内容: " + JSON.toJSONString(parse));
        MyMessage myMessage = JSONObject.parseObject(message.getBody(), MyMessage.class);
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905497.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存