Johan
Johan

Reputation: 40530

How to use a custom org.springframework.amqp.support.converter.MessageConverter with RabbitListener annotation?

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?

Upvotes: 0

Views: 6057

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

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.

Upvotes: 2

Related Questions