英文:
SimpleMessageContainer can't listen to Message object - Spring RabbitMQ
问题
我正在使用动态队列和容器监听器,我的问题是我必须监听特定队列的方法不会接收到Message对象。我收到了一个ListenerExecutionError异常,其中包含以下信息:
"Failed to invoke target method 'dataHandler' with argument type = [class [B], value = [{[B@65f26bf7}]"
我的代码以前在我使用RabbitListener注解时有效,它完美地接受了Message对象,但当我使用容器时出现了这个问题。我将处理程序参数类型从Message更改为byte[],并且方法被按需调用。我猜测RabbitListener注解可能进行了一些我不知道的转换操作。
请有人帮帮我。
这是我的容器创建代码:
SimpleMessageListenerContainer listen = new SimpleMessageListenerContainer();
listen.setConnectionFactory(rabbitTemplate.getConnectionFactory());
listen.setQueueNames(testQueue);
MessageListenerAdapter adapt = new MessageListenerAdapter();
adapt.setDefaultListenerMethod(randomMethod);
adapt.setDelegate(this);
listen.setMessageListener(adapt);
listen.start();
这是我的监听方法签名:
public void randomMethod(Message msg)
发送消息到这个队列的代码行是:
Message message = new Message(data, props)
rabbitTemplate.convertAndSend(testQueue, message);
Data是一个字节数组,props是一个MessageProperties对象。
英文:
I am using Dynamic queues and container listeners and my problem is that the method I have to listen to a specific queue won't receive Message objects. I get a ListenerExecutionError exception saying :
"Failed to invoke target method 'dataHandler' with argument type = [class [B], value = [{[B@65f26bf7}]"
My code used to work when I used the RabbitListener annotation and it accepted Message objects perfectly but when using containers I get this problem. I switched the handler argument type from Message to byte[] and the method was called as required. I am guessing there is some converting that the RabbitListener annotation did that I am unaware of doing.
Someone please help me.
This is my Container creation code :
SimpleMessageListenerContainer listen = new SimpleMessageListenerContainer();
listen.setConnectionFactory(rabbitTemplate.getConnectionFactory());
listen.setQueueNames(testQueue);
MessageListenerAdapter adapt = new MessageListenerAdapter();
adapt.setDefaultListenerMethod(randomMethod);
adapt.setDelegate(this);
listen.setMessageListener(adapt);
listen.start();
This is my listener method signature.
public void randomMethod(Message msg)
The line that sends code to this queue is :
Message message = new Message(data, props)
rabbitTemplate.convertAndSend(testQueue, message);
Data is a byte array and props is a MessageProperties object.
答案1
得分: 0
适配器不仅仅是传递消息,而是应该使用 MessageListener
。
listen.setMessageListener(msg -> randomMethod(msg));
或者...
listen.setMessageListener(this::randomMethod);
英文:
The adapter is not intended for simply passing the message like that, just use a MessageListener
instead.
listen.setMessageListener(msg -> randomMethod(msg);
or...
listen.setMessageListener(this::randomMethod);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论