jurmous / etcd4j

Java / Netty client for etcd, the highly-available key value store for shared configuration and service discovery.
Apache License 2.0
267 stars 83 forks source link

Does etcd4j Etcdclient support lasting waitForChange function? #147

Closed regardfs closed 7 years ago

regardfs commented 7 years ago

For now,I could monitor one time etcd value change and do a reaction with code below:

EtcdResponsePromise etcdResponsePromise = etcdClient.getDir(etcdConfPrefix).recursive().waitForChange().timeout(-1, TimeUnit.SECONDS).send();

etcdResponsePromise.addListener(promise -> { System.out.println("changed"); });

I want to know does etcd4j Etcdclient support lasting waitForChange function?

lburgazzoli commented 7 years ago

If I properly understand you want a listener for events, in that case can you have a look at: https://github.com/apache/camel/blob/master/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdWatchConsumer.java

regardfs commented 7 years ago

@lburgazzoli , thanks a lot!!! I mean monitor etcd key and do sth, like this cmd:/usr/bin/etcdctl exec-watch --recursive /corekube/apiservers -- /opt/bin/echosth.sh

lburgazzoli commented 7 years ago

So yes you can look at the example I provided, it use an index to avoid loosing events in between

regardfs commented 7 years ago

@lburgazzoli , hi firstly, I am a new greenhand on java, I am a pythoner and goer. I have read all code in the link:https://github.com/apache/camel/blob/master/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdWatchConsumer.java but It seems that it still a one time event for etcdwatch.

/usr/bin/etcdctl exec-watch --recursive /corekube/apiservers -- 1.sh

that means watch on /corekube/apiservers and if sth changed it will run /opt/bin/echosth.sh and continue to watch on next modification.

/ # etcdctl exec-watch --recursive /configurations/gaia/foo -- "1.sh" Mon May 22 15:25:20 UTC 2017: changed Mon May 22 15:25:26 UTC 2017: changed Mon May 22 15:25:30 UTC 2017: changed

lburgazzoli commented 7 years ago

When the code starts it invokes the watch method

Then inside the callback, it invokes watch again so it loops.

lburgazzoli commented 7 years ago

Yes


Luca Burgazzoli

On Mon, May 22, 2017 at 5:48 PM, Leo.feng notifications@github.com wrote:

@lburgazzoli https://github.com/lburgazzoli you mean this one will start first and trigger watch() in onResponse()?

protected void doStart() throws Exception { super.doStart(); watch(); }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jurmous/etcd4j/issues/147#issuecomment-303140206, or mute the thread https://github.com/notifications/unsubscribe-auth/AByEhRlaC1Y9bvwsHczrvfiIKka6I7vyks5r8a5qgaJpZM4NiWGt .

regardfs commented 7 years ago

@lburgazzoli , Sorry for bothering u again and again, It seems that use start and onResponse together to generate the loop, but I could not comprehend how,so dismay..

lburgazzoli commented 7 years ago
regardfs commented 7 years ago

@lburgazzoli thanks a lot!!!!

regardfs commented 7 years ago

@lburgazzoli last question how to register onResponse?Is this one:

https://github.com/apache/camel/blob/3505e718db48cc0d8be5b47f6c4030c958d93a5f/components/camel-etcd/src/main/java/org/apache/camel/component/etcd/EtcdWatchConsumer.java#L153 Soooooooooo appreciating you patience and explaination!

lburgazzoli commented 7 years ago

Yes it is, you need to associate a callback to the promise you get

regardfs commented 7 years ago

@lburgazzoli hurray!!! Thanks, I wish u merry Christmas and happy new year :)