【问题标题】:For cometd client wait for publish message in queue before shutting downCometd 客户端在关闭前等待队列中的发布消息
【发布时间】:2020-10-09 02:34:27
【问题描述】:

我们正在以下列方式关闭 cometd 客户端。有没有办法确保所有未决事件都已发布?有时我们会看到一些在触发关闭之前发送的事件丢失的问题。

public void shutdown() {

        try {
            if (bayeuxClient != null) {
                bayeuxClient.getChannel(getChannelInfo()).unsubscribe();
                bayeuxClient.disconnect(10000L);
                bayeuxClient.waitFor(10000L, BayeuxClient.State.DISCONNECTED);
            }
            if (client != null) {
                client.stop();
            }
        } catch (Exception e) {
            log.warn(e.getMessage());
        } finally {
            client = null;
            bayeuxClient = null;
        }

    }

【问题讨论】:

    标签: java comet cometd


    【解决方案1】:

    如果您正在发布并发线程并断开连接,则断开连接可能会超过发布。

    一般来说,此问题必须在应用程序级别解决,即应由应用程序协调上次发布和断开连接。

    您有以下选择:

    使用批处理

    批处理确保排序:

    bayeuxClient.batch(() -> {
      // Last publish
      bayeuxClient.getChannel("/foo").publish("data");
      bayeuxClient.disconnect();
      // You can wait here.
      bayeuxClient.waitFor(10000L, BayeuxClient.State.DISCONNECTED);
    });
    

    使用消息监听器

    使用来自publish() 的消息侦听器,以便在最后一次发布完成时通知您,然后您可以断开连接:

    // Last publish
    bayeuxClient.getChannel("/foo").publish("data", publishReply -> {
      // Do not wait inside CometD listeners.
      bayeuxClient.disconnect();
    });
    // Wait outside CometD listeners. 
    bayeuxClient.waitFor(10000L, BayeuxClient.State.DISCONNECTED);
    

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多