java-Rabbitmq引发AmqpException:类[B没有找到方法

前端之家收集整理的这篇文章主要介绍了java-Rabbitmq引发AmqpException:类[B没有找到方法 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我向RabbitMQ发送消息时,它将为循环抛出AmqpException:

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

我试图添加类级别的@RabbitListener,但是没有用

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

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

生产者方面:

@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());
}

消费者方面:

@Component
@RabbitListener(queues = "test")
public class ReceiverTwo {
    @RabbitHandler
    public void process(String message) {
        System.out.println("Receiver: " + message);
    }
}
最佳答案
您发送字节数组并尝试接收字符串.

将process(字符串消息)更改为process(字节[]消息).

其他选项是设置

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

并传递给

channel.basicpublish("testExchange",messageProperties,message.getBytes());

请参阅SimpleMessageConverter.createMessage以供参考.

或者您可以使用RabbitTemplate来帮您.

原文链接:https://www.f2er.com/java/532832.html

猜你在找的Java相关文章