Search in sources :

Example 21 with ChannelFutureListener

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project cdap by caskdata.

the class HttpRequestRouter method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    try {
        final Channel inboundChannel = ctx.channel();
        ChannelFutureListener writeCompletedListener = getFailureResponseListener(inboundChannel);
        if (msg instanceof HttpRequest) {
            inflightRequests++;
            if (inflightRequests != 1) {
                // At the end of the first response, we'll respond to all the other requests as well
                return;
            }
            // Disable read until sending of this request object is completed successfully
            // This is for handling the initial connection delay
            inboundChannel.config().setAutoRead(false);
            writeCompletedListener = new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        inboundChannel.config().setAutoRead(true);
                    } else {
                        getFailureResponseListener(inboundChannel).operationComplete(future);
                    }
                }
            };
            HttpRequest request = (HttpRequest) msg;
            currentMessageSender = getMessageSender(inboundChannel, getDiscoverable(request, (InetSocketAddress) inboundChannel.localAddress()));
        }
        if (inflightRequests == 1 && currentMessageSender != null) {
            ReferenceCountUtil.retain(msg);
            currentMessageSender.send(msg, writeCompletedListener);
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SSLException(javax.net.ssl.SSLException) HandlerException(co.cask.cdap.common.HandlerException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 22 with ChannelFutureListener

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project elephant by yanghuijava.

the class RemotingUtil method closeChannel.

public static void closeChannel(Channel channel) {
    final String addrRemote = RemotingHelper.parseChannelRemoteAddr(channel);
    channel.close().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            log.info("closeChannel: close the connection to remote address[{}] result: {}", addrRemote, future.isSuccess());
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 23 with ChannelFutureListener

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project modules-extra by CubeEngine.

the class WebSocketRequestHandler method doHandshake.

public void doHandshake(ChannelHandlerContext ctx, FullHttpRequest message) {
    WebSocketServerHandshakerFactory handshakerFactory = new WebSocketServerHandshakerFactory("ws://" + message.headers().get(HOST) + "/" + WEBSOCKET_ROUTE, null, false);
    this.handshaker = handshakerFactory.newHandshaker(message);
    if (handshaker == null) {
        this.log.info("client is incompatible!");
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        return;
    }
    this.log.debug("handshaking now...");
    this.handshaker.handshake(ctx.channel(), message).addListener((ChannelFutureListener) future -> {
        if (future.isSuccess()) {
            log.debug("Success!");
        } else {
            log.debug("Failed!");
        }
    });
}
Also used : WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) User(org.spongepowered.api.entity.living.player.User) EMPTY_HEADERS(io.netty.handler.codec.http.HttpHeaders.EMPTY_HEADERS) HOST(io.netty.handler.codec.http.HttpHeaders.Names.HOST) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CommandManager(org.cubeengine.libcube.service.command.CommandManager) Charset(java.nio.charset.Charset) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) ChannelFutureListener(io.netty.channel.ChannelFutureListener) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) JsonNode(com.fasterxml.jackson.databind.JsonNode) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) Log(org.cubeengine.logscribe.Log) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)

Example 24 with ChannelFutureListener

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project rocketmq by apache.

the class Broker2Client method checkProducerTransactionState.

public void checkProducerTransactionState(final Channel channel, final CheckTransactionStateRequestHeader requestHeader, final SelectMappedBufferResult selectMappedBufferResult) {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.markOnewayRPC();
    try {
        FileRegion fileRegion = new OneMessageTransfer(request.encodeHeader(selectMappedBufferResult.getSize()), selectMappedBufferResult);
        channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                selectMappedBufferResult.release();
                if (!future.isSuccess()) {
                    log.error("invokeProducer failed,", future.cause());
                }
            }
        });
    } catch (Throwable e) {
        log.error("invokeProducer exception", e);
        selectMappedBufferResult.release();
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ChannelFuture(io.netty.channel.ChannelFuture) OneMessageTransfer(org.apache.rocketmq.broker.pagecache.OneMessageTransfer) FileRegion(io.netty.channel.FileRegion) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException)

Example 25 with ChannelFutureListener

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFutureListener in project rocketmq by apache.

the class NettyRemotingAbstract method invokeOnewayImpl.

public void invokeOnewayImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
    request.markOnewayRPC();
    boolean acquired = this.semaphoreOneway.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
    if (acquired) {
        final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreOneway);
        try {
            channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    once.release();
                    if (!f.isSuccess()) {
                        log.warn("send a request command to channel <" + channel.remoteAddress() + "> failed.");
                    }
                }
            });
        } catch (Exception e) {
            once.release();
            log.warn("write send a request command to channel <" + channel.remoteAddress() + "> failed.");
            throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
        }
    } else {
        if (timeoutMillis <= 0) {
            throw new RemotingTooMuchRequestException("invokeOnewayImpl invoke too fast");
        } else {
            String info = String.format("invokeOnewayImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", timeoutMillis, this.semaphoreOneway.getQueueLength(), this.semaphoreOneway.availablePermits());
            log.warn(info);
            throw new RemotingTimeoutException(info);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SemaphoreReleaseOnlyOnce(org.apache.rocketmq.remoting.common.SemaphoreReleaseOnlyOnce) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException)

Aggregations

ChannelFutureListener (io.netty.channel.ChannelFutureListener)223 ChannelFuture (io.netty.channel.ChannelFuture)208 Channel (io.netty.channel.Channel)70 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)57 ByteBuf (io.netty.buffer.ByteBuf)49 Bootstrap (io.netty.bootstrap.Bootstrap)43 Test (org.junit.jupiter.api.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)36 IOException (java.io.IOException)35 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)33 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)31 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)31 InetSocketAddress (java.net.InetSocketAddress)27 ClosedChannelException (java.nio.channels.ClosedChannelException)25 ChannelPromise (io.netty.channel.ChannelPromise)21 Logger (org.slf4j.Logger)21 LoggerFactory (org.slf4j.LoggerFactory)21 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)20 EventLoopGroup (io.netty.channel.EventLoopGroup)18 List (java.util.List)17