Monday 20 January 2014

Dynamic Spring Message Driven Beans

Creating MDBs from spring is really easy particularly if all the information is known up front.  As usual it is just some configuration in the spring context and is shown here.

For dynamic beans things can still be put into the spring configuration but need the destination setting at runtime.  This can be done using some spring context as prototype beans so that a new instance of them is created each time and a small bit of wiring in the code.

Spring Context

<!-- MQ / JMS Connection factory -->
<bean id="jmsConnectionFactory" class="">
<property name="hostName" value="${}" />
<property name="port" value="${}" />
<property name="queueManager" value="${}" />
<property name="transportType" value="1" />

<!-- Spring Connection Factory -->
<bean id="springConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="jmsConnectionFactory" />
<property name="username" value="${}" />
<property name="password" value="${}" />

<!-- This bean is a prototype bean which is loaded dynamically-->
<!-- it is loaded using a getBean("jmsDestination", requiredTopic") and as such the HOLDING_VALUE is -->
<!-- replaced with whatever is required by the class in question                                    -->
<bean id="jmsDestination" class="" scope="prototype">
<constructor-arg value="HOLDING_VALUE" />

<!-- Generic MDB Container -->
<bean id="springMdbContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" scope="prototype">
<property name="connectionFactory" ref="springConnectionFactory" />
<!-- The destination is dynamically set in the factory method but a default is provided here-->
<property name="destination" ref="jmsDestination" />
<!-- The normal MessageListener which is put here is added dynamically in the factory -->


In the java code a new destination can be obtained using the bean above and the code,

     * Create a Destination object.
     * @param topic The topic to construct the destination with
     * @return the Destination created.
    public Destination createDestination(final String topic)
        // Create the destination with the topic string.
        return (Destination) applicationContext.getBean("jmsDestination", topic);

This creates a new Destination object based on the spring bean.  This allows the MQ provided to be changed in the spring configuration without having to change code.

To create a new MDB create a destination as above and then use the code,
     * Create a MDB wired by spring.
     * @param destination The destination topic to listen to.
     * @param messageListener The message listener (MDB) to wire
    private void wireMdb(final Destination destination, final MessageListener messageListener)
        // Use spring to wire up the MDB with this destination and a ConnectionFactory 
        // which is already provided in the spring context. 
        // The MDB and Destination are set here and then the container is manually started.
        final DefaultMessageListenerContainer container = 
                (DefaultMessageListenerContainer) applicationContext.getBean("springMdbContainer");

It is also possible to change the destination that a container is listening to once it has started.  Using the setDestination(...) method does this according to the spring documentation.

No comments:

Post a Comment