Search code examples
javaspringspring-bootqueuejms

How to move XML bean definition to @Configuration annotated class


I am new to Spring, I am working in moving some bean definitions from XML to @Configuration Class.

Here is one of the beans I am struggling with:

<bean id="jmsProducerTemplate" class="org.springframework.jms.core.JmsTemplate"
      p:connectionFactory-ref="connectionFactory"/>

<jms:listener-container container-type="default" 
                        connection-factory="connectionFactory"
                        acknowledge="auto">

<jms:listener destination="YOURQUEUENAME" ref="theListenerClassYouAreUsing" />

</jms:listener-container>

How would that look in a @Configuration class, thanks.

So far I have this

@Bean("myContainerFactory")
public JmsListenerContainerFactory myContainerFactory() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

    ConnectionFactory connectionFactory = pooledConnectionFactory();

   
    factory.setConnectionFactory(connectionFactory);
    factory.setPubSubDomain(false);

    return factory;
}

I am missing the destination set, no idea how to do it.


Solution

  • You have to add a listener as well, see for example Annotation-driven Listener Endpoints:

    @Component
    public class MyService {
    
        @JmsListener(destination = "YOURQUEUENAME", containerFactory = "myContainerFactory")
        public void myListener(String data) { ... }
    }