The "conversation" thing in zookeeper.

Source: Internet
Author: User
Tags sleep zookeeper zookeeper client
first, the basic concept

ZooKeeper client wants to request the service of ZooKeeper server, first must connect with ZooKeeper server, establish "session". Second, conduct 2.1. End "Session" 2.1.1, explicitly ending "session" by client side of "Session "

For example, if you now have the following code:

Package com.dslztx.zookeeper;

Import org.apache.zookeeper.*;
Import java.io.IOException;

Import Java.util.Date;
    public class Master {String hostport;
    Integer sessiontimeout;

    Watcher watcher;

    ZooKeeper ZK = null;
        Public Master (String hostport, Integer sessiontimeout, Watcher watcher) {this.hostport = Hostport;
        This.sessiontimeout = Sessiontimeout;
    This.watcher = Watcher;
    } void Startzk () throws IOException {ZK = new ZooKeeper (Hostport, sessiontimeout, watcher);
        } void Stopzk () throws Interruptedexception {if (ZK! = null) {zk.close (); }} public static void Main (string[] args) throws IOException, interruptedexception {String hostport = "
        127.0.0.1:2181 ";
        Integer sessiontimeout = 600000;

        Watcher Watcher = new Sessionwatcher ();

        Master m = new Master (hostport, sessiontimeout, watcher);

        M.startzk (); System.out.println ("Start to Create Session,now Time: "+ new Date ());

        Wait two minutes for the session to complete initialization of thread.sleep (60000 * 2);
        Explicitly close session System.out.println ("Close the Session,now Time:" + new Date ());

        M.stopzk ();
        Kill Process System.out.println ("Kill Process,now Time:" + new Date ());
    System.exit (0); }} class Sessionwatcher implements watcher {public void process (Watchedevent watchedevent) {SYSTEM.OUT.PR
    Intln (watchedevent); }
}

The results of the run are shown in Figure 1.

Figure 1

As shown in Figure 1, the "session" ID at this time is "0x1540339295d0004". After the program is running and running, you can execute the Zookeeper Server dump command to view the "session" information. View the results as shown in Figures 2 and 3, respectively.

Figure 2

Figure 3

As shown in Figure 2, Figure 3 shows that once the "session" instruction is issued at the client end of the "session", the "session" is immediately terminated. 2.1.2, the server side of "session" is based on expiration, ending "session"

Package com.dslztx.zookeeper;

Import org.apache.zookeeper.*;
Import java.io.IOException;

Import Java.util.Date;
    public class Master {String hostport;
    Integer sessiontimeout;

    Watcher watcher;

    ZooKeeper ZK = null;
        Public Master (String hostport, Integer sessiontimeout, Watcher watcher) {this.hostport = Hostport;
        This.sessiontimeout = Sessiontimeout;
    This.watcher = Watcher;
    } void Startzk () throws IOException {ZK = new ZooKeeper (Hostport, sessiontimeout, watcher);
        } void Stopzk () throws Interruptedexception {if (ZK! = null) {zk.close (); }} public static void Main (string[] args) throws IOException, interruptedexception {String hostport = "
        127.0.0.1:2181 ";
        Integer sessiontimeout = 600000;

        Watcher Watcher = new Sessionwatcher ();

        Master m = new Master (hostport, sessiontimeout, watcher);

        M.startzk (); System.out.println ("Start to Create Session,now Time: "+ new Date ());

        Wait two minutes for the session to complete initialization of thread.sleep (60000 * 2);
        Kill Process System.out.println ("Kill Process,now Time:" + new Date ());
    System.exit (0); }} class Sessionwatcher implements watcher {public void process (Watchedevent watchedevent) {SYSTEM.OUT.PR
    Intln (watchedevent); }
}

The results of the run are shown in Figure 4.

Figure 4

As shown in Figure 4, the "session" ID at this time is "0x1540339295d0003". After the program is running and running, you can execute the Zookeeper Server dump command to view the "session" information. View the results as shown in Figure 5,6,7,8 and 9, respectively.

Figure 5

Figure 6

Figure 7

Figure 8

Figure 9

As shown in Figure 5, figure 6, Figure 7, Figure 8 and Figure 9, for the "session" of the server side, only the "session" of the client side at "Timeout" (This "timeout" value here is taken to "600000", that is, 10 minutes) time does not send any data, the " Session "will be determined as" expired "and the" session "will not be terminated. 2.2, re-connected to another zookeeper Server, the original "session" unchanged

A zookeeper client connects to a zookeeper server, establishes a "session", and when the zookeeper client disconnects from the zookeeper server, the zookeeper The client selects one of the zookeeper servers in the Zookeeper server cluster (possibly the original zookeeper server) to be re-connected, and the resulting "session" is the original "session" instead of a new "session", the most obvious proof being " The session "ID has not changed.
Third, other

The client side of the session constantly sends a heartbeat request to its server to keep the session active, preventing the server side of the session from receiving no data from the client for the "timeout" period, thus determining that the session is "out of date" and ending it.



Remarks:
This article describes the content described in "4.2, using ZooKeeper to implement a distributed application ' session '" in ZooKeeper Chapter 2 Getting to Grips with ZooKeeper and expansion.

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.