Is it possible to have an async message receiver in Spring AMQP that handles multiple content types?

By : E-Riz
Source: Stackoverflow.com
Question!

I'm using an asyncronous message receiver in Spring-AMQP to receive messages. Currently only messages with JSON content are handled, but I have a requirement to also handle messages with XML content. My current implementation of MessageListener has a MessageConverter injected and uses it in onMessage(Message), like this:

MyMessage myMessage = (MyMessage) jsonConverter.fromMessage(message);

In order to support different content types I could obviously use the MessageProperties to interrogate the content-type header and manually select a converter to use. But that seems like a lot of work, like Spring should provide some better support for this scenario. I was hoping to find a generic MessageConverter implementation that would map from content-types to specific converters, but there doesn't seem to be such a thing.

Is my best option to write a delegating converter like that? Or is there a way to configure the ListenerContainer to support both async receiving and multiple converters that are automatically used as needed?

By : E-Riz


Answers

We have an open JIRA issue requesting support for a CompositeMessageConverter.

The listener container doesn't support conversion but we do have the MessageListenerAdapter which does support them (but just one, and has other stuff like handling replies).

Using the adapter means you can use a POJO method on your listener...

public void handleMessage(MyObject foo) {...}

If you put a delegating converter (one that delegates to either the json or marshalling converter) into the MLA, and both converters create the same object type, this will work fine. Otherwise the signature would have to take an Object and you'd have to do instanceof tests.

At some point, I'd like to make the adapter a bit smarter so it can choose the method based on the object type created by the converter...

public void handleMessage(Foo foo) {...}

public void handleMessage(Bar bar) {...}

... but that's really a different issue.

If you come up with a useful converter that you would like to contribute to the framework, the guidelines are on the project wiki.



This video can help you solving your question :)
By: admin