https://zookeeper.apache.org/doc/r3.5.2-alpha/javaExample.html
Executor
1 | public class Executor |
Declare class first, implements Zookeeper Watcher
, thread Runnable and DataMonitor’s inner interface Listener.
1 | try { |
Arguments sequence is
- host:port
- znode path
- process output file path
- process command array
1 | public Executor(String hostPort, String znode, String filename, |
In constuctor, Executor
use it’s Object self as a parameter.
ZooKeeper need a Watch
in third parameter, it triger
Executor’s implements to triger Monitor
again.
DataMonitor need a DataMonitorListener
to know what
operation need to notify when event happen.
1 | public void run() { |
It’s Runnable implements.
When Executor
be created and run in main, it check monitor’s status.
When first initiated, monitor dead should in false, main thread will wait for zookeeper client to wake it up.
1 | try { |
local process executed here, it’s output will be write into file path.
feedbackTarget.sh1
2
3
4
5
6
7
8
while true
do
echo "hello zookeeper" >&1
echo "hello error" >&2
sleep 3s
done
exit 0
192.168.3.11:2181 /key ./example/leon sh ./example/feedbackTarget.sh
DataMonitor
1 | public class DataMonitor implements Watcher, StatCallback |
implements Watcher
as a watcher, but not use as watcher.
implements StatCallback
, zookeeper client should callback this monitor when node status changed.
1 | public void process(WatchedEvent event) { |
Monitor feedback what it seen to the listener,
and register the next chainedWatcher if exitst.
1 | public void processResult(int rc, String path, Object ctx, Stat stat) { |
Put zookeeper client’s callback to executor.