Dynamic configure ContainerFactory for dynamic JMS Queue with Spring

Viewed 877

I'm having to change from simply using @JmsListener to setting up my listeners dynamically in order to allow my user to configure the application choosing which queue to read to.

I have tried follow the Spring JMS Documentation for Programmatic Endpoint Registration but there is one aspect it does not cover: how to set the ListenerContainerFactory I want to use for my listener.

I have tried the following:

@Configuration
@EnableJms
public class JmsConfig implements JmsListenerConfigurer {

    @Autowired
    private JmsListenerEndpointRegistry registry;

    @Overide
    public void configureJmsListeners(JmsListenerEndpointRegistrar register) {
        SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
        endpoint.setId("myJmsEndpoint");
        endpoint.setDestination("TestQueue");
        endpoint.setupListenerContainer(registry.getListenerContainer("myContainerFactory"))
        endpoint.setMessageListener( message -> {
            // handle
        });
        register.registerEndpoint(endpoint)

    }

    @Bean
    public JmsListenerContainerFactory myContainerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

   // Other Connection and Container factories

}

but I'm receiving:

java.lang.IllegalArgumentException: Could not configure endpoint with the specified container 'null' Only JMS (org.springframework.jms.listener.AbstractMessageListenerContainer subclass) or JCA (org.springframework.jms.listener.endpoint.JmsMessageEndpointManager) are supported.
    at org.springframework.jms.config.AbstractJmsListenerEndpoint$JcaEndpointConfigurer.configureEndpoint(AbstractJmsListenerEndpoint.java:188)

I'm guessing the null is because, at this phase, Spring still doesn't have the beans created (right?). What is the right approach here to make this work?

Thanks!

0 Answers
Related