创建 TIBCO Rendezvous Listener 等待对不同主题和候选事件键的响应

Creating TIBCO Rendezvous Listener wait for response on different subject and candidate event key

发布的消息和收到的回复是关于两个不同主题的。现在我的 java class 中有以下一组功能。 Class 正在实现 TibrvMsgCallback 接口。 我怎样才能确保无论发布什么消息,我都能准确地收到它的响应?

public class TibcoRVUtility implements TibrvMsgCallback {
    public void onMsg(TibrvListener listener, TibrvMsg msg) {
           try {
                _log.info("Request and Response found");

                msgReceived = true;
        } catch (final TibrvException ex) {
            _log.error("Exception@" + this.getClass().getName() + ".onMsg", ex);
        }
    }

    private void sendMessage(String messageString, final String soType,
            final String responseSubject) {
        try {

            Tibrv.open(Tibrv.IMPL_NATIVE);

            TibrvTransport transport = new TibrvRvdTransport(tibcoSetting.getService(), tibcoSetting.getNetwork(),
                    tibcoSetting.getDaemon());
            String inboxName = transport.createInbox();

            TibrvMsg msg = new TibrvMsg();
            msg.setSendSubject("PUBLISH_SUBJECT");
            msg.add("DATA", "DUMMY_MESSAGE");

            TibrvListener listener = new TibrvListener(Tibrv.defaultQueue(), this, transport, responseSubject, null);

            transport.send(msg);

            _log.info("msg" + msg.toString());
            _log.info("message successfully sent.");
            while (!msgReceived) {
                try {
                    Tibrv.defaultQueue().dispatch();
                } catch (InterruptedException ex) {
                    _log.error("Exception@" + this.getClass().getName() + ".sendMessage", ex);
                    break;
                } catch (TibrvException ex) {
                    _log.error("Exception@" + this.getClass().getName() + ".sendMessage", ex);
                    break;
                }
            }

            listener.destroy();
            transport.destroy();

        } catch (TibrvException e) {
            _log.error("Exception@" + this.getClass().getName() + ".sendMessage", e);
        }
    }
}

发送消息时,添加另一个字段

var correlation_id = Guid.NewGuid().ToString();
msg.add("CORRELATION_ID", correlation_id);

然后将该关联 ID 存储在某处,也许在哈希集中。

让发布者阅读请求的 CORRELATION_ID 并将其添加到响应中。

当您收到一条消息时,仅当它在 CORRELATION_ID 字段中具有您期望的 ID 时才处理它。