[#1036] Add special ConnectTimeoutException which is thrown if a connection failed because of a timeout

This commit is contained in:
Norman Maurer 2013-03-07 20:53:45 +01:00
parent 9599bfd569
commit 61d6c48365
5 changed files with 52 additions and 6 deletions

View File

@ -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.
* <p>
* 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.
* <p>
* 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.
*
* <p>
* 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.
* <p>
* This will result in having the
* {@link ChannelOperationHandler#connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)}

View File

@ -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() {
}
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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();