shayhatsor / zookeeper

Apache ZooKeeper .NET async Client
https://nuget.org/packages/ZooKeeperNetEx/
Apache License 2.0
236 stars 53 forks source link

Watch not triggered #33

Closed alexandrutatagit closed 5 years ago

alexandrutatagit commented 5 years ago

Hi!

I have a very simple example, I am reading a node from ZK, and then I want to see that I get notified when updates are made to that node. However, only my first change in the note gets notified, the rest not. Also, I am unsure what I must return in the process method, when extending the Watch abstract class. Thank you, Alex

class Program
{
    async static Task Main(string[] args)
    {
        Watcher watcher = new MyWatcher();
        ZooKeeper zk = new ZooKeeper("kafka1:2181,kafka2:2181,kafka3:2181", 30000, watcher);
        DataResult stat = await zk.getDataAsync("/alex/test", watcher);
        string json = Encoding.UTF8.GetString(stat.Data);
        while (true)
        {
            await Task.Delay(1000);
        }
    }
}

public class MyWatcher : Watcher
{
    public override Task process(WatchedEvent @event)
    {
        Console.WriteLine(@event.ToString());
        return Task.Delay(0);
    }
}
shayhatsor commented 5 years ago

this is by design.

alexandrutatagit commented 5 years ago

Ok, thank you, I see what you mean from the documentation.

  1. Then how do you implement triggers for every change? Documentation is preety small on the .Net Core side
  2. What is the process method supposed to return? What Task?
shayhatsor commented 5 years ago

1) There is no way to get notified about every change, it is explained in detail in the link i've provided earlier. An attempt to handle changes is explained here. About documentation, for any zookeeper related question you can always go to the official site. My C# port of the Apache Zookeeper Java Client provides the same API (in async flavour). Essentially, all Java Client code examples you find are applicable to the C# client. 2) The reason that the process method returns a Task is to allow async/await constructs usage. You can return Task\<T>, but it wouldn't make sense, since the return value is thrown away.

alexandrutatagit commented 5 years ago

Thank you, I managed to solve this.