Full jms: switch listener to JavaConfig

As the title says.

I read this valuable How to add multiple JMS MessageListners to one MessageListenerContainer for a Spring Java Config link

The author of this post is working through

messageListenerContainer.setMessageListener(new TaskFinished());

      

By the way: I am using

@Autowired
private ConsumerListener consumerListener;

defaultMessageListenerContainer.setMessageListener(consumerListener);

      

I am not using the new operator .

OK, limitation of the setMessageListener method : the class must implement the MessageListener interface , I tested and it works

My problem as per 23.6 JMS namespace support

How to present the following:

<jms:listener destination="queue.orders" ref="orderService" method="placeOrder"/>
<jms:listener destination="queue.confirmations" ref="confirmationLogger" method="log"/>

      

via JavaConfig?

They are simple pojo (see attributes ref

and method

)

I want to use as option a simple pojo (@Component or @Service) instead of a MessageListener object

There is no API in the DefaultMessageListenerContainer to work around this requirement or situation.

Thanks in advance.

+3


source to share


1 answer


<jms:listener destination="queue.orders" ref="orderService" method="placeOrder"/>

      

This xml uses MessageListenerAdapter

which you can pass to the delegate ( ref

and the method to execute (the default is' handleMessage`).



@Configuration
public MyJmsConfiguration {

    @Bean
    public DefaultMessageListenerContainer consumerJmsListenerContainer() {

        DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
        ...
        MessageListenerAdapter listener = new MessageListenerAdapter();
        listener.setDelegate(orderService());
        listener.setDefaultListenerMethod("placeOrder");
        dmlc.setMessageListener(listener);
        return dmlc;
}

      

To use it from Java config, use something like the snippet above.

+6


source







All Articles