Skip to content

Commit

Permalink
KAA-1470. Use Sockets instead of URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Oct 26, 2016
1 parent 37ed28b commit a478578
Showing 1 changed file with 13 additions and 13 deletions.
Expand Up @@ -23,40 +23,40 @@


import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.MessageFormat; import java.text.MessageFormat;


public class PingConnectivityChecker implements ConnectivityChecker { public class PingConnectivityChecker implements ConnectivityChecker {
public static final Logger LOG = LoggerFactory.getLogger(PingConnectivityChecker.class); public static final Logger LOG = LoggerFactory.getLogger(PingConnectivityChecker.class);
private static final String DEFAULT_HOST = "http://www.google.com"; private static final String DEFAULT_HOST = "www.google.com";
private static final int DEFAULT_PORT = 80;
private static final int CONNECTION_TIMEOUT_MS = 3000;


private final String urlAddress; private final String host;


public PingConnectivityChecker() { public PingConnectivityChecker() {
this(DEFAULT_HOST); this(DEFAULT_HOST);
} }


public PingConnectivityChecker(String urlAddress) { public PingConnectivityChecker(String host) {
this.urlAddress = urlAddress; this.host = host;
} }


@Override @Override
public boolean checkConnectivity() { public boolean checkConnectivity() {

try { try {
final URL url = new URL(urlAddress); try (Socket soc = new Socket()) {
final HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection(); soc.connect(new InetSocketAddress(host, DEFAULT_PORT), CONNECTION_TIMEOUT_MS);
urlConnect.connect(); }
return true; return true;

} catch (UnknownHostException ex) {
LOG.warn(MessageFormat.format("Host {0} is unreachable", urlAddress), ex);
return false;
} catch (IOException ex) { } catch (IOException ex) {
LOG.warn(MessageFormat.format("Host {0} is unreachable", urlAddress), ex); LOG.warn(MessageFormat.format("Host {0} is unreachable", host), ex);
return false; return false;
} }

} }


} }

0 comments on commit a478578

Please sign in to comment.