protected final void watchInstanceNode() {
   if (!shouldProcessZKEvent()) {
     return;
   }
   Futures.addCallback(
       zkClient.getData(
           getInstancePath(),
           new Watcher() {
             @Override
             public void process(WatchedEvent event) {
               if (!shouldProcessZKEvent()) {
                 return;
               }
               switch (event.getType()) {
                 case NodeDataChanged:
                   watchInstanceNode();
                   break;
                 case NodeDeleted:
                   instanceNodeFailed(
                       KeeperException.create(KeeperException.Code.NONODE, getInstancePath()));
                   break;
                 default:
                   LOG.info("Ignore ZK event for instance node: {}", event);
               }
             }
           }),
       instanceNodeDataCallback,
       Threads.SAME_THREAD_EXECUTOR);
 }
 private void watchStateNode() {
   if (!shouldProcessZKEvent()) {
     return;
   }
   Futures.addCallback(
       zkClient.getData(
           getZKPath("state"),
           new Watcher() {
             @Override
             public void process(WatchedEvent event) {
               if (!shouldProcessZKEvent()) {
                 return;
               }
               switch (event.getType()) {
                 case NodeDataChanged:
                   watchStateNode();
                   break;
                 default:
                   LOG.debug("Ignore ZK event for state node: {}", event);
               }
             }
           }),
       stateNodeDataCallback,
       Threads.SAME_THREAD_EXECUTOR);
 }
Ejemplo n.º 3
0
  private void processMessage(final String path, final String messageId) {
    Futures.addCallback(
        zkClient.getData(path),
        new FutureCallback<NodeData>() {
          @Override
          public void onSuccess(NodeData result) {
            Message message = MessageCodec.decode(result.getData());
            if (message == null) {
              LOG.error("Failed to decode message for " + messageId + " in " + path);
              listenFailure(zkClient.delete(path, result.getStat().getVersion()));
              return;
            }
            if (LOG.isDebugEnabled()) {
              LOG.debug(
                  "Message received from "
                      + path
                      + ": "
                      + new String(MessageCodec.encode(message), Charsets.UTF_8));
            }
            if (handleStopMessage(
                message, getDeleteSupplier(path, result.getStat().getVersion()))) {
              return;
            }
            messageCallback.onReceived(
                callbackExecutor, path, result.getStat().getVersion(), messageId, message);
          }

          @Override
          public void onFailure(Throwable t) {
            LOG.error("Failed to fetch message content.", t);
          }
        });
  }