一个简单的 Zookeeper Watch 客户端
来源:互联网 发布:橡胶木 知乎 编辑:程序博客网 时间:2024/05/18 00:43
http://www.cnblogs.com/haippy/archive/2012/07/20/2600077.html
完整的程序:
Executor.java:
/**
* A simple example program to use DataMonitor to start and
* stop executables based on a znode. The program watches the
* specified znode and saves the data that corresponds to the
* znode in the filesystem. It also starts the specified program
* with the specified arguments when the znode exists and kills
* the program if the znode goes away.
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class Executor
implements Watcher, Runnable, DataMonitor.DataMonitorListener
{
String znode;
DataMonitor dm;
ZooKeeper zk;
String filename;
String exec[];
Process child;
public Executor(String hostPort, String znode, String filename,
String exec[]) throws KeeperException, IOException {
this.filename = filename;
this.exec = exec;
zk = new ZooKeeper(hostPort, 3000, this);
dm = new DataMonitor(zk, znode, null, this);
}
/**
* @param args
*/
public static void main(String[] args) {
if (args.length < 4) {
System.err
.println("USAGE: Executor hostPort znode filename program [args ...]");
System.exit(2);
}
String hostPort = args[0];
String znode = args[1];
String filename = args[2];
String exec[] = new String[args.length - 3];
System.arraycopy(args, 3, exec, 0, exec.length);
try {
new Executor(hostPort, znode, filename, exec).run();
} catch (Exception e) {
e.printStackTrace();
}
}
/***************************************************************************
* We do process any events ourselves, we just need to forward them on.
*
* @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent)
*/
public void process(WatchedEvent event) {
dm.process(event);
}
public void run() {
try {
synchronized (this) {
while (!dm.dead) {
wait();
}
}
} catch (InterruptedException e) {
}
}
public void closing(int rc) {
synchronized (this) {
notifyAll();
}
}
static class StreamWriter extends Thread {
OutputStream os;
InputStream is;
StreamWriter(InputStream is, OutputStream os) {
this.is = is;
this.os = os;
start();
}
public void run() {
byte b[] = new byte[80];
int rc;
try {
while ((rc = is.read(b)) > 0) {
os.write(b, 0, rc);
}
} catch (IOException e) {
}
}
}
public void exists(byte[] data) {
if (data == null) {
if (child != null) {
System.out.println("Killing process");
child.destroy();
try {
child.waitFor();
} catch (InterruptedException e) {
}
}
child = null;
} else {
if (child != null) {
System.out.println("Stopping child");
child.destroy();
try {
child.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(data);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println("Starting child");
child = Runtime.getRuntime().exec(exec);
new StreamWriter(child.getInputStream(), System.out);
new StreamWriter(child.getErrorStream(), System.err);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 一个简单的 Zookeeper Watch 客户端
- 使用Node.js实现一个简单的ZooKeeper客户端
- 使用Node.js实现一个简单的ZooKeeper客户端
- 【zookeeper】简单的客户端命令
- [ZooKeeper]ZooKeeper的Watch事件类型
- zookeeper watch
- iWatch开发:创建一个简单的Apple watch应用
- [zookeeper]联接中断,watch恢复,心跳和客户端超时
- zookeeper的watch(原生API)
- Zookeeper实现一个简单的配置管理
- Zookeeper CURD的一个例子,简单记忆
- zookeeper java客户端简单API
- Zookeeper开源客户端框架Curator的简单使用
- 简单介绍JS的watch
- zookeeper 客户端的实现
- zookeeper的python客户端
- Zookeeper的Java客户端
- ZooKeeper 客户端的使用
- sgu 131解题记录
- wince获得mac地址(需要连接网络或路由器)
- 颜色直方图特征代码
- Pots
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 一个简单的 Zookeeper Watch 客户端
- [2767]翻转排序 sdutOJ
- 线程传递参数
- GDB高级调试
- hdoj 1070 milk
- 解决拍照按钮为英文问题
- hdu 1828 Picture 线段树+离散化(其区间的个数)
- mininet+FlowVisor+OpenDayLight环境搭建及实验一
- CodeBlocks的常用快捷键