Define MessageConverter for each Connection
Hi, hope you're doing great.
Is there any way to define a MessageConvert for each Connection?
Right now we have two Connections and we need to parse their messages using different MessageConverter:
//Connection A
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
//Connection B
@Bean
public MessageConverter simpleMessageConverter() {
return new SimpleMessageConverter();
}
As fas a I know I can define an AmqpTemplate and set the MessageConverter, but I don't know how to set the Template to each connection.
We are using Spring Boot 2.1.x and multirabbit 2.1.x. Thanks!
Any help would be great, thanks so much for your work and your help.
Hi. Sorry for the delay in answering the question.
AFAIK, Spring seems not to offer a way to configure MessageConverter via properties. Therefore, SpringMultiRabbit will also not offer this functionality via properties, since it extends Spring functionality.
What you can do is to provide a bean of MultiRabbitConnectionFactoryWrapper from SpringMultiRabbit providing all connection factories you need (here you create the ConnectionFactorys with the special MessageConverters). There's not much documentation on it, but it's pretty straightforward. You provide a bean annotated with @Primary and that will be taken by SpringMultiRabbit.
I didn't test quite well, but you can also do some hacky solution (which I don't recommend since it's less maintainable). That would be to intercept the bean and add the message converter to it.
@Component
class ContainerFactoryMessageConverterHackyEnhancer {
private BeanFactory beanFactory;
ContainerFactoryMessageConverterHackyEnhancer() {
this.beanFactory = beanFactory;
}
@PostConstruct
public void addMessageConverter() {
beanFactory.getBean("connectionNameA", SimpleRabbitListenerContainerFactory.class).setMessageConverter(new SimpleMessageConverter());
beanFactory.getBean("connectionNameB", SimpleRabbitListenerContainerFactory.class).setMessageConverter(new SimpleMessageConverter());
}
}
If you have any suggestions, let me know.
Hi @jpgu07, does the suggestion work for you?
I'm trying to think of a non-intrusive way of providing multiple MessageConverters but all possibilities I see seem like hacky solutions. Do you have a suggestion or want to send a pull request?