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();
}
Spring 整合 Tibco EMS的更多相关文章
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- Spring整合HBase
Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...
- Spring整合Ehcache管理缓存(转)
目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...
随机推荐
- 9.13noip模拟试题
题目名称 “与” 小象涂色 行动!行动! 输入文件 and.in elephant.in move.in 输出文件 and.out elephant.in move.in 时间限制 1s 1s 1s ...
- C#之—委托
(1)定义委托:(百度百科样例,只有写了才有收获) namespace Entrust { public delegate void GreetingDelegate(string name); // ...
- .net mvc datatables中orderby动态排序
今天在做项目中用datatables的排序来做筛选,不过人比较懒,不想写那么多的关于排序的代码,于是寻思这在度娘上找找,结果不负有心人啊,更感谢贴出此贴的哥们,来源:http://blog.csdn. ...
- (转)iFrame高度自适应
第一种方法:代码简单,兼容性还可以,大家可以先测试下: function SetWinHeight(obj) { var win=obj; if (document.getElementById) { ...
- python中的generator, iterator, iterabel
先来看看如果遇到一个对象,如何判断其是否是这三种类型: from types import GeneratorType from collectiuons import Iterable, Itera ...
- IE8-下背景色半透明滤镜在jquery动画中失效问题记录
前两天,UIer跟我说,把这些按钮都悬浮在这个图片上!我心中千万头草泥马奔过,图片各种各样.花花绿绿.五颜六色的,这几个按钮也没有多大的光环围绕,用户一眼看上去恐怕会以为这是图片的一部分吧!~~~我假 ...
- ASP.NET菜鸟之路之Response小例子
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Response.Write Redirect ...
- IOS 在Xcode 4.x以上添加静态库
参考网站:http://my.oschina.net/edwardlau/blog/95924 常用的代码可以通过静态库进行抽出来作为公共类方法,方便在其他地方调用,一般来说我们要准备2套静态库,一套 ...
- NoSQL数据库技术特性解析之文档数据库
现今云计算的从业人员对NoSQL一词并不感到陌生,虽然很多技术人员都长期从事关系数据库的工作,但现在他们对NoSQL技术充满期待.对于企业来说,从关系型数据库到NoSQL数据库转变绝对是个需要深思熟虑 ...
- nginx配置无效的问题
今天修改nginx的一个配置文件,却怎么都没效果,发现是启动nginx指定的配置文件不一样. #ps aux|grep nginx root 17672 0.0 0.0 45856 2 ...