Skip to content

Instantly share code, notes, and snippets.

@manusa
Created November 24, 2020 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manusa/01ce04b6ce4a5f070f88d4d62c4096ed to your computer and use it in GitHub Desktop.
Save manusa/01ce04b6ce4a5f070f88d4d62c4096ed to your computer and use it in GitHub Desktop.
JBang script showcasing functionality implemented in https://github.com/fabric8io/kubernetes-client/pull/2616
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.fabric8:kubernetes-client:5.0-SNAPSHOT
import static java.lang.System.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
public class TestWatcher {
public static void main(String... args) throws Exception {
try (KubernetesClient kc = new DefaultKubernetesClient()) {
final CountDownLatch cdl = new CountDownLatch(1);
try (Watch w = kc.pods().watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action arg0, Pod pod) {
out.printf("%s: %s - %s%n", Thread.currentThread().getName(), arg0, pod.getMetadata().getName());
}
@Override
public void onClose(WatcherException arg0) {
arg0.printStackTrace();
}
@Override
public void onClose() {
out.printf("%s: Watcher finished%n", Thread.currentThread().getName());
cdl.countDown();
}
})) {
// Watcher thread should print events (if any)
TimeUnit.SECONDS.sleep(2);
} catch(Exception ex) {
ex.printStackTrace();
}
// Watcher is being shutdown by try-with-resources close(), should eventually call the onClose method and print "Watcher finished"
cdl.await(5, TimeUnit.SECONDS);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment