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

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

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: Java

2015-06-28 16:29:47

1. Watcher1
自身实现 Watcher 接口,传入 this

点击(此处)折叠或打开

  1. package org.apache.zookeeper.demo;

  2. import java.util.concurrent.CountDownLatch ;


  3. import org.apache.zookeeper.WatchedEvent;
  4. import org.apache.zookeeper.Watcher;
  5. import org.apache.zookeeper.Watcher.Event.* ;
  6. import org.apache.zookeeper.ZooKeeper ;

  7. /**
  8.  * Created by root on 6/28/15.
  9.  */
  10. public class SyncConnect_Event_Tester implements Watcher // Watcher 1
  11. {
  12.     ZooKeeper zk = null ;
  13.  
  14.     final String CONNECTION_STRING = "127.0.0.1:2181" ;

  15.     final int SESSSION_TIMER = 2000 ;

  16.     private CountDownLatch connectedSemaphore = new CountDownLatch( 1 ) ;

  17.     public SyncConnect_Event_Tester ()
  18.     {
  19.         try
  20.         {

  21.             zk = new ZooKeeper ( CONNECTION_STRING , SESSSION_TIMER , this ) ;  
  22.             this.connectedSemaphore.await () ;
  23.         }
  24.         catch ( Exception e )
  25.         {
  26.             e.printStackTrace();
  27.         }
  28.     }

  29.     public void process ( WatchedEvent we )
  30.     {
  31.         try
  32.         {
  33.             Event.KeeperState keeperState = we.getState() ;

  34.             System.out.println (we.getState().toString()) ;

  35.             if (keeperState == Event.KeeperState.SyncConnected )
  36.             {
  37.                 System.out.println ("[Watche1] success connect to ZooKeeper Server ") ;

  38.                 connectedSemaphore.countDown();
  39.             }
  40.         }
  41.         catch ( Exception ex )
  42.         {

  43.         }
  44.     }


  45.     public static void main ( String [] args )
  46.     {
  47.         SyncConnect_Event_Tester tester = new SyncConnect_Event_Tester() ;
  48.     }
  49. }


2. Watcher2
另创建一个类实现 Watcher, 传入该类的实例对象

点击(此处)折叠或打开

  1. package org.apache.zookeeper.demo;

  2. import java.util.concurrent.CountDownLatch ;


  3. import org.apache.zookeeper.WatchedEvent;
  4. import org.apache.zookeeper.Watcher;
  5. import org.apache.zookeeper.Watcher.Event.* ;
  6. import org.apache.zookeeper.ZooKeeper ;

  7. /**
  8.  * Created by root on 6/28/15.
  9.  */
  10. public class SyncConnect_Event_Tester implements Watcher // Watcher 1
  11. {
  12.     ZooKeeper zk = null ;

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

  14.     final int SESSSION_TIMER = 2000 ;

  15.     private CountDownLatch connectedSemaphore = new CountDownLatch( 1 ) ;

  16.     public SyncConnect_Event_Tester ()
  17.     {
  18.         try
  19.         {

  20.             WatcherLoader w = new WatcherLoader( this.connectedSemaphore ) ;

  21.             zk = new ZooKeeper (CONNECTION_STRING , SESSSION_TIMER , w ) ; // this is for the Watcher 2
  22.             this.connectedSemaphore.await () ;
  23.          
  24.         }
  25.         catch ( Exception e )
  26.         {
  27.             e.printStackTrace();
  28.         }
  29.     }

  30.     public static void main ( String [] args )
  31.     {
  32.         SyncConnect_Event_Tester tester = new SyncConnect_Event_Tester() ;
  33.     }
  34. }

  35. class WatcherLoader implements Watcher
  36. {
  37.     private CountDownLatch countDownLatch ;

  38.     public WatcherLoader ( CountDownLatch countDownLatch )
  39.     {
  40.         this.countDownLatch = countDownLatch ;
  41.     }

  42.     public void process ( WatchedEvent e) {
  43.         try
  44.         {
  45.             Event.KeeperState keeperState = e.getState() ;

  46.             if (keeperState == Event.KeeperState.SyncConnected )
  47.             {
  48.                 System.out.println ("[Watche2] success connect to ZooKeeper Server ") ;
  49.                 countDownLatch.countDown();


  50.             }
  51.         }
  52.         catch ( Exception ex )
  53.         {
  54.             // ignore
  55.         }
  56.     }
  57. }

3. Watcher3


直接在 ZooKeeper 构造方法的 Watcher 参数中创建一个 Watcher 接口实例,并在 Watcher 内部编写 process 方法。

