-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to cancel watch? #528
Comments
Hi, I don't think that would solve my solution. Actually when you look into the source code of What I'd like to propose is to introduce a method called |
Here is a code snippet that I worked it around with low-level grpc interface: @Test
public void testCancelWatchWithGrpc() {
String path = "/hello";
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());
}
}
private ManagedChannel getChannel(Client client) {
try {
// hack, use reflection to get the shared channel.
Field connectionField = client.getClass().getDeclaredField("connectionManager");
connectionField.setAccessible(true);
Object connection = connectionField.get(client);
Method channelMethod = connection.getClass().getDeclaredMethod("getChannel");
channelMethod.setAccessible(true);
ManagedChannel channel = (ManagedChannel) channelMethod.invoke(connection);
return channel;
} catch (Exception e) {
return null;
}
} The |
Hi
I use the following code to do test the jetcd watch API, my question is: how to cancel the watch with the watch API?
According to the doc, a watch_id is required to cancel the watch.
Is there a way to obtain the watch id?
The text was updated successfully, but these errors were encountered: