Created
November 24, 2020 06:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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