参考文档: 
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();
}

Spring 整合 Tibco EMS的更多相关文章

  1. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  2. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  3. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  6. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  7. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  8. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...

  9. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

随机推荐

  1. python环境准备

    一.环境准备. 1.安装python3.5.2(勾选环境变量),python2.7.12 2.设置环境变量 (要求命令行输入python,进入python2命令行,打python3时,进入python ...

  2. MWEB+七牛 上传图片

    MWEB+七牛 上传图片 博客之前的图片也都用的七牛,但编辑和上传分离还是很麻烦,所以一直很心水meb, 上周mweb降到¥50,感觉短期内应该不会再降了,于是果断入手,今天在和使用图床功能遇到了一些 ...

  3. Filter过滤器实现同一地址手机和电脑页面不同

    最近做一个网站,客户要求在访问主域名的时候实现电脑访问时展示电脑页面,手机访问时展示h5的手机页面,这种需求的使用还是比较多的:尤其网站需要百度推广的时候,百度推广就要求同一域名下,手机访问时展示手机 ...

  4. 如何在ASP.NET端获取屏幕宽度

    using System.Windows.Forms; 首先引用上面的命名空间. 然后在代码中获取屏幕信息.如下代码: System.Windows.Forms.Screen screen = Sys ...

  5. 移动Web开发技巧

    META相关 1. 添加到主屏后的标题(IOS) <meta name="apple-mobile-web-app-title" content="标题" ...

  6. solr建立索引的过程

    HttpSolrServer HttpSolrServer继承SolrServer 参考文档:http://my.oschina.net/qige/blog/173008

  7. ASP.NET5 静态文件

    静态文件,包括HTML文件,CSS文件,图像文件和JavaScript文件,它是一个应用里所包含的资源. 1. 提供静态文件 默认的,静态文件存储在你的webroot目录下面,webroot的路径定义 ...

  8. Css简介

  9. 156 Useful Run Commands

    To Access… Run Command Accessibility Controls access.cpl Accessibility Wizard accwiz Add Hardware Wi ...

  10. Swift - 05 - 数值型字面量

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...