Java Code Examples for io.etcd.jetcd.Watch#Watcher

The following examples show how to use io.etcd.jetcd.Watch#Watcher . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From jetcd with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) {
    Args cmd = new Args();

    JCommander.newBuilder().addObject(cmd).build().parse(args);

    CountDownLatch latch = new CountDownLatch(cmd.maxEvents);
    ByteSequence key = ByteSequence.from(cmd.key, StandardCharsets.UTF_8);
    Collection<URI> endpoints = Util.toURIs(cmd.endpoints);

    Watch.Listener listener = Watch.listener(response -> {
        LOGGER.info("Watching for key={}", cmd.key);

        for (WatchEvent event : response.getEvents()) {
            LOGGER.info("type={}, key={}, value={}", event.getEventType(),
                Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(StandardCharsets.UTF_8)).orElse(""),
                Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(StandardCharsets.UTF_8))
                    .orElse(""));
        }

        latch.countDown();
    });

    try (Client client = Client.builder().endpoints(endpoints).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {

        latch.await();
    } catch (Exception e) {
        LOGGER.error("Watching Error {}", e);
        System.exit(1);
    }
}
 
Example 2
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doUnsubscribe(final ClusterBooking booking) {
    EtcdClusterBooking etcdBooking = (EtcdClusterBooking) booking;
    Watch.Watcher watcher = etcdBooking.getWatcher();
    if (watcher != null) {
        watcher.close();
    }
    return CompletableFuture.completedFuture(null);
}
 
Example 3
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doUnsubscribe(final ConfigBooking booking) {
    EtcdConfigBooking etcdBooking = (EtcdConfigBooking) booking;
    Watch.Watcher watcher = etcdBooking.getWatcher();
    if (watcher != null) {
        watcher.close();
    }
    return CompletableFuture.completedFuture(null);
}
 
Example 4
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public Watch.Watcher getWatcher() {
    return watcher;
}
 
Example 5
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public void setWatcher(Watch.Watcher watcher) {
    this.watcher = watcher;
}
 
Example 6
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public Watch.Watcher getWatcher() {
    return watcher;
}
 
Example 7
Source File: EtcdRegistry.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
public void setWatcher(Watch.Watcher watcher) {
    this.watcher = watcher;
}