Spring 整合 Tibco EMS
参考文档:
http://haohaoxuexi.iteye.com/blog/1893038
http://www.blogjava.net/chenhui7502/archive/2011/08/28/357457.html
网上大都是关于Spring和Apache整合的文章,很少有使用Tibco EMS的。都是遵循Spring JMS 标准,参考了这两篇文章后,做了一个demo跑Tibco EMS。可以运行。
1. Import jar 包
2. 配置文件
<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd "> <bean id="targetConnectionFactory" class="com.tibco.tibjms.TibjmsConnectionFactory"> <property name="serverUrl" value="tcp://localhost:1234"/> </bean> <bean id="connectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter"> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> <property name="username" value="username" /> <property name="password" value="password" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> </bean> <bean id="queueDestination" class="com.tibco.tibjms.TibjmsQueue"> <constructor-arg> <value>queue.name</value> </constructor-arg> </bean> <bean id="consumerMessageListener" class="euc.demo.emsqueue.ConsumerMessageListener"/> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="consumerMessageListener" /> </bean> </beans>
3. Send Message的类
package euc.demo.emsqueue; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.StreamMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Component; @Component public class SendEmsQueueMessage { private JmsTemplate jmsTemplate; public void sendTextMessage(Destination destination, final String message) { System.out.println("The producer send a message:" + message); jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } public void sendStreamMessage(Destination destination, final byte[] content){ System.out.println("The producer send a message:" + new String(content)); jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { StreamMessage message = session.createStreamMessage(); message.setIntProperty("length", content.length); message.writeBytes(content); return message; } }); } public JmsTemplate getJmsTemplate() { return jmsTemplate; } @Resource public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } }
4. 接收message的类
package euc.demo.emsqueue; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.StreamMessage; import javax.jms.TextMessage; public class ConsumerMessageListener implements MessageListener { public void onMessage(Message message) { if (message instanceof javax.jms.TextMessage) { TextMessage textMsg = (TextMessage) message; try { System.out.println("The receiver received a message:" + textMsg.getText()); } catch (JMSException e) { e.printStackTrace(); } } else if(message instanceof javax.jms.StreamMessage){ StreamMessage streamMsg = (StreamMessage)message; try { int length = message.getIntProperty("length"); byte[] content = new byte[length]; if(streamMsg.readBytes(content)>0) { String str = new String(content); System.out.println("The message length is:" + length); System.out.println("The receiver received a message:" + str); } } catch (JMSException e) { e.printStackTrace(); } } } }
5. 测试
public static void testEmsQueue() { ApplicationContext ac = new ClassPathXmlApplicationContext( "spring-config.xml"); SendEmsQueueMessage sendEmsQueueMessage = (SendEmsQueueMessage) ac.getBean("sendEmsQueueMessage"); Destination destination = (Destination) ac.getBean("queueDestination"); String str = "<?xmlversion=\"1.0\"encoding=\"UTF-8>"; // sendEmsQueueMessage.sendTextMessage(destination,"Text message"); sendEmsQueueMessage.sendStreamMessage(destination, str.getBytes()); } public static void main(String[] args) { testEmsQueue(); }