-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #790 from chickenlj:bugfix_zookeeper_block
Optimize zookeeper block logic on app startup
- Loading branch information
Showing
8 changed files
with
155 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...ookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkClientWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.alibaba.dubbo.remoting.zookeeper.zkclient; | ||
|
||
import com.alibaba.dubbo.common.concurrent.ListenableFutureTask; | ||
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.common.utils.Assert; | ||
|
||
import org.I0Itec.zkclient.IZkChildListener; | ||
import org.I0Itec.zkclient.IZkStateListener; | ||
import org.I0Itec.zkclient.ZkClient; | ||
import org.apache.zookeeper.Watcher.Event.KeeperState; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* 连接超时后,能自动监听连接状态的zkclient包装类 | ||
* 也为和curator在使用上总体保持一致 | ||
* @author ken.lj | ||
* @date 2017/10/29 | ||
*/ | ||
public class ZkClientWrapper { | ||
Logger logger = LoggerFactory.getLogger(ZkClientWrapper.class); | ||
|
||
private long timeout; | ||
private ZkClient client; | ||
private volatile KeeperState state; | ||
private ListenableFutureTask<ZkClient> listenableFutureTask; | ||
private volatile boolean started = false; | ||
|
||
|
||
public ZkClientWrapper(final String serverAddr, long timeout) { | ||
this.timeout = timeout; | ||
listenableFutureTask = ListenableFutureTask.create(new Callable<ZkClient>() { | ||
@Override | ||
public ZkClient call() throws Exception { | ||
return new ZkClient(serverAddr, Integer.MAX_VALUE); | ||
} | ||
}); | ||
} | ||
|
||
public void start() { | ||
if (!started) { | ||
Thread connectThread = new Thread(listenableFutureTask); | ||
connectThread.setName("DubboZkclientConnector"); | ||
connectThread.setDaemon(true); | ||
connectThread.start(); | ||
try { | ||
client = listenableFutureTask.get(timeout, TimeUnit.MILLISECONDS); | ||
} catch (Throwable t) { | ||
logger.error("Timeout! zookeeper server can not be connected in : " + timeout + "ms!", t); | ||
} | ||
started = true; | ||
} else { | ||
logger.warn("Zkclient has already been started!"); | ||
} | ||
} | ||
|
||
public void addListener(final IZkStateListener listener) { | ||
listenableFutureTask.addListener(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
client = listenableFutureTask.get(); | ||
client.subscribeStateChanges(listener); | ||
} catch (InterruptedException e) { | ||
logger.warn(Thread.currentThread().getName() + " was interrupted unexpectedly, which may cause unpredictable exception!"); | ||
} catch (ExecutionException e) { | ||
logger.error("Got an exception when trying to create zkclient instance, can not connect to zookeeper server, please check!", e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public boolean isConnected() { | ||
return client != null && state == KeeperState.SyncConnected; | ||
} | ||
|
||
public void createPersistent(String path) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
client.createPersistent(path, true); | ||
} | ||
|
||
public void createEphemeral(String path) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
client.createEphemeral(path); | ||
} | ||
|
||
public void delete(String path) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
client.delete(path); | ||
} | ||
|
||
public List<String> getChildren(String path) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
return client.getChildren(path); | ||
} | ||
|
||
public boolean exists(String path) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
return client.exists(path); | ||
} | ||
|
||
public void close() { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
client.close(); | ||
} | ||
|
||
public List<String> subscribeChildChanges(String path, final IZkChildListener listener) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
return client.subscribeChildChanges(path, listener); | ||
} | ||
|
||
public void unsubscribeChildChanges(String path, IZkChildListener listener) { | ||
Assert.notNull(client, new IllegalStateException("Zookeeper is not connected yet!")); | ||
client.unsubscribeChildChanges(path, listener); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters