bonifaido / rust-zookeeper

Pure Rust library for Apache ZooKeeper built on MIO
MIT License
206 stars 46 forks source link

how to receiver event when node data change? #55

Closed isnlan closed 5 years ago

isnlan commented 5 years ago
struct LoggingWatcher;
impl Watcher for LoggingWatcher {
    fn handle(&self, e: WatchedEvent) {
        info!("{:?}", e)
    }
}

struct RootWatcher;
impl Watcher for RootWatcher {
    fn handle(&self, e: WatchedEvent) {
        info!("Root->> {:?}", e)
    }
}

fn zk_example2() {
    let zk = ZooKeeper::connect("127.0.0.1/test", Duration::from_secs(15), LoggingWatcher).unwrap();

    zk.add_listener(|zk_state| println!("New ZkState is {:?}", zk_state));

    // how to receiver event when node data change?
    let data = zk.get_data_w("/", RootWatcher).unwrap();

    println!("press enter to close client");
    let mut tmp = String::new();
    io::stdin().read_line(&mut tmp).unwrap();
}

fn main(){
   zk_example2(); 
}
bonifaido commented 5 years ago

Hi @snlansky, get_data_w is the intended way of watching data events on a node, do you have any issues when using this method?

bonifaido commented 5 years ago

I have tried this example (triggering the event with a manual data change) and it works for me, have you exported RUST_LOG=info before running the example?

isnlan commented 5 years ago

@bonifaido Thank you very much for your reply, I cant receive event when i get_data_w ` path="\".

isnlan commented 5 years ago

Thank you, the problem is solved。