From 61d6c48365337fb3840ccfc8a3d097b294e360b7 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 7 Mar 2013 20:53:45 +0100 Subject: [PATCH] [#1036] Add special ConnectTimeoutException which is thrown if a connection failed because of a timeout --- .../netty/channel/ChannelOutboundInvoker.java | 10 ++++++ .../channel/ConnectTimeoutException.java | 33 +++++++++++++++++++ .../netty/channel/aio/AbstractAioChannel.java | 6 ++-- .../netty/channel/nio/AbstractNioChannel.java | 6 ++-- .../channel/socket/oio/OioSocketChannel.java | 3 ++ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 transport/src/main/java/io/netty/channel/ConnectTimeoutException.java diff --git a/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java b/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java index 62e758442f..2444d0c58f 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java +++ b/transport/src/main/java/io/netty/channel/ChannelOutboundInvoker.java @@ -15,6 +15,7 @@ */ package io.netty.channel; +import java.net.ConnectException; import java.net.SocketAddress; import io.netty.util.concurrent.EventExecutor; /** @@ -37,6 +38,10 @@ interface ChannelOutboundInvoker { * Request to connect to the given {@link SocketAddress} and notify the {@link ChannelFuture} once the operation * completes, either because the operation was successful or because of an error. *

+ * If the connection fails because of a connection timeout, the {@link ChannelFuture} will get failed with + * a {@link ConnectTimeoutException}. If it fails because of connection refused a {@link ConnectException} + * will be used. + *

* This will result in having the * {@link ChannelOperationHandler#connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)} * method called of the next {@link ChannelOperationHandler} contained in the {@link ChannelPipeline} of the @@ -160,6 +165,11 @@ interface ChannelOutboundInvoker { * completes, either because the operation was successful or because of an error. * * The given {@link ChannelFuture} will be notified. + * + *

+ * If the connection fails because of a connection timeout, the {@link ChannelFuture} will get failed with + * a {@link ConnectTimeoutException}. If it fails because of connection refused a {@link ConnectException} + * will be used. *

* This will result in having the * {@link ChannelOperationHandler#connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)} diff --git a/transport/src/main/java/io/netty/channel/ConnectTimeoutException.java b/transport/src/main/java/io/netty/channel/ConnectTimeoutException.java new file mode 100644 index 0000000000..b037df39ef --- /dev/null +++ b/transport/src/main/java/io/netty/channel/ConnectTimeoutException.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.channel; + +import java.net.ConnectException; + +/** + * {@link ConnectException} which will be thrown if a connection could + * not be established because of a connection timeout. + */ +public class ConnectTimeoutException extends ConnectException { + private static final long serialVersionUID = 2317065249988317463L; + + public ConnectTimeoutException(String msg) { + super(msg); + } + + public ConnectTimeoutException() { + } +} diff --git a/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java b/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java index 76b22e4223..dfb2be5fa4 100755 --- a/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java +++ b/transport/src/main/java/io/netty/channel/aio/AbstractAioChannel.java @@ -18,11 +18,11 @@ package io.netty.channel.aio; import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelPromise; +import io.netty.channel.ConnectTimeoutException; import io.netty.channel.EventLoop; import java.net.ConnectException; import java.net.SocketAddress; -import java.net.SocketTimeoutException; import java.nio.channels.AsynchronousChannel; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -115,8 +115,8 @@ public abstract class AbstractAioChannel extends AbstractChannel { @Override public void run() { ChannelPromise connectFuture = connectPromise; - SocketTimeoutException cause = - new SocketTimeoutException("connection timed out: " + remoteAddress); + ConnectTimeoutException cause = + new ConnectTimeoutException("connection timed out: " + remoteAddress); if (connectFuture != null && connectFuture.tryFailure(cause)) { close(voidFuture()); } diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java index 6cef235634..5a671a0e33 100755 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java @@ -19,6 +19,7 @@ import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelPromise; +import io.netty.channel.ConnectTimeoutException; import io.netty.channel.EventLoop; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -26,7 +27,6 @@ import io.netty.util.internal.logging.InternalLoggerFactory; import java.io.IOException; import java.net.ConnectException; import java.net.SocketAddress; -import java.net.SocketTimeoutException; import java.nio.channels.CancelledKeyException; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; @@ -184,8 +184,8 @@ public abstract class AbstractNioChannel extends AbstractChannel { @Override public void run() { ChannelPromise connectPromise = AbstractNioChannel.this.connectPromise; - SocketTimeoutException cause = - new SocketTimeoutException("connection timed out: " + remoteAddress); + ConnectTimeoutException cause = + new ConnectTimeoutException("connection timed out: " + remoteAddress); if (connectPromise != null && connectPromise.tryFailure(cause)) { close(voidFuture()); } diff --git a/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java index a1901b50eb..ce50dc256d 100755 --- a/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/oio/OioSocketChannel.java @@ -20,6 +20,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelPromise; +import io.netty.channel.ConnectTimeoutException; import io.netty.channel.EventLoop; import io.netty.channel.oio.OioByteStreamChannel; import io.netty.channel.socket.ServerSocketChannel; @@ -199,6 +200,8 @@ public class OioSocketChannel extends OioByteStreamChannel socket.connect(remoteAddress, config().getConnectTimeoutMillis()); activate(socket.getInputStream(), socket.getOutputStream()); success = true; + } catch (SocketTimeoutException e) { + throw new ConnectTimeoutException("connection timed out: " + remoteAddress); } finally { if (!success) { doClose();