Uploaded image for project: 'Apache Curator'
  1. Apache Curator
  2. CURATOR-587

Use ZooKeeper 3.7+ ZooKeeperServerEmbedded in order to start TestingServer

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 5.4.0
    • Tests
    • None

    Description

      with 3.7.0 ZooKeeper provides an official API to run a ZooKeeper node.
      We should use that API in order to launch ZooKeeper, this way we won't have any compatibility issues.
      In fact now (5.1) we are starting the ZooKeeper node using internal ZK classes, that are subject to change in the future.
      The ZooKeeperServerEmbedded API is designed to be forward compatible.
      It is a very skinny API, and it basically reproduces the usage for the users, that is to provide a configuration file and a base directory.
      It also provides a way to prevent ZooKeeper to exit the JVM and to shutdown gracefully the server.

       
          @TempDir
          Path tmpDir;
         
          @Test
           public void hello() throws Exception {
               Properties configuration = new Properties();
               configuration.setProperty("clientPort", "2181");
               try (ZooKeeperServerEmbedded embedded = ZooKeeperServerEmbedded
                       .builder()
                       .exitHandler(ExitHandler.LOG_ONLY)
                       .baseDir(tmpDir)
                       .configuration(configuration)
                       .build();) {
                   embedded.start();
                   CountDownLatch l = new CountDownLatch(1);
                   try (ZooKeeper zk = new ZooKeeper("localhost:2181", 40000, new Watcher() {
                       @Override
                       public void process(WatchedEvent event) {
                           System.out.println("event "+event)                            ;
                           l.countDown();
                       }                 
                   }
                   )) {
                       l.await();
                       System.out.println("WHOAMI"+ zk.whoAmI());
                       zk.create("/foo", "foo".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                   }
               }
           
           } 

      Attachments

        Activity

          People

            tison Zili Chen
            eolivelli Enrico Olivelli
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1.5h
                1.5h