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.
We welcome your feedback <users-AT-mq.java.net> ------ A. Instruction 1. Download jar files needed
2. Compile the example Classes
javac -classpath .:jms.jar:imq.jar MQConnectionFactoryFactory.java
javac -classpath .:jms.jar SimpleMessageListener.java
javac -classpath .:jms.jar:imq.jar:spring.jar CConsumerDemo.java 3. Start MQ broker 4. Edit Spring application context configuration file for the example
5. Run the example java -classpath <list of above jar files> CConsumerDemo B. The Spring application context configuration for the example <?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; } } |