Search in sources :

Example 26 with Client

use of io.etcd.jetcd.Client in project jetcd by coreos.

the class Main method main.

public static void main(String[] args) {
    Args global = new Args();
    CommandGet getCmd = new CommandGet();
    CommandPut putCmd = new CommandPut();
    CommandWatch watchCmd = new CommandWatch();
    JCommander jc = JCommander.newBuilder().addObject(global).addCommand("get", getCmd).addCommand("put", putCmd).addCommand("watch", watchCmd).build();
    jc.parse(args);
    String cmd = jc.getParsedCommand();
    if (cmd == null || global.help) {
        jc.usage();
        return;
    }
    try (Client client = Client.builder().endpoints(global.endpoints.split(",")).build()) {
        switch(cmd) {
            case "get":
                getCmd.accept(client);
                break;
            case "put":
                putCmd.accept(client);
                break;
            case "watch":
                watchCmd.accept(client);
                break;
        }
    } catch (Exception e) {
        LOGGER.error(cmd + " Error {}", e);
        System.exit(1);
    }
}
Also used : JCommander(com.beust.jcommander.JCommander) Client(io.etcd.jetcd.Client)

Example 27 with Client

use of io.etcd.jetcd.Client in project dubbo by alibaba.

the class JEtcdClientTest method test_watch_when_modify.

@Test
public void test_watch_when_modify() {
    String path = "/dubbo/config/jetcd-client-unit-test/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch latch = new CountDownLatch(1);
    ByteSequence key = ByteSequence.from(path, UTF_8);
    Watch.Listener listener = Watch.listener(response -> {
        for (WatchEvent event : response.getEvents()) {
            Assertions.assertEquals("PUT", event.getEventType().toString());
            Assertions.assertEquals(path, event.getKeyValue().getKey().toString(UTF_8));
            Assertions.assertEquals("Hello", event.getKeyValue().getValue().toString(UTF_8));
            latch.countDown();
        }
    });
    try (Client client = Client.builder().endpoints(endpoint).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {
        // try to modify the key
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        latch.await();
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : Watch(io.etcd.jetcd.Watch) ByteString(com.google.protobuf.ByteString) WatchEvent(io.etcd.jetcd.watch.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) Test(org.junit.jupiter.api.Test)

Example 28 with Client

use of io.etcd.jetcd.Client in project dubbo by alibaba.

the class JEtcdClientTest method testCancelWatchWithGrpc.

@Test
public void testCancelWatchWithGrpc() {
    String path = "/dubbo/config/testCancelWatchWithGrpc/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch updateLatch = new CountDownLatch(1);
    CountDownLatch cancelLatch = new CountDownLatch(1);
    final AtomicLong watchID = new AtomicLong(-1L);
    try (Client client = Client.builder().endpoints(endpoint).build()) {
        ManagedChannel channel = getChannel(client);
        StreamObserver<WatchRequest> observer = WatchGrpc.newStub(channel).watch(new StreamObserver<WatchResponse>() {

            @Override
            public void onNext(WatchResponse response) {
                watchID.set(response.getWatchId());
                for (Event event : response.getEventsList()) {
                    Assertions.assertEquals("PUT", event.getType().toString());
                    Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8));
                    Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8));
                    updateLatch.countDown();
                }
                if (response.getCanceled()) {
                    // received the cancel response
                    cancelLatch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        });
        // create
        WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder().setKey(ByteString.copyFrom(path, UTF_8));
        // make the grpc call to watch the key
        observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build());
        // try to put the value
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        // response received, latch counts down to zero
        updateLatch.await();
        WatchCancelRequest watchCancelRequest = WatchCancelRequest.newBuilder().setWatchId(watchID.get()).build();
        WatchRequest cancelRequest = WatchRequest.newBuilder().setCancelRequest(watchCancelRequest).build();
        observer.onNext(cancelRequest);
        // try to put the value
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello world", UTF_8));
        cancelLatch.await();
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : WatchRequest(io.etcd.jetcd.api.WatchRequest) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) WatchCancelRequest(io.etcd.jetcd.api.WatchCancelRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) WatchCreateRequest(io.etcd.jetcd.api.WatchCreateRequest) ManagedChannel(io.grpc.ManagedChannel) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Event(io.etcd.jetcd.api.Event) Client(io.etcd.jetcd.Client) Test(org.junit.jupiter.api.Test)

Aggregations

Client (io.etcd.jetcd.Client)28 Test (org.junit.jupiter.api.Test)20 ByteSequence (io.etcd.jetcd.ByteSequence)13 KV (io.etcd.jetcd.KV)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Watch (io.etcd.jetcd.Watch)5 WatchEvent (io.etcd.jetcd.watch.WatchEvent)5 ByteString (com.google.protobuf.ByteString)4 ClientBuilder (io.etcd.jetcd.ClientBuilder)4 Watcher (io.etcd.jetcd.Watch.Watcher)4 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)4 PutResponse (io.etcd.jetcd.kv.PutResponse)4 URI (java.net.URI)4 TimeUnit (java.util.concurrent.TimeUnit)4 Event (io.etcd.jetcd.api.Event)3 WatchCreateRequest (io.etcd.jetcd.api.WatchCreateRequest)3 EtcdClusterExtension (io.etcd.jetcd.test.EtcdClusterExtension)3 ArrayList (java.util.ArrayList)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 WatchCancelRequest (io.etcd.jetcd.api.WatchCancelRequest)2