0

I'm using Spring 4.2 and spring-amqp 1.5.5 and I've implemented my own org.springframework.amqp.support.converter.MessageConverter. This message converter is supposed to map between a org.springframework.amqp.core.Message and a Java object by looking at a property contained in the message body. But I cannot find a way to apply this message converter to be used in combination with the @RabbitListener annotation. What I want to do is something like this:

@RabbitListener
public void process(MyEvent myEvent) {
..
}

But Spring seems to be using a DefaultMessageHandlerMethodFactory to which you can only assign instances of org.springframework.messaging.converter.MessageConverter which is not the same type of MessageConverter that I'm using.

The reason for implementing my own org.springframework.amqp.support.converter.MessageConverter is because we generate messages from non Java applications which doesn't add the magic __TypeId__ header used by Spring to determine the class to deserialize the body into.

I have tried to define a SimpleRabbitListenerContainerFactory:

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(rabbitConnectionFactory());
    factory.setMessageConverter(messageConverter());
    return factory; 
}

but it doesn't seem to be used (or at least it doesn't make any difference).

So my question is, how do I apply my org.springframework.amqp.support.converter.MessageConverter?

Johan
  • 37,479
  • 32
  • 149
  • 237

1 Answers1

2

You are doing it the correct way, so something else must be happening, perhaps the bean is being overridden by another definition? You could try giving the bean a different name and explicitly reference it in the @RabbitListener.

We have lots of test cases that test this feature this one for example and its use here.

By the way, in the upcoming 1.6 release the type from the listener method is provided to the converter which helps with missing __TypeId__ headers.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • You're right, there was another bean definition overridding it (that had the same name). As a side note I wonder why I spring didn't complain about duplicated bean definitions in my case. Thanks a lot for the hint! – Johan Apr 05 '16 at 13:06
  • 1
    That is normal Spring Framework behavior - it's intended to work that way to allow, say, an application to override a bean definition that's in a jar. – Gary Russell Apr 05 '16 at 13:51