use of java.nio.channels.UnresolvedAddressException in project moleculer-java by moleculer-java.
the class TcpWriter method run.
// --- WRITER LOOP ---
@Override
public void run() {
try {
// Loop
while (true) {
// Waiting for sockets
int n;
try {
n = selector.select(3000);
} catch (NullPointerException nullPointer) {
continue;
} catch (Exception cause) {
break;
}
// Open new connections
SendBuffer buffer = opened.poll();
SelectionKey key = null;
while (buffer != null) {
try {
InetSocketAddress address;
try {
address = new InetSocketAddress(buffer.host, buffer.port);
} catch (UnresolvedAddressException dnsError) {
// Workaround: unable to resolve host name
Tree info = transporter.getDescriptor(buffer.nodeID);
if (info == null) {
throw dnsError;
}
String ip = getHostOrIP(false, info);
if (ip == null || buffer.host.equalsIgnoreCase(ip)) {
throw dnsError;
}
if (debug) {
logger.info("Unable to resolve hostname \"" + buffer.host + "\", trying with \"" + ip + "\"...");
}
address = new InetSocketAddress(ip, buffer.port);
}
SocketChannel channel = SocketChannel.open(address);
channel.configureBlocking(false);
channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
channel.setOption(StandardSocketOptions.SO_LINGER, -1);
key = channel.register(selector, SelectionKey.OP_WRITE);
key.attach(buffer);
buffer.connected(key, channel);
if (debug) {
logger.info("Client channel opened to \"" + buffer.nodeID + "\".");
}
} catch (Throwable cause) {
if (buffer != null) {
synchronized (buffers) {
buffers.remove(buffer.nodeID);
}
transporter.unableToSend(buffer.nodeID, buffer.getUnsentPackets(), cause);
}
}
buffer = opened.poll();
}
if (n < 1) {
continue;
}
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
key = keys.next();
if (key == null) {
continue;
}
if (!key.isValid()) {
keys.remove();
continue;
}
if (key.isWritable()) {
// Write data
buffer = null;
try {
buffer = (SendBuffer) key.attachment();
if (buffer != null) {
buffer.write();
}
} catch (Exception cause) {
if (buffer != null) {
synchronized (buffers) {
buffers.remove(buffer.nodeID);
}
transporter.unableToSend(buffer.nodeID, buffer.getUnsentPackets(), cause);
}
close(key, cause);
}
}
keys.remove();
}
}
} catch (Exception fatal) {
logger.error("TCP writer closed!", fatal);
}
}
use of java.nio.channels.UnresolvedAddressException in project alluxio by Alluxio.
the class AbstractClient method retryRPCInternal.
private synchronized <V> V retryRPCInternal(RetryPolicy retryPolicy, RpcCallable<V> rpc, Supplier<Void> onRetry) throws AlluxioStatusException {
Exception ex = null;
while (retryPolicy.attempt()) {
if (mClosed) {
throw new FailedPreconditionException("Client is closed");
}
connect();
try {
return rpc.call();
} catch (StatusRuntimeException e) {
AlluxioStatusException se = AlluxioStatusException.fromStatusRuntimeException(e);
if (se.getStatusCode() == Status.Code.UNAVAILABLE || se.getStatusCode() == Status.Code.CANCELLED || se.getStatusCode() == Status.Code.UNAUTHENTICATED || e.getCause() instanceof UnresolvedAddressException) {
ex = se;
} else {
throw se;
}
}
LOG.debug("Rpc failed ({}): ", retryPolicy.getAttemptCount(), ex);
onRetry.get();
disconnect();
}
throw new UnavailableException("Failed after " + retryPolicy.getAttemptCount() + " attempts: " + ex.toString(), ex);
}
use of java.nio.channels.UnresolvedAddressException in project grpc-java by grpc.
the class UtilsTest method testStatusFromThrowable.
@Test
public void testStatusFromThrowable() {
Status s = Status.CANCELLED.withDescription("msg");
assertSame(s, Utils.statusFromThrowable(new Exception(s.asException())));
Throwable t;
t = new ConnectTimeoutException("msg");
assertStatusEquals(Status.UNAVAILABLE.withCause(t), Utils.statusFromThrowable(t));
t = new UnresolvedAddressException();
assertStatusEquals(Status.UNAVAILABLE.withCause(t), Utils.statusFromThrowable(t));
t = new Http2Exception(Http2Error.INTERNAL_ERROR, "msg");
assertStatusEquals(Status.INTERNAL.withCause(t), Utils.statusFromThrowable(t));
t = new Exception("msg");
assertStatusEquals(Status.UNKNOWN.withCause(t), Utils.statusFromThrowable(t));
}
use of java.nio.channels.UnresolvedAddressException in project j2objc by google.
the class SocketChannelTest method testCFII_Unresolved.
public void testCFII_Unresolved() throws IOException {
statusNotConnected_NotPending();
InetSocketAddress unresolved = new InetSocketAddress("unresolved address", 1080);
try {
this.channel1.connect(unresolved);
fail("Should throw an UnresolvedAddressException here.");
} catch (UnresolvedAddressException e) {
// OK.
}
}
use of java.nio.channels.UnresolvedAddressException in project j2objc by google.
the class UnresolvedAddressExceptionTest method test_Constructor.
/**
* @tests {@link java.nio.channels.UnresolvedAddressException#UnresolvedAddressException()}
*/
public void test_Constructor() {
UnresolvedAddressException e = new UnresolvedAddressException();
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
Aggregations