You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2018/02/06 15:59:27 UTC
[2/2] aries-rsa git commit: Avoid connection refused exceptions
during tests when discovery client is faster than zookeeper server
Avoid connection refused exceptions during tests when discovery client is faster than zookeeper server
Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo
Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/389bae15
Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/389bae15
Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/389bae15
Branch: refs/heads/master
Commit: 389bae1517d421621cc96bb2c75f8219c82f3a73
Parents: 025516f
Author: Christian Schneider <cs...@adobe.com>
Authored: Tue Feb 6 16:59:12 2018 +0100
Committer: Christian Schneider <cs...@adobe.com>
Committed: Tue Feb 6 16:59:12 2018 +0100
----------------------------------------------------------------------
.../discovery/zookeeper/ZooKeeperDiscovery.java | 39 ++++++++++++++++----
1 file changed, 32 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/389bae15/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
----------------------------------------------------------------------
diff --git a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
index 584da35..13dadad 100644
--- a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
+++ b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
@@ -19,6 +19,7 @@
package org.apache.aries.rsa.discovery.zookeeper;
import java.io.IOException;
+import java.net.Socket;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
@@ -58,7 +59,7 @@ public class ZooKeeperDiscovery implements Watcher, ManagedService {
this.bctx = bctx;
}
- public synchronized void updated(Dictionary<String, ?> configuration) throws ConfigurationException {
+ public synchronized void updated(final Dictionary<String, ?> configuration) throws ConfigurationException {
LOG.debug("Received configuration update for Zookeeper Discovery: {}", configuration);
// make changes only if config actually changed, to prevent unnecessary ZooKeeper reconnections
if (!toMap(configuration).equals(toMap(curConfiguration))) {
@@ -66,13 +67,18 @@ public class ZooKeeperDiscovery implements Watcher, ManagedService {
curConfiguration = configuration;
// config is null if it doesn't exist, is being deleted or has not yet been loaded
// in which case we just stop running
- if (!closed && configuration != null) {
- try {
- createZookeeper(configuration);
- } catch (IOException e) {
- throw new ConfigurationException(null, "Error starting zookeeper client", e);
- }
+ if (closed || configuration == null) {
+ return;
}
+ new Thread(new Runnable() {
+ public void run() {
+ try {
+ createZookeeper(configuration);
+ } catch (IOException e) {
+ LOG.error("Error starting zookeeper client", e);
+ }
+ }
+ }).start();
}
}
@@ -155,8 +161,27 @@ public class ZooKeeperDiscovery implements Watcher, ManagedService {
String host = (String)getWithDefault(config, "zookeeper.host", "localhost");
String port = (String)getWithDefault(config, "zookeeper.port", "2181");
int timeout = Integer.parseInt((String)getWithDefault(config, "zookeeper.timeout", "3000"));
+ waitPort(host, Integer.parseInt(port));
zkClient = createZooKeeper(host, port, timeout);
}
+
+ private void waitPort(String host, int port) {
+ long start = System.currentTimeMillis();
+ while (System.currentTimeMillis() - start < 2000) {
+ try (Socket socket = new Socket(host, port)) {
+ return;
+ } catch (IOException e) {
+ safeSleep();
+ }
+ }
+ }
+
+ private void safeSleep() {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e1) {
+ }
+ }
public Object getWithDefault(Dictionary<String, ?> config, String key, Object defaultValue) {
Object value = config.get(key);