Fix #153: Add ChannelFuture.rethrowIfFailed()

This commit is contained in:
Trustin Lee 2012-01-19 13:33:37 +09:00
parent c1aa8b4c7b
commit 40ef4d2ccf
6 changed files with 114 additions and 57 deletions

View File

@ -58,6 +58,9 @@ public class WebSocketClient {
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())); Executors.newCachedThreadPool()));
Channel ch = null;
try {
String protocol = uri.getScheme(); String protocol = uri.getScheme();
if (!protocol.equals("ws")) { if (!protocol.equals("ws")) {
throw new IllegalArgumentException("Unsupported protocol: " + protocol); throw new IllegalArgumentException("Unsupported protocol: " + protocol);
@ -93,11 +96,10 @@ public class WebSocketClient {
ChannelFuture future = ChannelFuture future =
bootstrap.connect( bootstrap.connect(
new InetSocketAddress(uri.getHost(), uri.getPort())); new InetSocketAddress(uri.getHost(), uri.getPort()));
future.awaitUninterruptibly(); future.awaitUninterruptibly().rethrowIfFailed();
Channel ch = future.getChannel(); ch = future.getChannel();
handshaker.handshake(ch).awaitUninterruptibly().rethrowIfFailed();
handshaker.handshake(ch).awaitUninterruptibly();
// Send 10 messages and wait for responses // Send 10 messages and wait for responses
System.out.println("WebSocket Client sending message"); System.out.println("WebSocket Client sending message");
@ -116,9 +118,13 @@ public class WebSocketClient {
// WebSocketClientHandler will close the connection when the server // WebSocketClientHandler will close the connection when the server
// responds to the CloseWebSocketFrame. // responds to the CloseWebSocketFrame.
ch.getCloseFuture().awaitUninterruptibly(); ch.getCloseFuture().awaitUninterruptibly();
} finally {
if (ch != null) {
ch.close();
}
bootstrap.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
URI uri; URI uri;

View File

@ -258,6 +258,12 @@ public interface ChannelFuture {
*/ */
void removeListener(ChannelFutureListener listener); void removeListener(ChannelFutureListener listener);
/**
* Rethrows the exception that caused this future fail if this future is
* complete and failed.
*/
ChannelFuture rethrowIfFailed() throws Exception;
/** /**
* Waits for this future to be completed. * Waits for this future to be completed.
* *

View File

@ -175,6 +175,28 @@ public class DefaultChannelFuture implements ChannelFuture {
} }
} }
@Override
public ChannelFuture rethrowIfFailed() throws Exception {
if (!isDone()) {
return this;
}
Throwable cause = getCause();
if (cause == null) {
return this;
}
if (cause instanceof Exception) {
throw (Exception) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(cause);
}
@Override @Override
public ChannelFuture await() throws InterruptedException { public ChannelFuture await() throws InterruptedException {
if (Thread.interrupted()) { if (Thread.interrupted()) {

View File

@ -47,4 +47,17 @@ public class FailedChannelFuture extends CompleteChannelFuture {
public boolean isSuccess() { public boolean isSuccess() {
return false; return false;
} }
@Override
public ChannelFuture rethrowIfFailed() throws Exception {
if (cause instanceof Exception) {
throw (Exception) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(cause);
}
} }

View File

@ -40,4 +40,9 @@ public class SucceededChannelFuture extends CompleteChannelFuture {
public boolean isSuccess() { public boolean isSuccess() {
return true; return true;
} }
@Override
public ChannelFuture rethrowIfFailed() throws Exception {
return this;
}
} }

View File

@ -99,6 +99,11 @@ public class CompleteChannelFutureTest {
public boolean isSuccess() { public boolean isSuccess() {
throw new Error(); throw new Error();
} }
@Override
public ChannelFuture rethrowIfFailed() throws Exception {
throw new Error();
}
} }
private static class ExpectedError extends Error { private static class ExpectedError extends Error {