下面列出了io.netty.channel.Channel#newPromise ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see {@link #connect()}
*/
private ChannelFuture doConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister();
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
final ChannelPromise promise = channel.newPromise();
if (regFuture.isDone()) {
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
} else {
regFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
}
});
}
return promise;
}
@Ignore("not supported anymore atm")
@Test
public void cancellingWritesBeforeFlush() {
LastInboundHandler inboundHandler = streamActiveAndWriteHeaders(inboundStream);
Channel childChannel = inboundHandler.channel();
Http2HeadersFrame headers1 = new DefaultHttp2HeadersFrame(new DefaultHttp2Headers());
Http2HeadersFrame headers2 = new DefaultHttp2HeadersFrame(new DefaultHttp2Headers());
ChannelPromise writePromise = childChannel.newPromise();
childChannel.write(headers1, writePromise);
childChannel.write(headers2);
assertTrue(writePromise.cancel(false));
childChannel.flush();
Http2HeadersFrame headers = parentChannel.readOutbound();
assertSame(headers, headers2);
}
private void write(Append cmd) throws ConnectionFailedException {
Channel channel = nettyHandler.getChannel();
EventLoop eventLoop = channel.eventLoop();
ChannelPromise promise = channel.newPromise();
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
throttle.release(cmd.getDataLength());
nettyHandler.setRecentMessage();
if (!future.isSuccess()) {
future.channel().pipeline().fireExceptionCaught(future.cause());
}
}
});
// Work around for https://github.com/netty/netty/issues/3246
eventLoop.execute(() -> {
try {
if (!closed.get()) {
channel.write(cmd, promise);
}
} catch (Exception e) {
channel.pipeline().fireExceptionCaught(e);
}
});
Exceptions.handleInterrupted(() -> throttle.acquire(cmd.getDataLength()));
}
private void write(WireCommand cmd) throws ConnectionFailedException {
Channel channel = nettyHandler.getChannel();
EventLoop eventLoop = channel.eventLoop();
ChannelPromise promise = channel.newPromise();
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
nettyHandler.setRecentMessage();
if (!future.isSuccess()) {
future.channel().pipeline().fireExceptionCaught(future.cause());
}
}
});
// Work around for https://github.com/netty/netty/issues/3246
eventLoop.execute(() -> {
try {
if (!closed.get()) {
channel.write(cmd, promise);
}
} catch (Exception e) {
channel.pipeline().fireExceptionCaught(e);
}
});
}
@Override
public void sendAsync(List<Append> appends, CompletedCallback callback) {
Channel ch;
try {
checkClientConnectionClosed();
ch = nettyHandler.getChannel();
} catch (ConnectionFailedException e) {
callback.complete(new ConnectionFailedException("Connection to " + connectionName + " is not established."));
return;
}
PromiseCombiner combiner = new PromiseCombiner(ImmediateEventExecutor.INSTANCE);
for (Append append : appends) {
combiner.add(ch.write(append));
}
ch.flush();
ChannelPromise promise = ch.newPromise();
promise.addListener(future -> {
nettyHandler.setRecentMessage();
Throwable cause = future.cause();
callback.complete(cause == null ? null : new ConnectionFailedException(cause));
});
combiner.finish(promise);
}
/**
* @see {@link #connect()}
*/
private ChannelFuture doConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister();
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
final ChannelPromise promise = channel.newPromise();
if (regFuture.isDone()) {
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
} else {
regFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
}
});
}
return promise;
}
private Future<Void> disconnect(final Channel channel) {
if (channel == null) {
return null;
}
final Promise<Void> promise = channel.newPromise();
writeToChannel(channel, Unpooled.EMPTY_BUFFER).addListener(future -> closeChannel(promise, channel));
return promise;
}
@Test(timeout = 3000)
public void testConnectFutureBeforeChannelActive() throws Exception {
Bootstrap cb = new Bootstrap();
ServerBootstrap sb = new ServerBootstrap();
cb.group(group1)
.channel(LocalChannel.class)
.handler(new ChannelInboundHandlerAdapter());
sb.group(group2)
.channel(LocalServerChannel.class)
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new TestHandler());
}
});
Channel sc = null;
Channel cc = null;
try {
// Start server
sc = sb.bind(TEST_ADDRESS).sync().channel();
cc = cb.register().sync().channel();
final ChannelPromise promise = cc.newPromise();
final Promise<Void> assertPromise = cc.eventLoop().newPromise();
cc.pipeline().addLast(new TestHandler() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// Ensure the promise was done before the handler method is triggered.
if (promise.isDone()) {
assertPromise.setSuccess(null);
} else {
assertPromise.setFailure(new AssertionError("connect promise should be done"));
}
}
});
// Connect to the server
cc.connect(sc.localAddress(), promise).sync();
assertPromise.syncUninterruptibly();
assertTrue(promise.isSuccess());
} finally {
closeChannel(cc);
closeChannel(sc);
}
}
@Override
public ChannelFuture register(Channel channel) {
super.register(channel).syncUninterruptibly();
promise = channel.newPromise();
return promise;
}
@SuppressWarnings("FutureReturnValueIgnored")
WebsocketServerOperations(String wsUrl, WebsocketServerSpec websocketServerSpec, HttpServerOperations replaced) {
super(replaced);
this.proxyPing = websocketServerSpec.handlePing();
Channel channel = replaced.channel();
onCloseState = MonoProcessor.create();
// Handshake
WebSocketServerHandshakerFactory wsFactory =
new WebSocketServerHandshakerFactory(wsUrl, websocketServerSpec.protocols(), true, websocketServerSpec.maxFramePayloadLength());
handshaker = wsFactory.newHandshaker(replaced.nettyRequest);
if (handshaker == null) {
//"FutureReturnValueIgnored" this is deliberate
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
handshakerResult = null;
}
else {
removeHandler(NettyPipeline.HttpTrafficHandler);
removeHandler(NettyPipeline.AccessLogHandler);
removeHandler(NettyPipeline.HttpMetricsHandler);
handshakerResult = channel.newPromise();
HttpRequest request = new DefaultFullHttpRequest(replaced.version(),
replaced.method(),
replaced.uri());
request.headers()
.set(replaced.nettyRequest.headers());
if (websocketServerSpec.compress()) {
removeHandler(NettyPipeline.CompressionHandler);
WebSocketServerCompressionHandler wsServerCompressionHandler =
new WebSocketServerCompressionHandler();
try {
wsServerCompressionHandler.channelRead(channel.pipeline()
.context(NettyPipeline.ReactiveBridge),
request);
addHandlerFirst(NettyPipeline.WsCompressionHandler, wsServerCompressionHandler);
} catch (Throwable e) {
log.error(format(channel(), ""), e);
}
}
handshaker.handshake(channel,
request,
replaced.responseHeaders
.remove(HttpHeaderNames.TRANSFER_ENCODING),
handshakerResult)
.addListener(f -> {
if (replaced.rebind(this)) {
markPersistent(false);
}
else {
log.debug("Cannot bind WebsocketServerOperations after the handshake.");
}
});
}
}
@Override
public ChannelFuture register(Channel channel) {
super.register(channel).syncUninterruptibly();
promise = channel.newPromise();
return promise;
}
public void startHandshake(Channel channel) {
handshaker.handshake(channel);
handshakeFuture = channel.newPromise();
}
public void startHandshake(Channel channel) {
handshaker.handshake(channel);
handshakeFuture = channel.newPromise();
}