使用 rabbitmq 消息时获取方法未找到异常

getting method not found exception while consuming rabbitmq message

已在消费者 class 上创建以使用来自 rabbitMQ GUI 的消息。

但是当我从 rabbitMQ GUI 发布任何消息时,它给出错误 class 未找到。控制台上不断打印错误。

可能问题是 rabbitGUI 从字节发送消息,我正在尝试通过字符串接收它。

我想调用基于消息正文的特定方法。

@Component
@RabbitListener(queues =  {"#{addNeRangeQueue.name}"})
public class consumerController {

    @RabbitHandler
    public void receive(String message) {
        System.out.println("rabbit mq listener receive : "+message);
    }
}
   
@Configuration
public class RabbitMQConsumerConfig {
 
    //properties value configuration i have removed here.
    @Bean
    public Queue addNeRangeQueue() {
        return new Queue(queueName,false);
    }
    
    @Bean
    public TopicExchange getTopicEchangeName() {
        return new TopicExchange(exchange);
    }

    @Bean
    public Binding bindaddNeRange(TopicExchange getTopicEchangeName, Queue addNeRangeQueue) {
        return BindingBuilder.bind(addNeRangeQueue).to(getTopicEchangeName).with(routingkey);
    }
}

异常:

2020-09-08 00:19:10.378  WARN 9456 --- [ntContainer#0-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.
    
    org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Listener threw exception
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:1705) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1595) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1510) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1498) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1489) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1433) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:970) [spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:916) [spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access00(SimpleMessageListenerContainer.java:83) [spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1291) [spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1197) [spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at java.lang.Thread.run(Unknown Source) [na:1.8.0_211]
    Caused by: org.springframework.amqp.AmqpException: No method found for class [B
        at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:151) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getMethodFor(DelegatingInvocableHandler.java:270) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.getMethodFor(HandlerAdapter.java:84) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandlerAndProcessResult(MessagingMessageListenerAdapter.java:146) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:133) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1591) ~[spring-rabbit-2.2.10.RELEASE.jar:2.2.10.RELEASE]
        ... 10 common frames omitted
   

    

从 UI 发布时,您必须添加 content_type 属性 以便消息转换器知道将 byte[] 消息正文转换成什么。

使用 method-level @RabbitListener,转换器可以从方法参数中推断类型 - 这对于 class 级别注释是不可能的,因为我们需要先进行转换确定要调用的方法。

对于具有默认值 SimpleMessageConverter 的简单字符串:

当使用 JSON(和 Jackson2JsonMessageConverter)时: