Issue
My Spring AMQP application has been logging the following exception on initiation:
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@660cff44}]
From my searching I understand that this is because there is a class incompatibility with the message type? However, I am not able to see where this is.
The following are the relevant code segments:
@Bean
public MessageConverter jsonMessageConverter(){
return new Jackson2JsonMessageConverter();
}
@Bean
Queue queue() {
return new Queue(config.getAMQPResultsQueue(), false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(config.getAMQPResultsExchange());
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("#");
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(config.getAMQPResultsQueue());
container.setMessageListener(listenerAdapter);
container.setMessageConverter(jsonMessageConverter());
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
and
@Component
public class Receiver {
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
}
}
I have tried setting the class of message to Byte[] but the result is the same. I am sure I am missing something simple - just not sure what it is!
Solution
The Jackson2JsonMessageConverter
will only perform conversion if the message has a content_type
header that contains json
.
Otherwise, it will return byte[]
.
byte[]
will also not be converted to Byte[]
. Set the header or use byte[]
.
Answered By - Gary Russell
Answer Checked By - Gilberto Lyons (JavaFixing Admin)