本文整理汇总了Java中io.netty.channel.ConnectTimeoutException类的典型用法代码示例。如果您正苦于以下问题:Java ConnectTimeoutException类的具体用法?Java ConnectTimeoutException怎么用?Java ConnectTimeoutException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConnectTimeoutException类属于io.netty.channel包,在下文中一共展示了ConnectTimeoutException类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConnectTimeoutFailure
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Test
public void testConnectTimeoutFailure() throws Exception {
NetworkClient<String> client = createClient(f -> f.setConnectTimeout(1));
repeat(3, i -> {
NetworkClientCallbackMock<String> callback = new NetworkClientCallbackMock<>();
assertSame(NetworkClient.State.DISCONNECTED, client.state());
try {
get(client.connect(new InetSocketAddress("hekate.io", 81), callback));
fail("Error was expected");
} catch (ExecutionException e) {
assertTrue(e.getCause().toString(), ErrorUtils.isCausedBy(ConnectTimeoutException.class, e));
}
assertSame(NetworkClient.State.DISCONNECTED, client.state());
assertNull(client.remoteAddress());
assertNull(client.localAddress());
callback.assertConnects(0);
callback.assertDisconnects(1);
callback.assertErrors(1);
callback.getErrors().forEach(e -> assertTrue(e.toString(), e instanceof ConnectTimeoutException));
});
}
示例2: doConnect
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Override
protected void doConnect(SocketAddress remoteAddress,
SocketAddress localAddress) throws Exception {
if (localAddress != null) {
socket.bind(localAddress);
}
boolean success = false;
try {
socket.connect(remoteAddress, config().getConnectTimeoutMillis());
activate(socket.getInputStream(), socket.getOutputStream());
success = true;
} catch (SocketTimeoutException e) {
ConnectTimeoutException cause = new ConnectTimeoutException("connection timed out: " + remoteAddress);
cause.setStackTrace(e.getStackTrace());
throw cause;
} finally {
if (!success) {
doClose();
}
}
}
示例3: exceptionCaught
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
String message;
if (!(cause instanceof ConnectTimeoutException) && (!(cause instanceof ConnectException) || !cause.getMessage()
.contains("connection timed out"))) {
if (cause instanceof ReadTimeoutException) {
message = "Read timed out.";
} else if (cause instanceof WriteTimeoutException) {
message = "Write timed out.";
} else {
message = "Internal network exception.";
}
} else {
message = "Connection timed out.";
}
this.disconnect(message, cause);
}
示例4: testConnectTimeout
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Test(timeout = 1000)
public void testConnectTimeout() {
SocketOptions socketOptions = SocketOptions.builder().connectTimeout(100, TimeUnit.MILLISECONDS).build();
client.setOptions(ClientOptions.builder().socketOptions(socketOptions).build());
try {
client.connect(RedisURI.create("2:4:5:5::1", 60000));
fail("Missing RedisConnectionException");
} catch (RedisConnectionException e) {
if (e.getCause() instanceof ConnectTimeoutException) {
assertThat(e).hasRootCauseInstanceOf(ConnectTimeoutException.class);
assertThat(e.getCause()).hasMessageContaining("connection timed out");
return;
}
if (e.getCause() instanceof SocketException) {
// Network is unreachable or No route to host are OK as well.
return;
}
}
}
示例5: shouldForceTimeoutOfSocketConnectDoesNotReturn
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Test
public void shouldForceTimeoutOfSocketConnectDoesNotReturn() {
BootstrapAdapter bootstrap = mock(BootstrapAdapter.class);
when(bootstrap.connect()).thenReturn(channel.newPromise()); // this promise never completes
Endpoint endpoint = new DummyEndpoint(bootstrap, environment);
Observable<LifecycleState> observable = endpoint.connect();
TestSubscriber<LifecycleState> testSubscriber = new TestSubscriber<LifecycleState>();
observable.subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
List<Throwable> errors = testSubscriber.getOnErrorEvents();
assertEquals(1, errors.size());
assertEquals(ConnectTimeoutException.class, errors.get(0).getClass());
endpoint.disconnect().subscribe();
}
示例6: testConnectException2
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Test
public void testConnectException2() throws Exception {
HttpClientBuilder<ByteBuf, ByteBuf> clientBuilder = new HttpClientBuilder<ByteBuf, ByteBuf>("www.google.com", 81);
HttpClient<ByteBuf, ByteBuf> client = clientBuilder.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10).build();
Observable<HttpClientResponse<ByteBuf>> response = client.submit(HttpClientRequest.createGet("/"));
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> ex = new AtomicReference<Throwable>();
response.subscribe(new Observer<HttpClientResponse<ByteBuf>>() {
@Override
public void onCompleted() {
latch.countDown();
}
@Override
public void onError(Throwable e) {
ex.set(e);
latch.countDown();
}
@Override
public void onNext(HttpClientResponse<ByteBuf> args) {
}
});
latch.await(10, TimeUnit.SECONDS);
assertTrue(ex.get() instanceof ConnectTimeoutException);
}
示例7: isRetryException
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
public static boolean isRetryException(Throwable exception) {
if (exception instanceof ConnectException
|| exception instanceof ConnectTimeoutException
|| exception instanceof UnknownHostException) {
return true;
}
return false;
}
示例8: testNegativeConnectCall
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Test
public void testNegativeConnectCall() throws Exception {
asyncCall(new ClientBuilder() {
@Override
public PbrpcClient getClient() {
return PbrpcClientFactory.buildShortLiveConnection("9.9.9.9", 9999);
}
}, new ConnectTimeoutException(), false);
}
示例9: exceptionCaught
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause instanceof ConnectTimeoutException) {
Log.d(TAG, "connection timeout exception");
return; // no finish, fall through to listener
}
mSession.finish(cause);
}
示例10: exceptionCaught
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
String message = null;
if(cause instanceof ConnectTimeoutException || (cause instanceof ConnectException && cause.getMessage().contains("connection timed out"))) {
message = "Connection timed out.";
} else if(cause instanceof ReadTimeoutException) {
message = "Read timed out.";
} else if(cause instanceof WriteTimeoutException) {
message = "Write timed out.";
} else {
message = cause.toString();
}
this.disconnect(message, cause);
}
示例11: userEventTriggered
import io.netty.channel.ConnectTimeoutException; //导入依赖的package包/类
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof SslHandshakeCompletionEvent) {
if (((SslHandshakeCompletionEvent)evt).isSuccess()) {
if (debug) {
log.debug("SSL connection established [to={}]", id);
}
handshake(ctx);
}
super.userEventTriggered(ctx, evt);
} else if (evt instanceof AutoReadChangeEvent) {
if (evt == AutoReadChangeEvent.PAUSE) {
// Completely ignore read timeouts.
ignoreTimeouts = -1;
} else {
// Ignore next timeout.
ignoreTimeouts = 1;
}
} else if (evt instanceof IdleStateEvent) {
if (state == CONNECTING || state == CONNECTED) {
IdleStateEvent idle = (IdleStateEvent)evt;
if (idle.state() == IdleState.WRITER_IDLE) {
if (hbFlushed) {
// Make sure that we don't push multiple heartbeats to the network buffer simultaneously.
// Need to perform this check since remote peer can hang and stop reading
// while this channel will still be trying to put more and more heartbeats on its send buffer.
hbFlushed = false;
ctx.writeAndFlush(Heartbeat.INSTANCE).addListener(hbOnFlush);
}
} else {
// Reader idle.
// Ignore if auto-reading was disabled since in such case we will not read any heartbeats.
if (ignoreTimeouts != -1 && ctx.channel().config().isAutoRead()) {
// Check if timeout should be ignored.
if (ignoreTimeouts > 0) {
// Decrement the counter of ignored timeouts.
ignoreTimeouts--;
} else {
if (state == CONNECTING) {
ctx.fireExceptionCaught(new ConnectTimeoutException("Timeout while connecting to " + id));
} else if (state == CONNECTED) {
ctx.fireExceptionCaught(new SocketTimeoutException("Timeout while reading data from " + id));
}
}
}
}
}
} else {
super.userEventTriggered(ctx, evt);
}
}