• curator zookeeper监控。SpiderWatcher


    import java.util.List;
    
    import org.apache.curator.RetryPolicy;
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    
    public class SpiderWatcher implements Watcher {
        CuratorFramework client = null;
        List<String> list1 = null;
    
        public SpiderWatcher() {
            String connectString = "192.168.14.139:2181";
            int sessionTimeoutMs = 30000;
            int connectionTimeoutMs = 30000;
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(5000, 3);
            client = CuratorFrameworkFactory.newClient(connectString, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
            client.start();
            try {
                list1 = client.getChildren().usingWatcher(this).forPath("/dddd");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void process(WatchedEvent arg0) {
            try {
                List<String> list2 = client.getChildren().usingWatcher(this).forPath("/dddd");
                for (String znode : list1) {
                    if (!list2.contains(znode)) {
                        System.out.println("删除");
                    }
                }
                for (String znode : list2) {
                    if (!list1.contains(znode)) {
                        System.out.println("新增");
                    }
                }
                list1 = list2;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void stayAlive() {
            for (;;) {
            }
        }
    
        public static void main(String[] args) throws Exception {
            SpiderWatcher spiderWatcher = new SpiderWatcher();
            spiderWatcher.stayAlive();
        }
    }
  • 相关阅读:
    记账本第二天
    记账本第一天
    HDU 1811
    Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
    KMP超强模板贴一份
    2014辽宁省赛总结
    Codeforces Round #244 (Div. 2)
    CodeForces 383D Antimatter
    NEU 1351 Goagain and xiaodao's romantic story I
    UVA 10692 Huge Mod
  • 原文地址:https://www.cnblogs.com/sysout/p/5463498.html
Copyright © 2020-2023  润新知