Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Define MessageConverter for each Connection #8

Open
jpgu07 opened this issue Aug 29, 2019 · 2 comments
Open

Define MessageConverter for each Connection #8

jpgu07 opened this issue Aug 29, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jpgu07
Copy link

jpgu07 commented Aug 29, 2019

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.

@rwanderc
Copy link
Contributor

rwanderc commented Oct 23, 2019

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.

@rwanderc rwanderc added the question Further information is requested label Feb 25, 2020
@rwanderc rwanderc self-assigned this Feb 25, 2020
@rwanderc rwanderc added the enhancement New feature or request label Feb 25, 2020
@rwanderc
Copy link
Contributor

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?

@rwanderc rwanderc added help wanted Extra attention is needed and removed question Further information is requested labels Mar 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request help wanted Extra attention is needed
Development

No branches or pull requests

2 participants