ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Asynchronous Messaging Made Easy With Spring JMS
Pages: 1, 2, 3, 4, 5

These queue names and other JMS and JNDI parameters used in the sample application are shown in Table 4.



Table 4. Spring JMS Configuration Parameters

Parameter Name Parameter Value
Initial Context Factory org.jnp.interfaces.NamingContextFactory
Provider URL localhost:8080
Initial Context Factory URL Packages org.jnp.interfaces:org.jboss.naming
Queue Connection Factory UIL2ConnectionFactory
Queue Name queue/CreditRequestSendQueue, queue/CreditRequestReceiveQueue

Spring Configuration

Now that we have the JMS destinations required to run the sample application, it's time to get into the details of wiring the JMS components using an XML Spring configuration file (called spring-jms.xml). The components are wired with JMS object instances using the setter injection principle in the Inversion of Controller (IOC) design pattern. Let's look at the components in detail, showing a XML configuration snippet for each JMS component.

JNDI context is the starting point in getting the JMS resources, so first we configure a JNDI template. Listing 2 shows a Spring bean named jndiTemplate with the usual parameters required to get the JNDI initial context.

Listing 2. JNDI context template

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">
                org.jnp.interfaces.NamingContextFactory
            </prop>
            <prop key="java.naming.provider.url">
                localhost
            </prop>
            <prop key="java.naming.factory.url.pkgs">
                org.jnp.interfaces:org.jboss.naming
            </prop>
        </props>
    </property>
</bean>

Next, we configure the queue connection factory. Listing 3 shows the configuration for the queue connection factory.

Listing 3. JMS queue connection factory configuration

<bean id="jmsQueueConnectionFactory"
      class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>UIL2ConnectionFactory</value>
    </property>
</bean>

We define two JMS destinations to send and receive the messages. Listings 4 and 5 show these details.

Listing 4. Configuration for send queue

<bean id="sendDestination"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>queue/CreditRequestSendQueue</value>
    </property>
</bean>

Listing 5. Configuration for receive queue

<bean id="receiveDestination"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>queue/CreditReqeustReceiveQueue</value>
    </property>
</bean>

Then we configure the JmsTemplate component. We use JmsTemplate102 for the sample application. We use the defaultDestination attribute to specify JMS destination.

Listing 6. JMS template configuration

<bean id="jmsTemplate" 
      class="org.springframework.jms.core.JmsTemplate102">
    <property name="connectionFactory">
        <ref bean="jmsQueueConnectionFactory"/>
    </property>
    <property name="defaultDestination">
        <ref bean="destination"/>
    </property>
    <property name="receiveTimeout">
        <value>30000</value>
    </property>
</bean>

Finally we configure sender and receiver components. Listings 7 and 8 show the configuration of Sender and Receiver objects.

Listing 7. JMS Sender configuration

<bean id="jmsSender" class="springexample.client.JMSSender">
    <property name="jmsTemplate">
        <ref bean="jmsTemplate"/>
    </property>
</bean>

Listing 8. JMS Receiver configuration

<bean id="jmsReceiver" class="springexample.client.JMSReceiver">
    <property name="jmsTemplate">
        <ref bean="jmsTemplate"/>
    </property>
</bean>

Pages: 1, 2, 3, 4, 5

Next Pagearrow