rabbitmq抛出AmqpException:未找到类[B]的方法

hmmo2u0o  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(3)|浏览(139)

当我向RabbitMQ发送一条消息时,它会抛出一个AmqpException for循环:

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);
    }
}
yeotifhr

yeotifhr1#

发送字节数组并尝试接收字符串。
process(String message)变更为process(byte[] message)
其他选项是设置

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

并传给了

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

请参阅SimpleMessageConverter.createMessage以取得指涉。
或者您可以使用RabbitTemplate,它将为您完成此操作。

vfh0ocws

vfh0ocws2#

在我的例子中,我已经将一个不同的类传递给了发布者,并期望将一个不同的类作为侦听器。

camsedfj

camsedfj3#

奇怪的是,代码工作了几天,这个问题突然出现了,我不知道是什么问题,只是把@RabbitListener从类移到方法,连同@RabbitHandler一起,就解决了。

相关问题