import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
public class ZKUtils2 {
private static ZooKeeper zkCli;
private static String address = "192.168.79.3:2181/testConf";
private static DefaultWatch2 watch = new DefaultWatch2();
private static CountDownLatch init = new CountDownLatch(1);
public static ZooKeeper getZkCli(){
try {
zkCli = new ZooKeeper(address,2000,watch);
watch.setCountDownLatch(init);
init.await();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return zkCli ;
}
}
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import java.util.concurrent.CountDownLatch;
public class DefaultWatch2 implements Watcher {
CountDownLatch countDownLatch ;
public void setCountDownLatch(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.toString());
switch (watchedEvent.getState()) {
case Unknown:
break;
case Disconnected:
break;
case NoSyncConnected:
break;
case SyncConnected:
countDownLatch.countDown();
System.out.println("connected!!!!!!!!");
break;
case AuthFailed:
break;
case ConnectedReadOnly:
break;
case SaslAuthenticated:
break;
case Expired:
break;
}
}
}
import com.zzh.zookeeper.config.MyConf;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
public class WatchCallBack2 implements Watcher, AsyncCallback.StatCallback, AsyncCallback.DataCallback {
ZooKeeper zkCli;
MyConf conf;
CountDownLatch countDownLatch = new CountDownLatch(1);
public ZooKeeper getZkCli() {
return zkCli;
}
public void setZkCli(ZooKeeper zkCli) {
this.zkCli = zkCli;
}
public MyConf getConf() {
return conf;
}
public void setConf(MyConf conf) {
this.conf = conf;
}
public void watchConf() {
zkCli.exists("/AppConf", this, this, "exists???");
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
if(data != null ){
String config = new String(data);
conf.setConf(config);
countDownLatch.countDown();
}
}
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
if (stat != null){
zkCli.getData("/AppConf",this,this,"数据存在吗");
}else {
System.out.println(path+"配置文件丢失");
}
}
@Override
public void process(WatchedEvent watchedEvent) {
switch (watchedEvent.getType()) {
case None:
break;
case NodeCreated:
zkCli.getData("/AppConf",this,this,"文件创建了");
break;
case NodeDeleted:
conf.setConf("");
countDownLatch = new CountDownLatch(1);
break;
case NodeDataChanged:
zkCli.getData("/AppConf",this,this,"文件修改了");
break;
case NodeChildrenChanged:
break;
}
}
}
import com.zzh.zookeeper.config.MyConf;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestConfig2 {
ZooKeeper zkCli;
@Before
public void getZK() {
zkCli = ZKUtils2.getZk();
}
@After
public void closeZK(){
try {
zkCli.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void getConf2() throws InterruptedException {
WatchCallBack2 watchCallBack = new WatchCallBack2();
MyConf myConf = new MyConf();
watchCallBack.setZkCli(zkCli);
watchCallBack.setConf(myConf);
watchCallBack.watchConf();
if(myConf.getConf()==null||myConf.getConf().equals("")){
watchCallBack.watchConf();
}else{
System.out.println(myConf.getConf());
}
ExecutorService service = Executors.newFixedThreadPool(3);
for (int i=0;i<3;i++)
service.submit(()->{
watchCallBack.watchConf();
if(myConf.getConf()==null||myConf.getConf().equals("")){
watchCallBack.watchConf();
}else{
System.out.println(Thread.currentThread().getName()+myConf.getConf());
}
});
service.shutdown();
new Thread(()->{
watchCallBack.watchConf();
if(myConf.getConf()==null||myConf.getConf().equals("")){
watchCallBack.watchConf();
}else{
System.out.println(Thread.currentThread().getName()+myConf.getConf());
}
},"t1").start();
new Thread(()->{
watchCallBack.watchConf();
if(myConf.getConf()==null||myConf.getConf().equals("")){
watchCallBack.watchConf();
}else{
System.out.println(Thread.currentThread().getName()+myConf.getConf());
}
},"t2").start();
new Thread(()->{
watchCallBack.watchConf();
if(myConf.getConf()==null||myConf.getConf().equals("")){
watchCallBack.watchConf();
}else{
System.out.println(Thread.currentThread().getName()+myConf.getConf());
}
},"t3").start();
Thread.sleep(10000);
}
}