点击(此处)折叠或打开

  1. package org.apache.zookeeper.demo;

  2. import java.util.concurrent.CountDownLatch ;


  3. import org.apache.zookeeper.WatchedEvent;
  4. import org.apache.zookeeper.Watcher;
  5. import org.apache.zookeeper.Watcher.Event.* ;
  6. import org.apache.zookeeper.ZooKeeper ;

  7. /**
  8.  * Created by root on 6/28/15.
  9.  */

  10. public class SyncConnect_Event_Tester implements Watcher // Watcher 1
  11. {
  12.     ZooKeeper zk = null ;

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

  14.     final int SESSSION_TIMER = 2000 ;

  15.     private CountDownLatch connectedSemaphore = new CountDownLatch( 1 ) ;

  16.     public SyncConnect_Event_Tester ()
  17.     {
  18.         try
  19.         {
  20.             // this is for the Watcher 3
  21.             zk = new ZooKeeper ( CONNECTION_STRING , SESSSION_TIMER , new Watcher ()
  22.             {
  23.                 public void process ( WatchedEvent e )
  24.                 {
  25.                     try
  26.                     {
  27.                         Event.KeeperState keeperState = e.getState() ;

  28.                         if (keeperState == Event.KeeperState.SyncConnected )
  29.                         {
  30.                             System.out.println ("[Watche3] success connect to ZooKeeper Server ") ;
  31.                             connectedSemaphore.countDown();
  32.                         }
  33.                     }
  34.                     catch ( Exception ex )
  35.                     {
  36.                         // ignored
  37.                     }
  38.                 }
  39.             }) ;
  40.             this.connectedSemaphore.await () ;

  41.         }
  42.         catch ( Exception e )
  43.         {
  44.             e.printStackTrace();
  45.         }
  46.     }


  47.     public static void main ( String [] args )
  48.     {
  49.         SyncConnect_Event_Tester tester = new SyncConnect_Event_Tester() ;
  50.     }
  51. }


4. Watcher4

类似于第三种方法,创建 Watcher 实例,需要的时候将该实例传入到参数中.

点击(此处)折叠或打开

  1. package org.apache.zookeeper.demo;

  2. import java.util.concurrent.CountDownLatch ;


  3. import org.apache.zookeeper.WatchedEvent;
  4. import org.apache.zookeeper.Watcher;
  5. import org.apache.zookeeper.Watcher.Event.* ;
  6. import org.apache.zookeeper.ZooKeeper ;

  7. /**
  8.  * Created by root on 6/28/15.
  9.  */
  10. public class SyncConnect_Event_Tester implements Watcher // Watcher 1
  11. {
  12.     ZooKeeper zk = null ;

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

  14.     final int SESSSION_TIMER = 2000 ;

  15.     Watcher watcher ;

  16.     private CountDownLatch connectedSemaphore = new CountDownLatch( 1 ) ;

  17.     public SyncConnect_Event_Tester ()
  18.     {
  19.         try
  20.         {
  21.             // this is for the Watcher 4
  22.             this.watcher = new Watcher () {
  23.                 public void process(WatchedEvent e) {
  24.                     try
  25.                     {
  26.                         Event.KeeperState keeperState = e.getState() ;

  27.                         if (keeperState == Event.KeeperState.SyncConnected )
  28.                         {
  29.                             System.out.println ("[Watche4] success connect to ZooKeeper Server ") ;
  30.                             connectedSemaphore.countDown();
  31.                         }
  32.                     }
  33.                     catch ( Exception ex )
  34.                     {
  35.                         // ignored
  36.                     }
  37.                 }
  38.             } ;


  39.             zk = new ZooKeeper (CONNECTION_STRING, SESSSION_TIMER , this.watcher) ;

  40.             this.connectedSemaphore.await () ;


  41.         }
  42.         catch ( Exception e )
  43.         {
  44.             e.printStackTrace();
  45.         }
  46.     }

  47.     public static void main ( String [] args )
  48.     {
  49.         SyncConnect_Event_Tester tester = new SyncConnect_Event_Tester() ;
  50.     }
  51. }


end






阅读(2627) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

夏目玲子2015-06-28 17:07:48

正确的运行结果显示 Watcher1/2/3/4 如下:


[Watche4] success connect to ZooKeeper Server 

Process finished with exit code 0

其中,CountDownLatch  对象的作用是用作让主线程等待,直到回调函数的方法执行结束才能够让主线程退出。
如果不使用它,主线程跑完之后直接就退出了,而不会等待服务器连接成功,显示连接成功的信息之后才退出,
即,不使用无法看到 process 方法中的信息。