tibcorv入门实例
原文地址: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