Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rabbitmq throws the AmqpException: No method found for class [B

When I send a message to the RabbitMQ, then it throws a AmqpException for loop:

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException

    : Listener method 'no match' threw exception
        at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:198) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:127) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1521) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1444) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1431) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1410) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:848) [spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:832) [spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:78) [spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1073) [spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]
    Caused by: org.springframework.amqp.AmqpException: No method found for class [B
        at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:149) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.invoke(DelegatingInvocableHandler.java:129) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:60) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:190) ~[spring-rabbit-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        ... 10 common frames omitted

I tried to add class-level @RabbitListener but it didn't work

@Component
@RabbitListener(queues = "test", containerFactory="rabbitListenerContainerFactory")
public class ReceiverTwo {

    @RabbitHandler
    public void process(String message) {
        System.out.println("Receiver: " + message);
    }
}

The PRODUCER side :

@Test
public void publishTest() throws IOException {
    channel.exchangeDeclare("testExchange", "direct", true);
    channel.queueBind("many", "testExchange", "many");
    String message = "The test message";
    channel.basicPublish("testExchange", "test",null, message.getBytes());
}

The CONSUMER side :

@Component
@RabbitListener(queues = "test")
public class ReceiverTwo {
    @RabbitHandler
    public void process(String message) {
        System.out.println("Receiver: " + message);
    }
}
like image 502
Kenho Lyu Avatar asked Jan 31 '19 09:01

Kenho Lyu


1 Answers

You send array of bytes and try to receive string.

Change process(String message) to process(byte[] message).

Other option is to set

MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setContentEncoding(this.defaultCharset);

and pass it to

channel.basicPublish("testExchange", "test", messageProperties, message.getBytes());

See SimpleMessageConverter.createMessage for reference.

Or you can use RabbitTemplate it will do it for you.

like image 80
talex Avatar answered Nov 14 '22 21:11

talex