Tibco-Rv:如何使用TibRv-Java API建立双向通信

时间:2018-06-23 07:01:12

标签: messaging tibco-rv tibco-topic

使用TibRvdTransport->send(msg)发送Tbrv消息时是否可以进行回调,并且在订户中我们可以发送回复吗?

  

我要从发布者发送“你好”,接收者应该接收它   并发送“嗨”答复。发布者必须在回调中获得此“ Hi”,   打印出来。

Publisher =>

TibrvRvdTrasport transport= new TibrvRvdTrasport ("12000","127.0.0.1","6000");
TibrvMsg tibMsg = new TibrvMsg();
tibMsg.add("msg" "hello");
tibMsg.setSendSubject(subject);
transport.send(tibMsg);

Subscriber =>

listener = new TibRvListener(tibRvQueue, new TibRvMsgCallback(){
    @Override
    public void onMsg(TibRvListener listener,TibRvMsg msg){
        try{
            -----//sendReply("Hi")
        }
        catch(Exception e){

        }
    }, 
    new TibRvdTransport("12000","127.0.0.1","6000")),subject,null);

1 个答案:

答案 0 :(得分:0)

当然,这是一种方法。通常,您创建一个私人“收件箱”主题,该主题用作原始请求的回复主题。该“收件箱”只是一个简单的唯一字符串。可以是任何东西(也可以是“ 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();