/**
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.fabric8.kubernetes.examples;

import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static io.fabric8.kubernetes.client.Watcher.Action.ERROR;

public class WatchExample {

  private static final Logger logger = LoggerFactory.getLogger(WatchExample.class);

  public static void main(String[] args) throws InterruptedException {
    String master = "http://localhost:8080/";
    if (args.length == 1) {
      master = args[0];
    }

    final CountDownLatch closeLatch = new CountDownLatch(1);
    Config config = new ConfigBuilder().withMasterUrl(master).withWatchReconnectLimit(2).build();
    try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
      try (Watch watch = client.replicationControllers().inNamespace("default").withName("test").watch(new Watcher<ReplicationController>() {
        @Override
        public void eventReceived(Action action, ReplicationController resource) {
          logger.info("{}: {}", action, resource.getMetadata().getResourceVersion());
        }

        @Override
        public void onClose(KubernetesClientException e) {
          if (e != null) {
            logger.error(e.getMessage(), e);
            closeLatch.countDown();
          }
        }
      })) {
        closeLatch.await(10, TimeUnit.SECONDS);
      } catch (KubernetesClientException | InterruptedException e) {
        logger.error("Could not watch resources", e);
      }
      try (Watch watch = client.replicationControllers().inNamespace("default").watch(new Watcher<ReplicationController>() {
        @Override
        public void eventReceived(Action action, ReplicationController resource) {
          logger.info("{}: {}", action, resource.getMetadata().getResourceVersion());
        }

        @Override
        public void onClose(KubernetesClientException e) {
          if (e != null) {
            logger.error(e.getMessage(), e);
            closeLatch.countDown();
          }
        }
      })) {
        closeLatch.await(10, TimeUnit.SECONDS);
      } catch (KubernetesClientException | InterruptedException e) {
        logger.error("Could not watch resources", e);
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage(), e);

      Throwable[] suppressed = e.getSuppressed();
      if (suppressed != null) {
        for (Throwable t : suppressed) {
          logger.error(t.getMessage(), t);
        }
      }
    }
  }

}

@@ -706,55 +706,8 @@
 er;%0A
-import io.fabric8.kubernetes.api.model.Status;%0A
 impo
@@ -1208,74 +1208,8 @@
 t;%0A%0A
-import static io.fabric8.kubernetes.client.Watcher.Action.ERROR;%0A%0A
 publ
@@ -1393,113 +1393,8 @@
 n %7B%0A
-    String master = %22http://localhost:8080/%22;%0A    if (args.length == 1) %7B%0A      master = args%5B0%5D;%0A    %7D%0A%0A
     
@@ -1494,57 +1494,8 @@
 r().
-withMasterUrl(master).withWatchReconnectLimit(2).
 buil
@@ -2010,718 +2010,40 @@
     
-if (e != null) %7B%0A            logger.error(e.getMessage(), e);%0A            closeLatch.countDown();%0A          %7D%0A        %7D%0A      %7D)) %7B%0A        closeLatch.await(10, TimeUnit.SECONDS);%0A      %7D catch (KubernetesClientException %7C InterruptedException e) %7B%0A        logger.error(%22Could not watch resources%22, e);%0A      %7D%0A      try (Watch watch = client.replicationControllers().inNamespace(%22default%22).watch(new Watcher%3CReplicationController%3E() %7B%0A        @Override%0A        public void eventReceived(Action action, ReplicationController resource) %7B%0A          logger.info(%22%7B%7D: %7B%7D%22, action, resource.getMetadata().getResourceVersion());%0A        %7D%0A%0A        @Override%0A        public void onClose(KubernetesClientException e) %7B
+logger.debug(%22Watcher onClose%22);
 %0A   
@@ -2645,15 +2645,41 @@
 %7D%0A    %7D%0A
+    Thread.sleep(60000l);%0A
   %7D%0A%0A%7D%0A

Clean up WatchExample to simplify troubleshooting