Thursday, January 14, 2010

Connecting to MQ using Spring without WAS

Here's how to connect to MQ using spring without WAS.  First, here's the properties value for the connection settings:
# MQ related values
mq.jms.qcf=IBMMQQCF
mq.jms.request.queue=TEST.REQUEST.QUEUE

# Connection details
mq.host.url=192.168.1.200:1415/SYSTEM.DEF.SVRCONN
mq.factoryclass=com.ibm.mq.jms.context.WMQInitialContextFactory

# Authentication details
mq.auth=simple
mq.user=username
mq.password=password
Now the Spring beans configuration:
    <bean id="queueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${mq.jms.qcf}" />
        <property name="resourceRef" value="false" />
        <property name="jndiEnvironment">
            <props>
                <prop key="java.naming.factory.initial">${mq.factoryclass}</prop>
                <prop key="java.naming.provider.url">${mq.host.url}</prop>
                <prop key="java.naming.security.authentication">${mq.auth}</prop>
                <prop key="java.naming.security.principal">${mq.user}</prop>
                <prop key="java.naming.security.credentials">${mq.password}</prop>
            </props>
        </property>
    </bean>

    <bean id="requestQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${mq.jms.request.queue}" />
        <property name="resourceRef" value="false" />
        <property name="jndiEnvironment">
            <props>
                <prop key="java.naming.factory.initial">${mq.factoryclass}</prop>
                <prop key="java.naming.provider.url">${mq.host.url}</prop>
                <prop key="java.naming.security.credentials">${mq.password}</prop>
                <prop key="java.naming.security.authentication">${mq.auth}</prop>
            </props>
        </property>
    </bean>
Once that's done, you can use the normal JMS calls to send the message to the queue.  Below is a sample of how to send a message:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

public class JMSProcessor {
    private QueueConnectionFactory qcf;
    private Queue reqQ;
    private Queue respQ;

    private long timeout = 0;

    private Logger log = Logger.getLogger(MessageProcessor.class);

    public void sendMessage(final String msg) throws Exception {
       
        String messageOut = null;
       
        QueueConnection connection = null;
        QueueSession queueSession = null;
        QueueSender queueSender = null;

        try {
            connection = qcf.createQueueConnection();
            connection.start();
       
            queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
       
            queueSender = queueSession.createSender(reqQ);
           
            TextMessage reqMsg = queueSession.createTextMessage(msg);
           
            reqMsg.setJMSDestination(reqQ);

            queueSender.send(reqMsg);

        } catch (Exception e) {
            throw e;

        } finally {
            try {
                connection.close();
                queueSession.close();
                queueSender.close();
            } catch (JMSException e1) {
                log.error(e1.getMessage(), e1);
            }
        }
    }

}


No comments: