编写功能类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
编写main方法类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class MianClass { 2 public static void main(String[] args) { 3 // FileToZkUtil.setFileToZkNode("10.48.172.101:2161", "/test", "E:/test/zookeeper/t.txt"); 4 FileToZkUtil.setFileToZkNode(args[0], args[1], args[2]); 5 } 6 }
在pom文件中配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.apache.maven.plugins</groupId> 5 <artifactId>maven-assembly-plugin</artifactId> 6 <configuration> 7 <archive> 8 <manifest> 9 <mainClass>com.hpay.FileToZkUtil.MianClass</mainClass> 10 </manifest> 11 </archive> 12 <descriptorRefs> 13 <descriptorRef>jar-with-dependencies</descriptorRef> 14 </descriptorRefs> 15 </configuration> 16 </plugin> 17 </plugins> 18 </build>
打包方式:
1.进入到项目所在目录,例如:
cd D:workspace iskCompensateFileToZkUtil
2.运行命令
mvn assembly:assembly
3.在项目的target目录中可以看到如下文件
说明打包完成.
运行方式:
1.在cmd命令窗口进入target目录
cd cd D:workspace iskCompensateFileToZkUtil arget
2.输入
java -jar FileToZkUtil-0.0.1-SNAPSHOT-jar-with-dependencies.jar 10.48.172.101:2161 /test E:/test/zookeeper/t.txt
即可执行该jar,其中 10.48.172.101:2161 /test E:/test/zookeeper/t.txt 为main方法的三个参数
需要注意的事项:
1.如果项目中依赖了其他maven项目,需要先将依赖的项目编译.
选中依赖的项目执行 maven install
2.需要将依赖的jar,如该项目中的zookeeper的pom文件中scope属性修改为compile