Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563485
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: Java

2015-06-28 22:44:48

本片文章是 [java,zookeeper] ZooKeeper 触发事件__Watcher 专题 的子链接
测试代码如下

点击(此处)折叠或打开

  1. package org.apache.zookeeper.demo;

  2. import java.util.concurrent.CountDownLatch ;

  3. import org.apache.zookeeper.*;
  4. import org.apache.zookeeper.Watcher.Event.* ;

  5. import java.util.List ;

  6. /**
  7.  * Created by root on 6/28/15.
  8.  *
  9.  * step descriptions :
  10.  * 1. create temporary folder paths
  11.  * /test/
  12.  * |--- t1
  13.  * |--- t2
  14.  * |--- t3
  15.  * |--- t4
  16.  * |--- t5
  17.  * |--- t6
  18.  * |--- t7
  19.  *
  20.  * 2. monitor path /test/ by method exists(String path , Watcher )
  21.  *
  22.  * 3. getChildren by /test/ get all its sub-path names
  23.  * 3.1 set all its sub-path monitored by exists ( sub-path , watcher )
  24.  * implements an invoke of the watcher that if deleteNode event happens
  25.  * output the deleted path name on the console-screen
  26.  *
  27.  * 3.2 another method to add invoker of delete sub-path node is
  28.  * add deleteNode event methods into the step 2. 's Watcher
  29.  *
  30.  * 4. after add the corresponding methods into the invoker ,
  31.  * we try to delete a znode /test/t4 and see what happen
  32.  *
  33.  * 5. continue testing : if we add a znode on the monitored sub-path
  34.  * what will happen ?
  35.  *
  36.  * 6. getChildren with passing path as '/test/' , and get sub-path name
  37.  * and create a new Watcher with NodeChildChanged() and get the list of
  38.  * sub-child-node ,and get the tip from the stack-over-flow that the
  39.  * CreateNode Event only valid for exists () method .
  40.  *
  41.  * So , i have to run a cycle to add every , sorry , i have no idea yet
  42.  *
  43.  * At last , to increase the flexibility of Watcher , I use lots of
  44.  * different kinds of class to implement Watcher interface.
  45.  *
  46.  */

  47. public class NodeChildrenChanged_Event_Tester
  48. {
  49.     ZooKeeper zk = null ;

  50.     Conn_Watcher_Impl ConnWatcher = null ;

  51.     final String CONNECTION_STRING = "127.0.0.1:2181" ;

  52.     final int SESSION_TIMER = 20000 ;


  53.     public NodeChildrenChanged_Event_Tester()
  54.     {
  55.         try
  56.         {
  57.              this.ConnWatcher = new Conn_Watcher_Impl() ;

  58.             this.zk = new ZooKeeper (this.CONNECTION_STRING , this.SESSION_TIMER , this.ConnWatcher ) ;

  59.         }
  60.         catch ( Exception ex )
  61.         {
  62.             ex.printStackTrace();
  63.         }
  64.     }

  65.     /**
  66.      * method createPath
  67.      * @param path : name of path which going to be created
  68.      * @param data_info : data_info is used to store path's information on the znode
  69.      * @param watcher : if we want to add a monitor to the new created path ,pass an instance of Watcher
  70.      * if do not need add one , pass null is Ok
  71.      * */
  72.     public void createPath ( String path , String data_info , Watcher watcher )
  73.     {
  74.         try
  75.         {
  76.             // in case of Exception , we delete the path first
  77.             // note : -1 means whatever the version is delete them all ,
  78.             // cause ZooKeeper distinguish same path name' node by its version id

  79.             this.zk.create( path , data_info.getBytes() , ZooDefs.Ids.OPEN_ACL_UNSAFE , CreateMode.PERSISTENT) ;

  80.             // after create a znode , we add an Watcher to monitor it
  81.             if ( watcher != null )
  82.             this.zk.exists( path , watcher ) ;

  83.         }
  84.         catch ( Exception ex )
  85.         {
  86.             ex.printStackTrace();
  87.         }
  88.     }

  89.     /**
  90.      * method : showChildList
  91.      * this method is used to show all path's children name on ZooKeeper server
  92.      * if the path does not have any child , print out message
  93.      *
  94.      * @param path : path name
  95.      * */
  96.     public void showChildList ( String path )
  97.     {
  98.         try
  99.         {
  100.             List<String> childList = this.zk.getChildren(path , false ) ;

  101.             if ( childList.isEmpty())
  102.             {
  103.                 System.out.println("path : "+path +" on ZooKeeper is null , do not have sub-path ") ;
  104.             }
  105.             else
  106.             {
  107.                 for ( String s : childList )
  108.                 {
  109.                     System.out.println (path + "/"+ s ) ;
  110.                 }
  111.             }

  112.         }
  113.         catch (Exception ex )
  114.         {
  115.             ex.printStackTrace();
  116.         }
  117.     }

  118.     public static void main ( String [] args ) throws Exception
  119.     {
  120.         String main_path = "/test" ;

  121.         NodeChildrenChanged_Event_Tester tester = new NodeChildrenChanged_Event_Tester() ;

  122.         MasterWatcher masterWatcher = new MasterWatcher( tester , main_path ) ;

  123.         // create main_path node
  124.         tester.createPath (main_path , "main_path_data_content" , masterWatcher ) ;



  125.         // create sub-path
  126.         for ( int i = 1 ; i <= 3 ; i++ )
  127.         {
  128.             String sub_path = main_path+"/t"+i ;
  129.             tester.createPath( sub_path , "sub_path"+i , null ) ; // i can add sub-path=monitors here
  130.         }

  131.         // here we gonna to test showChildList method
  132.         tester.showChildList(main_path );


  133.         Thread.sleep(600000) ;
  134.     }
  135. }

  136. class Conn_Watcher_Impl implements Watcher
  137. {
  138.     public void process ( WatchedEvent watchedEvent )
  139.     {
  140.         KeeperState keeperStat = watchedEvent.getState() ;

  141.         if ( keeperStat == KeeperState.SyncConnected )
  142.         {
  143.             System.out.println ("You have succeed in connecting to ZooKeeper Server ") ;

  144.             // we use the CountDownLatch is comming from the NodeChildrenChanged_Event_Tester
  145.             // it means until we execute this process , the Main thread could exit
  146.             // use it in case of Main thread exiting earlier than this process method ,
  147.             // if so , we could not see this message on screen
  148.         }


  149.     }
  150. }

  151. /**
  152.  * MasterWatcher this is class implements interface Watcher
  153.  * its process method mainly used to monitor the main path's events
  154.  * such as :
  155.  * 1. node deletion
  156.  * 2. child node changed
  157.  * 3. node created
  158.  * 4. node data content be updated
  159.  *
  160.  * and write different events , do different actions :
  161.  *
  162.  * 1. print out messages on console
  163.  * 2. child node changed this situation is a little complex:
  164.  * 2.1 it may be add a node on the child-path
  165.  * 2.2 it may be delete a n ode on the child-path
  166.  * 2.3 it may be the sub-path nodes' data content is updated
  167.  * 3. print out messages on console
  168.  * 4. print out messages on console
  169.  *
  170.  * at beginning , what is important is that : distinguish different events
  171.  * so , all kinds of event's actions are simply output messages
  172.  *
  173.  * after we get a good master of different kind events , we write the event's
  174.  * method in detail
  175.  * */
  176. class MasterWatcher implements Watcher
  177. {
  178.     // cause we need to show the path names in this class
  179.     // so we need the handler of the ZooKeeper

  180.     // so we need the ZooKeeper object being sent into this class as
  181.     // constructor's parameter

  182.     NodeChildrenChanged_Event_Tester zk_master = null ;

  183.     String main_path = null ;

  184.     public MasterWatcher ( NodeChildrenChanged_Event_Tester zk_master , String master_path )
  185.     {
  186.         this.zk_master = zk_master ;
  187.         this.main_path = master_path ;
  188.     }

  189.     public void process ( WatchedEvent watchedEvent)
  190.     {
  191.         KeeperState keeperState = watchedEvent.getState() ;
  192.         EventType pathEvents = watchedEvent.getType() ;

  193.         if ( keeperState == KeeperState.Disconnected || keeperState == KeeperState.Expired )
  194.         {
  195.             System.out.println ("connection problems ") ;
  196.         }
  197.         else
  198.         {
  199.              if ( pathEvents == EventType.NodeCreated)
  200.              {
  201.                  System.out.println ("path node already be created") ;
  202.              }
  203.              else if ( pathEvents == EventType.NodeDataChanged )
  204.              {
  205.                  System.out.println ("node data been updated ") ;
  206.              }
  207.             else if ( pathEvents == EventType.NodeDeleted )
  208.              {
  209.                  System.out.println ("node is deleted ") ;
  210.              }
  211.             else if ( pathEvents == EventType.NodeChildrenChanged )
  212.              {
  213.                  System.out.println ("node child-path has been changed ") ;
  214.              }
  215.             else
  216.              {
  217.                  System.out.println ("here is something else happen") ;
  218.              }
  219.         }

  220.     }
  221. }
end
阅读(1339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~