编写功能类:

 1 package com.hpay.FileToZkUtil;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.util.concurrent.CountDownLatch;
 8 
 9 import org.apache.log4j.Logger;
10 import org.apache.zookeeper.KeeperException;
11 import org.apache.zookeeper.WatchedEvent;
12 import org.apache.zookeeper.Watcher;
13 import org.apache.zookeeper.Watcher.Event.KeeperState;
14 import org.apache.zookeeper.ZooKeeper;
15 import org.apache.zookeeper.data.Stat;
16 
17 public class FileToZkUtil 
18 {
19     private static final Logger logger = Logger.getLogger(FileToZkUtil.class);
20     
21     private static CountDownLatch connectedSemaphore = new CountDownLatch( 1 );   
22     
23     /**
24      * 
25      * @param zkUrl      zookeeper地址
26      * @param zkNode     zookeeper节点
27      * @param filePath   文件路径
28      */
29     @SuppressWarnings("unused")
30     public static void setFileToZkNode(String zkUrl, String zkNode, String filePath) {
31 
32         Watcher watch = new Watcher() {
33             public void process(WatchedEvent event) {
34                 // System.out.println(event.getPath());
35                 logger.info(">>>>> " + event.getPath() +">>>>>" + event.getType());
36                 if (KeeperState.SyncConnected == event.getState()) {
37                     connectedSemaphore.countDown();
38                 } 
39             }
40         };
41         ZooKeeper zk;
42         try {
43             //读取目标文件
44             byte[] data = readFile(filePath);
45             if (data != null) {
46                 zk = new ZooKeeper(zkUrl, 3000, watch);
47                 connectedSemaphore.await();  
48                 logger.info("zk连接创建成功:" + zkUrl);
49                 //测试是否存在目标节点
50                 Stat stat = zk.exists(zkNode, true);
51                 logger.info("目标节点状态:" + stat);
52                 //设置目标节点数据
53                 zk.setData(zkNode, data, -1);
54             } else {
55                 logger.info("获取" + filePath + "数据失败!");
56             }
57 
58         } catch (IOException e) {
59             e.printStackTrace();
60             logger.error("绑定zk节点出错:" + e.getMessage());
61         } catch (KeeperException e) {
62             e.printStackTrace();
63             logger.error("绑定zk节点出错:" + e.getMessage());
64         } catch (InterruptedException e) {
65             e.printStackTrace();
66             logger.error("绑定zk节点出错:" + e.getMessage());
67         }
68     }
69 
70     //读取文件
71     private static byte[] readFile(String filePath) {
72         File file = new File(filePath);
73         byte b[] = null;
74         if (file.isFile() && file.exists()) {
75             FileInputStream fis;
76             try {
77                 fis = new FileInputStream(file);
78                 int count = fis.available();
79                 int readCount = 0;
80                 b = new byte[count];
81                 while (readCount < count) {
82                     readCount += fis.read(b, readCount, count - readCount);
83                 }
84                 fis.close();
85             } catch (FileNotFoundException e) {
86                 e.printStackTrace();
87                 logger.error("读取文件出错:" + e.getMessage());
88             } catch (IOException e) {
89                 e.printStackTrace();
90                 logger.error("读取文件出错:" + e.getMessage());
91             }
92         }
93         return b;
94     }
95 }
View Code

相关文章:

  • 2021-07-04
  • 2021-09-27
  • 2021-07-06
  • 2021-09-02
  • 2021-12-02
  • 2021-09-29
猜你喜欢
  • 2021-10-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2021-12-08
  • 2022-12-23
  • 2021-07-09
相关资源
相似解决方案