当然,这是一种方法。通常,您会创建一个私有“收件箱”主题,用作原始请求的回复主题。这个“收件箱”只是一个简单的唯一字符串。它可以是任何东西(也可以是“REPLY”),但在大多数情况下拥有一个独特的是很有用的。
发送方:
Tibrv.open(Tibrv.IMPL_NATIVE);
TibrvRvdTransport transport = new TibrvRvdTransport ("12000","127.0.0.1","6000");
TibrvMsg request = new TibrvMsg();
request.add("msg", "hello ");
request.setSendSubject("TEST");
request.setReplySubject(transport.createInbox()); // the subject we expect a reply on
System.err.println("sending request: " + request);
TibrvMsg reply = transport.sendRequest(request, 10*1000); // wait 10 seconds for reply
System.err.println("received response: " + reply);
Tibrv.close();
和接收方:
Tibrv.open(Tibrv.IMPL_NATIVE);
TibrvRvdTransport transport = new TibrvRvdTransport ("12000","127.0.0.1","6000");
new TibrvListener( Tibrv.defaultQueue(), new TibrvMsgCallback() {
@Override
public void onMsg(TibrvListener listener, TibrvMsg msg)
{
try {
System.err.println("received request: " + msg );
TibrvMsg reply = new TibrvMsg();
reply.setSendSubject(msg.getReplySubject()); // send response to the 'reply' subject
reply.add("response","world!");
System.err.println("sending response: " + reply );
transport.send(reply);
}
catch (TibrvException e) {
e.printStackTrace();
}
}}, transport, "TEST", null );
TibrvDispatcher dispatcher = new TibrvDispatcher(Tibrv.defaultQueue());
Thread.sleep(100*1000);
dispatcher.destroy();
Tibrv.close();