user3534483
user3534483

Reputation: 2363

Configuring multiple DefaultJmslistenercontainerfactory

In my app, I have 2 diff mq conn factory beans. For this I have 2 diff DefaultJmslistenercontainerfactory beans ex cf1 n cf2. Each of DefaultJmslistenercontainerfactory bean is being referred in seperate @JmsListener. ..Now i want to start stop each listrner programatically , for that I am overriding configureMessageListeners(JmsListenerRegistrar) method where I can set the DefaultJmslistenercontainerfactory instance. Note I only one instance can be set.. then in my code I get spring instance of JmsListenerRegistry from which I can get list dmlc..which I can start n stop However. .since I have set only one DefaultJmslistenercontainerfactory instance, my code returns only one dmlc.. Question here is how can I pass multiple DefaultJmslistenercontainerfactory instances in configureJmsListener() method?? Note- I do not create dmlc manually..I just configure factory..

Upvotes: 4

Views: 7182

Answers (1)

Gary Russell
Gary Russell

Reputation: 174729

Why are you using configureMessageListeners() ? That is for programmatic endpoint registration, not influencing the configuration of @JmsListener.

Show your configuration (edit the question, don't try to post code/config in comments).

This works fine for me...

@Bean
public JmsListenerContainerFactory<DefaultMessageListenerContainer> one(
        @Qualifier("jmsConnectionFactory1") ConnectionFactory cf) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(cf);
    return factory;
}

@Bean
public JmsListenerContainerFactory<DefaultMessageListenerContainer> two(
        @Qualifier("jmsConnectionFactory2") ConnectionFactory cf) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(cf);
    return factory;
}

@JmsListener(id="fooListener", destination="foo", containerFactory="one")
public void listen1(String payload) {
    System.out.println(payload + "foo");
}

@JmsListener(id="barListener", destination="bar", containerFactory="two")
public void listen2(String payload) {
    System.out.println(payload + "bar");
}

...

@Autowired
JmsListenerEndpointRegistry registry;

...

MessageListenerContainer fooContainer = registry.getListenerContainer("fooListener");
MessageListenerContainer barContainer = registry.getListenerContainer("barListener");

You can also use registry.getListenerContainers() to get a collection.

I thought I explained all this in my answer to your other question.

Upvotes: 8

Related Questions