Zookeeper.net Client (ii) "Introduction to official-driven development" __.net

Source: Internet
Author: User
Tags zookeeper

First, the project structure is simple, as shown in figure:


Next is the Program.cs content:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
Using Zookeeperclient;

Using Zookeepernet;  Namespace Zktest {class Program {static void Main (string[] args) {string _address =
            "127.0.0.1:2181";
            
            Izookeeperfactory _factory=zookeeperfactory.instance; var ZK = _factory.
 
            Connect (_address); if (ZK. Exists ("/connectiontest", True) ==null) {//Create a directory node that does not control ACL permissions, and the node is permanent ZK.C Reate ("/connectiontest", "Testrootdata").

                GetBytes (), Ids.open_acl_unsafe, createmode.persistent); Create a subdirectory node//Temp node: After the client session fails or the connection is closed, the node is automatically deleted and no child nodes can be created under the temporary node, or the following error is reported: Org.apache.zookeeper.keeperexception$no Childrenforephemeralsexception//createmode.ephemeral//Temporary sequential node: Basic features are consistent with temporary nodes, creating nodes in the process of Zook Eeper will automatically append a monotonically growing number suffix after its name, as the new node name//createmode.ephemeralseqUential//Persistent node: Once the node is created, it persists and does not delete the//createmode.persistent//persistent sequential node because the client session fails:
                The basic feature is consistent with the persistent node, and zookeeper will automatically append a monotonically growing number suffix to the node after its name is created, as the new node name//createmode.persistentsequential Zk. Create ("/connectiontest/testchildpathone", "Testchilddataone". 
            GetBytes (), Ids.open_acl_unsafe, createmode.persistent); The var state = ZK.
            State;
            Thread.Sleep (2000); if (state. Equals (ZooKeeper.States.CONNECTED)) {string Contestdata= Encoding.Default.GetString (ZK.
                 GetData ("/connectiontest", True, null));
                Console.WriteLine ("Remove root node Data:" +contestdata); Remove the subdirectory node list ZK. GetChildren ("/connectiontest", true). ToList ().
                     ForEach (Delegate (String nodename) {Console.WriteLine ("Child directory Node Name:" +nodename);

                 }
                     ); Remove Child directory node data, returns byte[] string childtestdata = Encoding.Default.GetString (ZK.
                 GetData ("/connectiontest/testchildpathone", True, null));

                Console.WriteLine ("Remove subdirectory node data:" +childtestdata); Modify the data under the node/root/childone, the third parameter is version, if it is-1, it will ignore the modified version of the data, directly change ZK. SetData ("/connectiontest/testchildpathone", "childgeekmodify").
                 GetBytes (),-1); Query modified Data Childtestdata = Encoding.Default.GetString (ZK.
                 GetData ("/connectiontest/testchildpathone", True, null));
                Console.WriteLine ("Remove the subdirectory node modified data is:" + childtestdata); Delete/connectiontest/testchildpathone This node, the second parameter is version,-1 words are deleted directly, ignoring version//ZK.  
             
            Delete ("/connectiontest/testchildpathone",-1); } ZK.
            Dispose ();
          
            Console.WriteLine ("---------------------------------------------------");
        Console.readkey ();
 }
    }
}

Next look at the Watcher.cs code:

Namespace Zookeeperclient
{
    internal class Watcher:iwatcher
    {public
        void Process (Watchedevent @ Event)
        {
            if (@event. Type = = eventtype.nodedatachanged)
            {
                Console.WriteLine (@event. Path + "   This path node has changed. ");
            }  
        }
    }
}

Very important ZooKeeperFactory.cs and IZooKeeperFactory.cs:

Using System;
Using Zookeepernet;

Namespace Zookeeperclient
{public
    interface izookeeperfactory
    {
        zookeeper Connect (string address);
        
        Zookeeper Connect (string address, TimeSpan Timeoutspan);
        
        Zookeeper Connect (string address, TimeSpan Timeoutspan, Iwatcher watcher);

        Zookeeper Connect (string address, TimeSpan Timeoutspan, Iwatcher Watcher, Long sessionId, byte[] password);
    }

Using System;

Using Zookeepernet; Namespace Zookeeperclient {public sealed class Zookeeperfactory:izookeeperfactory {public static Izook

        Eeperfactory Instance = new Zookeeperfactory (); Private Zookeeperfactory () {}///Create a zookeeper instance, the first parameter is the target server address and port, the second parameter is the session timeout, and the third is the callback method when the node is changed p Ublic Zookeeper Connect (string address) {return Connect (address, new TimeSpan (0, 0, 0,), new Watch
        ER ()); Zookeeper Connect (string address, TimeSpan Timeoutspan) {return connect (address, ti
        Meoutspan, New Watcher ()); Zookeeper Connect (string address, TimeSpan Timeoutspan, Iwatcher watcher) {return n
        EW Zookeeper (address, timeoutspan, watcher); Zookeeper Connect (string address, TimeSpan Timeoutspan, Iwatcher Watcher, Long SessionId, byte[] Passwor D) {Return to New Zookeeper (address, Timeoutspan, watchEr, sessionId, password); }
    }
}

The final result is as shown in figure:




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.