Java客户端如何进行操作_java培训

发布时间:2020年07月30日作者:atguigu浏览次数:625

Java客户端操作

Maven工程和配置POM

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

  xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.atguigu.mall</groupId>

  <artifactId>zk0919</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

  <name>zk0919</name>

  <url>http://maven.apache.org</url>

  <properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

  <dependencies>

   <!– https://mvnrepository.com/artifact/com.101tec/zkclient –>

   <dependency>

     <groupId>com.101tec</groupId>

     <artifactId>zkclient</artifactId>

     <version>0.10</version>

   </dependency>

   <!– https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper –>

   <dependency>

     <groupId>org.apache.zookeeper</groupId>

     <artifactId>zookeeper</artifactId>

     <version>3.4.9</version>

   </dependency>

   <dependency>

     <groupId>log4j</groupId>

     <artifactId>log4j</artifactId>

     <version>1.2.17</version>

   </dependency>

   <dependency>

     <groupId>junit</groupId>

     <artifactId>junit</artifactId>

     <version>4.9</version>

     <scope>test</scope>

   </dependency>

  </dependencies>

</project>

程序代码

package com.atguigu.zk3;

import java.io.IOException;

import org.apache.log4j.Logger;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.KeeperException;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.Stat;

public class HelloZK

{

   /**

   * Logger for this class

   */

   private static final Logger logger = Logger.getLogger(HelloZK.class);

   private static final String CONNECTSTRING = “192.168.67.167:2181”;

   private static final String PATH = “/atguigu”;

   private static final int    SESSION_TIMEOUT = 50*1000;

   public ZooKeeper startZK() throws IOException

   {

       return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {

          @Override

          public void process(WatchedEvent event)

          {

          }

       });

   }

   public void stopZK(ZooKeeper zk) throws InterruptedException

   {

       if(zk != null)

       {

          zk.close();

       }

   }

   public void createZNode(ZooKeeper zk,String path,String nodeValue) throws KeeperException, InterruptedException

   {

       zk.create(path,nodeValue.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

   }

   public String getZNode(ZooKeeper zk,String path) throws KeeperException, InterruptedException

   {

       byte[] byteArray = zk.getData(path, false, new Stat());

       return new String(byteArray);

   }

   public static void main(String[] args) throws IOException, KeeperException, InterruptedException

   {

       HelloZK hello = new HelloZK();

       ZooKeeper zk = hello.startZK();

       Stat stat = zk.exists(PATH, false);

       if(stat == null)

       {

          hello.createZNode(zk, PATH, “zk1014”);

          String result = hello.getZNode(zk, PATH);

          System.out.println(“**********result: “+result);

       }else{

          System.out.println(“***********znode has already ok***********”);

       }

       hello.stopZK(zk);

   }

}

java培训


上一篇:
下一篇:
相关课程

java培训 大数据培训 前端培训 UI/UE设计培训

关于尚硅谷
教育理念
名师团队
学员心声
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
全国统一咨询电话:010-56253825
地址:北京市昌平区宏福科技园2号楼3层(北京校区)

深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

武汉市东湖高新开发区东湖网谷(武汉校区)

西安市雁塔区和发智能大厦B座3层(西安校区)