Fix #153: Add ChannelFuture.rethrowIfFailed()
This commit is contained in:
parent
fafeae7aa3
commit
c95f9314f3
@ -261,6 +261,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.
|
||||||
*
|
*
|
||||||
|
@ -168,6 +168,27 @@ public class DefaultChannelFuture implements ChannelFuture {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
public ChannelFuture await() throws InterruptedException {
|
public ChannelFuture await() throws InterruptedException {
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
throw new InterruptedException();
|
throw new InterruptedException();
|
||||||
|
@ -45,4 +45,16 @@ public class FailedChannelFuture extends CompleteChannelFuture {
|
|||||||
public boolean isSuccess() {
|
public boolean isSuccess() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ChannelFuture rethrowIfFailed() throws Exception {
|
||||||
|
if (cause instanceof Exception) {
|
||||||
|
throw (Exception) cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cause instanceof Error) {
|
||||||
|
throw (Error) cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException(cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,4 +38,8 @@ public class SucceededChannelFuture extends CompleteChannelFuture {
|
|||||||
public boolean isSuccess() {
|
public boolean isSuccess() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ChannelFuture rethrowIfFailed() throws Exception {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ package org.jboss.netty.example.http.websocketx.client;
|
|||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@ -39,11 +38,9 @@ import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
|
|||||||
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
||||||
|
|
||||||
public class WebSocketClient {
|
public class WebSocketClient {
|
||||||
@ -61,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);
|
||||||
@ -96,25 +96,20 @@ 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);
|
|
||||||
|
|
||||||
Thread.sleep(1000);
|
|
||||||
|
|
||||||
// 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");
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
ch.write(new TextWebSocketFrame("Message #" + i));
|
ch.write(new TextWebSocketFrame("Message #" + i));
|
||||||
}
|
}
|
||||||
Thread.sleep(1000);
|
|
||||||
|
|
||||||
// Ping
|
// Ping
|
||||||
System.out.println("WebSocket Client sending ping");
|
System.out.println("WebSocket Client sending ping");
|
||||||
ch.write(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
|
ch.write(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[]{1, 2, 3, 4, 5, 6})));
|
||||||
Thread.sleep(1000);
|
|
||||||
|
|
||||||
// Close
|
// Close
|
||||||
System.out.println("WebSocket Client sending close");
|
System.out.println("WebSocket Client sending close");
|
||||||
@ -123,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;
|
||||||
|
@ -96,6 +96,10 @@ public class CompleteChannelFutureTest {
|
|||||||
public boolean isSuccess() {
|
public boolean isSuccess() {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ChannelFuture rethrowIfFailed() throws Exception {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ExpectedError extends Error {
|
private static class ExpectedError extends Error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user