java tibco rv server和client的demo
时间: 2023-10-11 10:13:04 浏览: 374
TIBCO Rendezvous(RV)是一种基于消息的中间件,它提供了高效、可靠的消息传递机制。下面是一个简单的 Java RV Server 和 Client 的示例代码:
Server:
```java
import com.tibco.tibrv.*;
public class RVServer implements TibrvMsgCallback {
private TibrvQueue queue;
private TibrvRvdTransport transport;
public RVServer(String service, String network, String daemon) throws TibrvException {
Tibrv.open();
queue = new TibrvQueue();
transport = new TibrvRvdTransport(service, network, daemon);
new TibrvListener(queue, this, transport, "test.subject", null);
}
public void onMsg(TibrvListener listener, TibrvMsg msg) {
try {
String message = msg.getField("message").data.toString();
System.out.println("Received message: " + message);
} catch (TibrvException e) {
e.printStackTrace();
}
}
public void start() throws TibrvException {
queue.dispatch();
}
public void stop() throws TibrvException {
Tibrv.close();
}
public static void main(String[] args) {
try {
RVServer server = new RVServer("7500", null, "tcp:7500");
server.start();
} catch (TibrvException e) {
e.printStackTrace();
}
}
}
```
Client:
```java
import com.tibco.tibrv.*;
public class RVClient {
private TibrvRvdTransport transport;
public RVClient(String service, String network, String daemon) throws TibrvException {
Tibrv.open();
transport = new TibrvRvdTransport(service, network, daemon);
}
public void send(String subject, String message) throws TibrvException {
TibrvMsg msg = new TibrvMsg();
msg.setSendSubject(subject);
msg.add("message", message);
transport.send(msg);
System.out.println("Sent message: " + message);
}
public void stop() throws TibrvException {
Tibrv.close();
}
public static void main(String[] args) {
try {
RVClient client = new RVClient("7500", null, "tcp:7500");
client.send("test.subject", "Hello, World!");
client.stop();
} catch (TibrvException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,Server 和 Client 都使用了 `TibrvRvdTransport` 来进行通信。Server 运行在本地的 `7500` 端口上,监听 `test.subject` 主题。当收到消息时,会调用 `onMsg` 方法进行处理。Client 发送一条消息到 `test.subject` 主题。
需要注意的是,上述代码需要使用 TIBCO RV Java API 来编译和运行。你需要在项目中添加相应的 JAR 文件,并在代码中进行引用。
阅读全文