1. 程式人生 > >spring boot整合rabbitmq踩坑

spring boot整合rabbitmq踩坑

按照官方的例子程式碼如下:

@Component
@RabbitListener(queues="test_mq_testQueue")
public class Receiver {


    @RabbitHandler 
    public void process(String msg){
        System.out.println(msg);
    }
}

但是執行接收訊息時,後臺一直迴圈報錯

Caused by: org.springframework.amqp.AmqpException: No method found for class [B
    at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:127
) at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.getMethodNameFor(DelegatingInvocableHandler.java:224) at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.getMethodAsString(HandlerAdapter.java:61) at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:140
) at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:106) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:822) ... 10 common frames omitted

後來查資料發現,@RabbitListener(queues=”test_mq_testQueue”)不需要放在類上。直接註釋在方法上就好

@Component
public class Receiver {
@RabbitListener(queues="test_mq_testQueue")
    public void process(Message message){
        System.out.println(new String(message.getBody()));
    }
}

測試,在控制檯傳送訊息
這裡寫圖片描述

後臺接收訊息
這裡寫圖片描述