本文整理了Java中io.netty.channel.ChannelFuture.addListener()
方法的一些代码示例,展示了ChannelFuture.addListener()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ChannelFuture.addListener()
方法的具体详情如下:
包路径:io.netty.channel.ChannelFuture
类名称:ChannelFuture
方法名:addListener
暂无
代码示例来源:origin: apache/incubator-dubbo
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
if (!acceptForeignIp) {
if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) {
ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted."
+ QosConstants.BR_STR).getBytes());
ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE);
}
}
}
}
代码示例来源:origin: eclipse-vertx/vert.x
private void sendServiceUnavailable(Channel ch) {
ch.writeAndFlush(
Unpooled.copiedBuffer("HTTP/1.1 503 Service Unavailable\r\n" +
"Content-Length:0\r\n" +
"\r\n", StandardCharsets.ISO_8859_1))
.addListener(ChannelFutureListener.CLOSE);
}
代码示例来源:origin: redisson/redisson
@Override
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception {
final InetAddress remoteIp = remoteAddress.getAddress();
if (!connected.add(remoteIp)) {
return false;
} else {
ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
connected.remove(remoteIp);
}
});
return true;
}
}
}
代码示例来源:origin: lets-blade/blade
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
var response = new DefaultFullHttpResponse(HTTP_1_1, status,
Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT);
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
代码示例来源:origin: Netflix/zuul
public final void sendHttpResponse(HttpRequest req, ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status);
resp.headers().add("Content-Length", "0");
final boolean closeConn = ((status != OK) || (! HttpUtil.isKeepAlive(req)));
if (closeConn) {
resp.headers().add(HttpHeaderNames.CONNECTION, "Close");
}
final ChannelFuture cf = ctx.channel().writeAndFlush(resp);
if (closeConn) {
cf.addListener(ChannelFutureListener.CLOSE);
}
}
代码示例来源:origin: mrniko/netty-socketio
private void sendError(ChannelHandlerContext ctx) {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
ctx.channel().writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
}
代码示例来源:origin: netty/netty
private void shutdownOutputDone(final ChannelFuture shutdownOutputFuture, final ChannelPromise promise) {
ChannelFuture shutdownInputFuture = shutdownInput();
if (shutdownInputFuture.isDone()) {
shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise);
} else {
shutdownInputFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture shutdownInputFuture) throws Exception {
shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise);
}
});
}
}
代码示例来源:origin: Netflix/zuul
public final void sendErrorAndClose(ChannelHandlerContext ctx, int statusCode, String reasonText) {
final Object mesg = serverClosingConnectionMessage(statusCode, reasonText);
ctx.writeAndFlush(mesg).addListener(ChannelFutureListener.CLOSE);
}
代码示例来源:origin: Netflix/zuul
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
int status = 500;
final String errorMsg = "ClientResponseWriter caught exception in client connection pipeline: " +
ChannelUtils.channelInfoForLogging(ctx.channel());
if (cause instanceof ZuulException) {
final ZuulException ze = (ZuulException) cause;
status = ze.getStatusCode();
LOG.error(errorMsg, cause);
}
else if (cause instanceof ReadTimeoutException) {
LOG.error(errorMsg + ", Read timeout fired");
status = 504;
}
else {
LOG.error(errorMsg, cause);
}
if (isHandlingRequest && !startedSendingResponseToClient && ctx.channel().isActive()) {
final HttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(status));
ctx.writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE);
startedSendingResponseToClient = true;
}
else {
ctx.close();
}
}
代码示例来源:origin: redisson/redisson
private void writeQuery(final DnsQuery query, final boolean flush, final ChannelPromise writePromise) {
final ChannelFuture writeFuture = flush ? parent.ch.writeAndFlush(query, writePromise) :
parent.ch.write(query, writePromise);
if (writeFuture.isDone()) {
onQueryWriteCompletion(writeFuture);
} else {
writeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
onQueryWriteCompletion(writeFuture);
}
});
}
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
if (!acceptForeignIp) {
if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) {
ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted."
+ QosConstants.BR_STR).getBytes());
ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE);
}
}
}
}
代码示例来源:origin: lets-blade/blade
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
var response = new DefaultFullHttpResponse(HTTP_1_1, status,
Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT);
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
代码示例来源:origin: Netflix/zuul
private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest request, HttpResponseStatus status,
PushUserAuth userAuth) {
final FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status);
resp.headers().add("Content-Length", "0");
final ChannelFuture cf = ctx.channel().writeAndFlush(resp);
if (!HttpUtil.isKeepAlive(request)) {
cf.addListener(ChannelFutureListener.CLOSE);
}
logPushEvent(request, status, userAuth);
}
代码示例来源:origin: ffay/lanproxy
private void handleDisconnectMessage(ChannelHandlerContext ctx, ProxyMessage proxyMessage) {
Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get();
logger.debug("handleDisconnectMessage, {}", realServerChannel);
if (realServerChannel != null) {
ctx.channel().attr(Constants.NEXT_CHANNEL).remove();
ClientChannelMannager.returnProxyChanel(ctx.channel());
realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
代码示例来源:origin: jamesdbloom/mockserver
/**
* Closes the specified channel after all queued write requests are flushed.
*/
public static void closeOnFlush(Channel ch) {
if (ch != null && ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
代码示例来源:origin: netty/netty
private void shutdownOutputDone(final ChannelFuture shutdownOutputFuture, final ChannelPromise promise) {
ChannelFuture shutdownInputFuture = shutdownInput();
if (shutdownInputFuture.isDone()) {
shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise);
} else {
shutdownInputFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture shutdownInputFuture) throws Exception {
shutdownDone(shutdownOutputFuture, shutdownInputFuture, promise);
}
});
}
}
代码示例来源:origin: Netflix/zuul
public final void sendErrorAndClose(ChannelHandlerContext ctx, int statusCode, String reasonText) {
ctx.writeAndFlush(serverClosingConnectionMessage(statusCode, reasonText))
.addListener(ChannelFutureListener.CLOSE);
}
代码示例来源:origin: Netflix/zuul
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
int status = 500;
final String errorMsg = "ClientResponseWriter caught exception in client connection pipeline: " +
ChannelUtils.channelInfoForLogging(ctx.channel());
if (cause instanceof ZuulException) {
final ZuulException ze = (ZuulException) cause;
status = ze.getStatusCode();
LOG.error(errorMsg, cause);
}
else if (cause instanceof ReadTimeoutException) {
LOG.error(errorMsg + ", Read timeout fired");
status = 504;
}
else {
LOG.error(errorMsg, cause);
}
if (isHandlingRequest && !startedSendingResponseToClient && ctx.channel().isActive()) {
final HttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(status));
ctx.writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE);
startedSendingResponseToClient = true;
}
else {
ctx.close();
}
}
代码示例来源:origin: Graylog2/graylog2-server
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
connections.incrementAndGet();
totalConnections.incrementAndGet();
ctx.channel().closeFuture().addListener(f -> connections.decrementAndGet());
super.channelActive(ctx);
}
代码示例来源:origin: apache/pulsar
public CompletableFuture<MessageIdData> sendGetLastMessageId(ByteBuf request, long requestId) {
CompletableFuture<MessageIdData> future = new CompletableFuture<>();
pendingGetLastMessageIdRequests.put(requestId, future);
ctx.writeAndFlush(request).addListener(writeFuture -> {
if (!writeFuture.isSuccess()) {
log.warn("{} Failed to send GetLastMessageId request to broker: {}", ctx.channel(), writeFuture.cause().getMessage());
pendingGetLastMessageIdRequests.remove(requestId);
future.completeExceptionally(writeFuture.cause());
}
});
return future;
}
内容来源于网络,如有侵权,请联系作者删除!