Caused by: org.springframework.amqp.AmqpException: No method found for class [B

created at 12-16-2021 views: 59

Problem Description

Write RabbitMQ message

data

Receive RabbitMQ message type String

@RabbitHandler
public void receive(String message) {
    if (StringUtils.isEmpty(message)) {
        return;
    }
    // code
}

The above configuration causes an infinite loop to throw an exception as follows

[08:57:05.502][DEBUG][o.s.a.r.listener.BlockingQueueConsumer][org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] Received message: (Body:'[B@d5b8d16(byte[223])' MessageProperties [headers={}, contentLength=0, redelivered=true, receivedExchange=exchange.canal, receivedRoutingKey=canal-routing-key, deliveryTag=450, consumerTag=amq.ctag-x079bMTfxizFn_WM75rJyw, consumerQueue=mes_warning_message])
[08:57:05.502][DEBUG][o.s.a.r.l.a.MessagingMessageListenerAdapter][org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] Processing [GenericMessage [payload=byte[223], headers={amqp_receivedRoutingKey=canal-routing-key, amqp_receivedExchange=exchange.canal, amqp_deliveryTag=450, amqp_consumerQueue=mes_warning_message, amqp_redelivered=true, id=e14a2efd-12d0-1c7d-2c1b-43b9f6432c28, amqp_consumerTag=amq.ctag-x079bMTfxizFn_WM75rJyw, amqp_lastInBatch=false, timestamp=1639011425502}]]
[08:57:05.502][DEBUG][o.s.a.r.listener.BlockingQueueConsumer][pool-2-thread-6] Received cancelOk for tag amq.ctag-x079bMTfxizFn_WM75rJyw (mes_warning_message); Consumer@34114276: tags=[[amq.ctag-x079bMTfxizFn_WM75rJyw]], channel=Cached Rabbit Channel: AMQChannel(amqp://test@127.0.0.1:5672/,1), conn: Proxy@42db3ff Shared Rabbit Connection: SimpleConnection@23dda7a3 [delegate=amqp://test@127.0.0.1:5672/, localPort= 50086], acknowledgeMode=AUTO local queue size=0
[08:57:05.502][DEBUG][o.s.a.r.l.SimpleMessageListenerContainer][org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] Listener exception after container shutdown
org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Listener threw exception
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:1699)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1589)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1504)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1492)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1483)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1427)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:970)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:916)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:83)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1291)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1197)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.amqp.AmqpException: No method found for class [B
    at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:151)
    at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getMethodFor(DelegatingInvocableHandler.java:270)
    at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.getMethodFor(HandlerAdapter.java:84)
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandlerAndProcessResult(MessagingMessageListenerAdapter.java:146)
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:133)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1585)
    ... 10 common frames omitted

reason

The type of the received message is wrong, change the String type to byte[]

Solution

Added RabbItConfig configuration file to complete the conversion of String in JSON format into bytecode

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }
}

Receive with bytecode array and convert to String

    @RabbitHandler
    public void receive(byte[] result) {

        String message = null;
        try {
            message = new String(result, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (StringUtils.isEmpty(message)) {
            return;
        }
   }
created at:12-16-2021
edited at: 12-16-2021: