use of java.net.ConnectException in project pinot by linkedin.
the class NettyTCPClientConnection method connect.
/**
* Open a connection
*/
@Override
public boolean connect() {
try {
checkTransition(State.CONNECTED);
//Connect synchronously. At the end of this line, _channel should have been set
TimerContext t = MetricsHelper.startTimer();
ChannelFuture f = _bootstrap.connect(_server.getHostname(), _server.getPort()).sync();
/**
* Waiting for future alone does not guarantee that _channel is set. _channel is set
* only when the channelActive() async callback runs. So we should also wait for it.
*/
f.get();
_channelSet.await();
t.stop();
_connState = State.CONNECTED;
_clientMetric.addConnectStats(t.getLatencyMs());
return true;
} catch (Exception ie) {
if (ie instanceof ConnectException && ie.getMessage() != null && ie.getMessage().startsWith("Connection refused")) {
// Most common case when a server is down. Don't print the entire stack and fill the logs.
LOGGER.error("Could not connect to server {}:{} connId:{}", _server, ie.getMessage(), getConnId());
} else {
LOGGER.error("Got exception when connecting to server {} connId {}", _server, ie, getConnId());
}
}
return false;
}
use of java.net.ConnectException in project rest.li by linkedin.
the class LoadBalancerUtilTest method testFindOriginalThrowable.
@Test
public void testFindOriginalThrowable() {
ConnectException connectException = new ConnectException("Foo");
RemoteInvocationException e = new RemoteInvocationException("Failed to get connect to a server", connectException);
Throwable throwable = LoadBalancerUtil.findOriginalThrowable(e);
Assert.assertEquals(throwable, connectException);
//we only go as far as 100 level deep for finding exception
Exception npe = new NullPointerException();
Exception temp = npe;
for (int i = 0; i < 100; i++) {
e = new RemoteInvocationException(temp);
temp = e;
}
throwable = LoadBalancerUtil.findOriginalThrowable(e);
Assert.assertEquals(throwable, npe);
//we add the 101th exception then we lost the reference to NullPointerException
e = new RemoteInvocationException(temp);
throwable = LoadBalancerUtil.findOriginalThrowable(e);
Assert.assertFalse(throwable instanceof NullPointerException);
}
use of java.net.ConnectException in project jmxtrans by jmxtrans.
the class OpenTSDBWriter method internalWrite.
@Override
public void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
Socket socket = null;
PrintWriter writer = null;
try {
socket = pool.borrowObject(address);
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), UTF_8), true);
for (String formattedResult : messageFormatter.formatResults(results, server)) {
log.debug("OpenTSDB Message: {}", formattedResult);
writer.write("put " + formattedResult + "\n");
}
} catch (ConnectException e) {
log.error("Error while connecting to OpenTSDB", e);
} finally {
if (writer != null && writer.checkError()) {
log.error("Error writing to OpenTSDB, clearing OpenTSDB socket pool");
pool.invalidateObject(address, socket);
} else {
pool.returnObject(address, socket);
}
}
}
use of java.net.ConnectException in project hudson-2.x by hudson.
the class ProxyConfiguration method open.
/**
* This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly.
*/
public static URLConnection open(URL url) throws IOException {
// this code might run on slaves
Hudson hudson = Hudson.getInstance();
ProxyConfiguration proxyConfig = hudson != null ? hudson.proxy : null;
if (proxyConfig == null) {
return url.openConnection();
}
if (proxyConfig.noProxyFor != null) {
StringTokenizer tokenizer = new StringTokenizer(proxyConfig.noProxyFor, ",");
while (tokenizer.hasMoreTokens()) {
String noProxyHost = tokenizer.nextToken().trim();
if (noProxyHost.contains("*")) {
if (url.getHost().trim().contains(noProxyHost.replaceAll("\\*", ""))) {
return url.openConnection(Proxy.NO_PROXY);
}
} else if (url.getHost().trim().equals(noProxyHost)) {
return url.openConnection(Proxy.NO_PROXY);
}
}
}
URLConnection urlConnection = url.openConnection(proxyConfig.createProxy());
if (proxyConfig.isAuthNeeded()) {
String credentials = proxyConfig.getUserName() + ":" + proxyConfig.getPassword();
String encoded = new String(Base64.encodeBase64(credentials.getBytes()));
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
}
boolean connected = false;
int count = 0;
while (!connected) {
try {
urlConnection.connect();
connected = true;
} catch (SocketTimeoutException exc) {
LOGGER.fine("Connection timed out. trying again " + count);
if (++count > TIME_OUT_RETRY_COUNT) {
throw new IOException("Could not connect to " + url.toExternalForm() + ". Connection timed out after " + TIME_OUT_RETRY_COUNT + " tries.");
}
connected = false;
} catch (UnknownHostException exc) {
throw new IOException2("Could not connect to " + url.toExternalForm() + ". Check your internet connection.", exc);
} catch (ConnectException exc) {
throw new IOException2("Could not connect to " + url.toExternalForm() + ". Check your internet connection.", exc);
}
}
return urlConnection;
}
use of java.net.ConnectException in project jsoup by jhy.
the class UrlConnectTest method invalidProxyFails.
@Test
public void invalidProxyFails() throws IOException {
boolean caught = false;
String url = "https://jsoup.org";
try {
Document doc = Jsoup.connect(url).proxy("localhost", 8889).get();
} catch (IOException e) {
caught = e instanceof ConnectException;
}
assertTrue(caught);
}
Aggregations