Skip to content

Commit

Permalink
Set the host automatically when cluster-host isn't defined explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
manish-panwar authored and rworsnop committed Aug 4, 2016
1 parent 3abf97d commit 7060011
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/main/java/io/vertxbeans/VertxBeansBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import io.vertx.core.AsyncResultHandler;
import io.vertx.core.VertxOptions;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.spi.cluster.ClusterManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand All @@ -19,6 +26,9 @@
* Created by Rob Worsnop on 9/5/15.
*/
public class VertxBeansBase {

private static final Logger log = LoggerFactory.getLogger(VertxBeansBase.class);

@Autowired(required = false)
private ClusterManager clusterManager;

Expand All @@ -39,40 +49,63 @@ protected VertxOptions vertxOptions() {
setParameter(env.getProperty("vertx.max-worker-execution-time", Long.class), options::setMaxWorkerExecuteTime);
setParameter(env.getProperty("vertx.blocked-thread-check-interval", Long.class), options::setBlockedThreadCheckInterval);
setParameter(env.getProperty("vertx.internal-blocking-pool-size", Integer.class), options::setInternalBlockingPoolSize);
options.setHAEnabled(env.getProperty("vertx.ha-enabled",Boolean.class, false));
options.setHAEnabled(env.getProperty("vertx.ha-enabled", Boolean.class, false));
setParameter(env.getProperty("vertx.ha-group", ""), options::setHAGroup);
setParameter(env.getProperty("vertx.quorum-size", Integer.class), options::setQuorumSize);
options.setClustered(env.getProperty("vertx.clustered", Boolean.class, false));
options.setClusterHost(env.getProperty("vertx.cluster-host", "localhost"));
options.setClusterHost(env.getProperty("vertx.cluster-host", getDefaultAddress()));
setParameter(env.getProperty("vertx.cluster-port", Integer.class), options::setClusterPort);
setParameter(env.getProperty("vertx.cluster-ping-interval", Long.class), options::setClusterPingInterval);
setParameter(env.getProperty("vertx.cluster-ping-reply-interval", Long.class), options::setClusterPingReplyInterval);
setParameter(clusterManager, options::setClusterManager);
setParameter(metricsOptions, options::setMetricsOptions);

return options;
}

protected <T> T clusteredVertx(Consumer<AsyncResultHandler<T>> consumer) throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<T> future = new CompletableFuture<>();
clusteredVertx(consumer, ar -> {
if (ar.succeeded()){
if (ar.succeeded()) {
future.complete(ar.result());
} else{
} else {
future.completeExceptionally(ar.cause());
}
});
return future.get(2, MINUTES);
}

private <T> void clusteredVertx(Consumer<AsyncResultHandler<T>> consumer, AsyncResultHandler<T> handler){
private <T> void clusteredVertx(Consumer<AsyncResultHandler<T>> consumer, AsyncResultHandler<T> handler) {
consumer.accept(handler);
}

private <T> void setParameter(T param, Consumer<T> setter){
if (param != null){
private <T> void setParameter(T param, Consumer<T> setter) {
if (param != null) {
setter.accept(param);
}
}

private String getDefaultAddress() {
Enumeration<NetworkInterface> nets;
try {
nets = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
log.warn("Unable to determine network interfaces. Using \"localhost\" as host address.", e);
return "localhost";
}
NetworkInterface netinf;
while (nets.hasMoreElements()) {
netinf = nets.nextElement();
Enumeration<InetAddress> addresses = netinf.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isAnyLocalAddress() && !address.isMulticastAddress()
&& !(address instanceof Inet6Address)) {
return address.getHostAddress();
}
}
}
log.info("Couldn't determine the network host. Using \"localhost\" as host address");
return "localhost";
}
}

0 comments on commit 7060011

Please sign in to comment.