ITeye博客

TibcoRV入门实例

jssay · 2010-09-04 10:43 · Java · 企业架构 · Markdown

原文地址:http://www.jssay.com/blog/index.php/2010/09/03/tibcorv入门实例/

Tibco RV(Rendezvous) 出来将近20年了, 但是在网上的中文资源还是少的可怜,可能是因为Tibco RV的使用成本太高,以至于很多中小型公司都放弃使用了.我也是最近才接触到一点TibcoRV的知识,希望我的一点总结能对大家有用. Tibco RV是基于发布/订阅模式的,消息发送方被称为发布者(Publisher),消息接收方称为订阅者(Subscriber). 一个消息的生产者可以发布一条消息给多个消费者,通过一条叫做主题(Topic)的虚拟通道.消息消费者可以选择订阅他们感兴趣的主题(Topic), 任何关于此主题(Topic)的消息都会被发送到订阅此主题的订阅者那里. 下图简单描述了发布/订阅模式:

在了解了原理之后,我想通过代码来感受一下TibcoRv的神气力量, 下面定义了两个基本的类:TibSubScriber和TibPublisher,分别表示订阅者和发布者.

public class TibSubscriber implements TibrvMsgCallback {

private TibrvRvdTransport transport = null;

//初始化TibrvListener public TibSubscriber() { try { Tibrv.open(Tibrv.IMPL_NATIVE); transport = new TibrvRvdTransport( ConfigUtil.getProperty(Constants.TIBCO_SERVICE), ConfigUtil.getProperty(Constants.TIBCO_NETWORK), ConfigUtil.getProperty(Constants.TIBCO_DAEMON)); new TibrvListener(Tibrv.defaultQueue(), this, transport, ConfigUtil.getProperty(Constants.TIBCO_SUBJECT), null); } catch (TibrvException e) { e.printStackTrace(); } }

//监听 public void listen() { while (true) { try { Tibrv.defaultQueue().dispatch(); } catch(TibrvException ex) { ex.printStackTrace(); } catch(InterruptedException ex) { ex.printStackTrace(); } } }

//回调方法,监听到指定Subject时触发 public void onMsg(TibrvListener listener, TibrvMsg msg) { if (msg != null) { String receivedMsg; try { receivedMsg = (String)msg.get("msg"); System.out.println(receivedMsg); } catch (TibrvException e) { e.printStackTrace(); }

} }

public static void main(String args) { TibSubscriber tibListener = new TibSubscriber(); tibListener.listen(); }

public class TibPublisher {

private TibrvRvdTransport transport = null;

public TibPublisher() { try { Tibrv.open(Tibrv.IMPL_NATIVE); transport = new TibrvRvdTransport( ConfigUtil.getProperty(Constants.TIBCO_SERVICE), ConfigUtil.getProperty(Constants.TIBCO_NETWORK), ConfigUtil.getProperty(Constants.TIBCO_DAEMON)); } catch (TibrvException e) { e.printStackTrace(); } }

public void publish(String sendMessage) { try { TibrvMsg msg = new TibrvMsg(); msg.setSendSubject(ConfigUtil.getProperty(Constants.TIBCO_SUBJECT)); msg.add(“msg”, sendMessage); transport.send(msg); } catch (TibrvException e) { e.printStackTrace(); } }

public static void main(String args) { TibPublisher tibPublisher = new TibPublisher(); tibPublisher.publish(“Hello, World”); }

运行时,先执行TibSubscriber的main方法开始监听Topic,然后执行TibPublisher的main方法开始发布消息, TibSubscriber监听到消息后,执行回调函数onMsg(). 打印结果如下: Hello, World 这就是经典的Hello World入门例子(需要下载tibrvj.jar), 希望大家能够喜欢!

由于本人水平有限,错误再所难免,希望各位大虾批判指正. 原创文章,转载请著名出处!

相关文章: TibcoRV vs Tibco EMS: http://narencoolgeek.blogspot.com/2006/01/tibco-rv-vs-tibco-ems.html TibcoRV vs SonicMQ: http://www.cs.cmu.edu/~priya/WFoMT2002/Pang-Maheshwari.pdf

评论(1)

  1. #1 楼 · u_lie · 2013-02-01 11:05

    你好: 以前没有用过RV,向你请教一下,使用您上面的例子怎么实现两台机器之间的通信,该如何配置?

    请问 ConfigUtil.getProperty(Constants.TIBCO_SERVICE), ConfigUtil.getProperty(Constants.TIBCO_NETWORK), ConfigUtil.getProperty(Constants.TIBCO_DAEMON)); 这三个参数 service、network、daemon具体是什么?帮忙解释下!谢谢!

评论功能维护中,当前仅展示历史评论。