Using Spring and Open MQ – ConnectionConsumer and ServerSession Cannot resolve external resource into attachment.

If you are interested in contributing to this FAQ, please read the Contribution Guidelines.
Back to Open MQ

Please note that these samples are posted for the benefit of the community. These samples are intended to help you understand how you can use Open MQ in various integration and application settings. Use of these samples is at your own risk.

We welcome your feedback <users-AT-mq.java.net>

------
The following simple MQ Spring example integrates MQ ConnectionConsumer with Spring's
ServerSession/ServerSessionPool implementation classes by using Spring's
ServerSessionMessageListenerContainer for concurrent JMS message processing.

A. Instruction

1. Download jar files needed

  • spring.jar - Spring 2.5.5, The Spring Framework
  • commons-logging-1.1.1.jar - Apache Commons Logging, dependency of Spring
  • aspectjrt.jar aspectjweaver.jar - AspectJ, comes with Spring 2.5.5
  • cglib-nodep-2.1_3.jar - comes with Spring 2.5.5
  • jms.jar - JMS 1.1
  • imq.jar - MQ 4.3 Client Runtime

2. Compile the example Classes

  • compile MQ dependent class
javac -classpath .:jms.jar:imq.jar MQConnectionFactoryFactory.java
  • compile POJO class
javac -classpath .:jms.jar SimpleMessageListener.java
  • compile Spring dependent class
javac -classpath .:jms.jar:imq.jar:spring.jar CConsumerDemo.java

3. Start MQ broker

4. Edit Spring application context configuration file for the example
if necessary (e.g. broker port)

  • cconsumercontext.xml

5. Run the example

java -classpath <list of above jar files> CConsumerDemo

B. The Spring application context configuration for the example
(cconsumercontext.xml content)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="connectionfactoryfactory"
     class="MQConnectionFactoryFactory">
        <property name="properties">
            <props>
                <prop key="imqAddressList">localhost:7676</prop>
            </props>
        </property>
</bean>

<bean id="connectionfactory"
     factory-bean="connectionfactoryfactory"
     factory-method="createConnectionFactory"/>

<bean id="springconnectionfactory" class="org.springframework.jms.connection.SingleConnectionFactory">
                <property name="targetConnectionFactory" ref="connectionfactory" />
</bean>

<bean id="myqueue" class="com.sun.messaging.Queue">
     <constructor-arg type="java.lang.String" value="myqueue"/>
</bean>

<bean id="cconsumercontainer"
       class="org.springframework.jms.listener.serversession.ServerSessionMessageListenerContainer">
       <property name="connectionFactory" ref="springconnectionfactory"/>
       <property name="destination" ref="myqueue"/>
       <property name="messageListener" ref="messagelistener"/>
</bean>

<bean id="messagelistener"
      class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
      <constructor-arg>
          <bean class="SimpleMessageListener"/>
      </constructor-arg>
</bean>

</beans>

C. Example Classes

import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;

public class CConsumerDemo {

    public static void main(String[] args) {

        System.out.println("CConsumerDemo.main() ..");

        ApplicationContext ctx = new FileSystemXmlApplicationContext("./cconsumercontext.xml");

        ctx.getBean("cconsumercontainer");
        try {
            System.out.println("CConsumerDemo.main(): Waiting for container shutdown ...");
            while (true) Thread.sleep(5000);
        } catch (Exception e) {
            System.out.println("CConsumerDemo.main(): Got exception: "+e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("CConsumerDemo.main(): Done!");

        System.exit(0);
    }

}
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class SimpleMessageListener implements MessageListener {

    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                System.out.println(
                "SimpleMessageListener.onMessage(): Received message: "+
                                      ((TextMessage) message).getText());
            }
            catch (JMSException ex) {
                System.out.println(
                "SimpleMessageListener.onMessage(): got exception: "+ex.getMessage());
                ex.printStackTrace();
                throw new RuntimeException(ex);
            }
        }
        else {
            throw new IllegalArgumentException(
            "MessageListener.onMessage(): Message must be of type TextMessage");
        }
    }
}
import java.util.Properties;
import java.util.Enumeration;
import javax.jms.*;

public class MQConnectionFactoryFactory {
    private Properties props;

    public void setProperties(Properties props) {
        this.props = props;
    }

    public ConnectionFactory createConnectionFactory(){
        com.sun.messaging.ConnectionFactory cf =
                new com.sun.messaging.ConnectionFactory();
        try{
            Enumeration keys = props.propertyNames();
            while (keys.hasMoreElements()) {
                 String name = (String)keys.nextElement();
                 String value = props.getProperty(name);
                 cf.setProperty(name, value);
            }
        } catch (Exception e){
            throw new RuntimeException(
            "MQConnectionFactoryFactory.createConnectionFactory() failed: "+
            e.getMessage(), e);
        }
        return cf;
    }
}