简体   繁体   中英

Tibco-Rv: How to establish two way communication using TibRv-Java api

Is it possible to have a callback while sending Tbrv msg using TibRvdTransport->send(msg) and in subscriber can we send a reply ?

I want to send "Hello" from publisher and receiver should receieve it and send "Hi" reply. Publisher must get this "Hi" in callback and print it.

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);

Sure, here's one way of doing this. Typically you create a private 'inbox' subject that is used as reply subject on the original request. This 'inbox' is just a simple unique string. It could be anything (also "REPLY"), but it's useful to have a unique one in most cases.

Sender side:

  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();

And receiver side:

  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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM