Search in sources :

Example 96 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method pollPooledChannel.

private Channel pollPooledChannel(Request request, ProxyServer proxy, AsyncHandler<?> asyncHandler) {
    try {
        asyncHandler.onConnectionPoolAttempt();
    } catch (Exception e) {
        LOGGER.error("onConnectionPoolAttempt crashed", e);
    }
    Uri uri = request.getUri();
    String virtualHost = request.getVirtualHost();
    final Channel channel = channelManager.poll(uri, virtualHost, proxy, request.getChannelPoolPartitioning());
    if (channel != null) {
        LOGGER.debug("Using pooled Channel '{}' for '{}' to '{}'", channel, request.getMethod(), uri);
    }
    return channel;
}
Also used : Channel(io.netty.channel.Channel) Uri(org.asynchttpclient.uri.Uri) FilterException(org.asynchttpclient.filter.FilterException) IOException(java.io.IOException) PoolAlreadyClosedException(org.asynchttpclient.exception.PoolAlreadyClosedException) RemotelyClosedException(org.asynchttpclient.exception.RemotelyClosedException)

Example 97 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method sendRequestThroughProxy.

/**
 * Using CONNECT depends on wither we can fetch a valid channel or not Loop
 * until we get a valid channel from the pool and it's still valid once the
 * request is built @
 */
private <T> ListenableFuture<T> sendRequestThroughProxy(Request request, AsyncHandler<T> asyncHandler, NettyResponseFuture<T> future, ProxyServer proxyServer) {
    NettyResponseFuture<T> newFuture = null;
    for (int i = 0; i < 3; i++) {
        Channel channel = getOpenChannel(future, request, proxyServer, asyncHandler);
        if (channel == null) {
            // pool is empty
            break;
        }
        if (newFuture == null) {
            newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, false);
        }
        if (Channels.isChannelActive(channel)) {
            // otherwise, channel was closed by the time we computed the request, try again
            return sendRequestWithOpenChannel(newFuture, asyncHandler, channel);
        }
    }
    // couldn't poll an active channel
    newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, true);
    return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler);
}
Also used : CONNECT(org.asynchttpclient.util.HttpConstants.Methods.CONNECT) GET(org.asynchttpclient.util.HttpConstants.Methods.GET) EXPECT(io.netty.handler.codec.http.HttpHeaderNames.EXPECT) Channel(io.netty.channel.Channel)

Example 98 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project async-http-client by AsyncHttpClient.

the class NettyChannelConnector method connect0.

private void connect0(Bootstrap bootstrap, final NettyConnectListener<?> connectListener, InetSocketAddress remoteAddress) {
    bootstrap.connect(remoteAddress, localAddress).addListener(new SimpleChannelFutureListener() {

        @Override
        public void onSuccess(Channel channel) {
            try {
                asyncHandler.onTcpConnectSuccess(remoteAddress, channel);
            } catch (Exception e) {
                LOGGER.error("onTcpConnectSuccess crashed", e);
                connectListener.onFailure(channel, e);
                return;
            }
            connectListener.onSuccess(channel, remoteAddress);
        }

        @Override
        public void onFailure(Channel channel, Throwable t) {
            try {
                asyncHandler.onTcpConnectFailure(remoteAddress, t);
            } catch (Exception e) {
                LOGGER.error("onTcpConnectFailure crashed", e);
                connectListener.onFailure(channel, e);
                return;
            }
            boolean retry = pickNextRemoteAddress();
            if (retry) {
                NettyChannelConnector.this.connect(bootstrap, connectListener);
            } else {
                connectListener.onFailure(channel, t);
            }
        }
    });
}
Also used : Channel(io.netty.channel.Channel) SimpleChannelFutureListener(org.asynchttpclient.netty.SimpleChannelFutureListener) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 99 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultConsumerIdsChangeListener method handle.

@Override
public void handle(ConsumerGroupEvent event, String group, Object... args) {
    if (event == null) {
        return;
    }
    switch(event) {
        case CHANGE:
            if (args == null || args.length < 1) {
                return;
            }
            List<Channel> channels = (List<Channel>) args[0];
            if (channels != null && brokerController.getBrokerConfig().isNotifyConsumerIdsChangedEnable()) {
                for (Channel chl : channels) {
                    this.brokerController.getBroker2Client().notifyConsumerIdsChanged(chl, group);
                }
            }
            break;
        case UNREGISTER:
            this.brokerController.getConsumerFilterManager().unRegister(group);
            break;
        case REGISTER:
            if (args == null || args.length < 1) {
                return;
            }
            Collection<SubscriptionData> subscriptionDataList = (Collection<SubscriptionData>) args[0];
            this.brokerController.getConsumerFilterManager().register(group, subscriptionDataList);
            break;
        default:
            throw new RuntimeException("Unknown event " + event);
    }
}
Also used : Channel(io.netty.channel.Channel) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) Collection(java.util.Collection) List(java.util.List)

Example 100 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Broker2Client method getConsumeStatus.

public RemotingCommand getConsumeStatus(String topic, String group, String originClientId) {
    final RemotingCommand result = RemotingCommand.createResponseCommand(null);
    GetConsumerStatusRequestHeader requestHeader = new GetConsumerStatusRequestHeader();
    requestHeader.setTopic(topic);
    requestHeader.setGroup(group);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_CONSUMER_STATUS_FROM_CLIENT, requestHeader);
    Map<String, Map<MessageQueue, Long>> consumerStatusTable = new HashMap<String, Map<MessageQueue, Long>>();
    ConcurrentMap<Channel, ClientChannelInfo> channelInfoTable = this.brokerController.getConsumerManager().getConsumerGroupInfo(group).getChannelInfoTable();
    if (null == channelInfoTable || channelInfoTable.isEmpty()) {
        result.setCode(ResponseCode.SYSTEM_ERROR);
        result.setRemark(String.format("No Any Consumer online in the consumer group: [%s]", group));
        return result;
    }
    for (Map.Entry<Channel, ClientChannelInfo> entry : channelInfoTable.entrySet()) {
        int version = entry.getValue().getVersion();
        String clientId = entry.getValue().getClientId();
        if (version < MQVersion.Version.V3_0_7_SNAPSHOT.ordinal()) {
            result.setCode(ResponseCode.SYSTEM_ERROR);
            result.setRemark("the client does not support this feature. version=" + MQVersion.getVersionDesc(version));
            log.warn("[get-consumer-status] the client does not support this feature. version={}", RemotingHelper.parseChannelRemoteAddr(entry.getKey()), MQVersion.getVersionDesc(version));
            return result;
        } else if (UtilAll.isBlank(originClientId) || originClientId.equals(clientId)) {
            try {
                RemotingCommand response = this.brokerController.getRemotingServer().invokeSync(entry.getKey(), request, 5000);
                assert response != null;
                switch(response.getCode()) {
                    case ResponseCode.SUCCESS:
                        {
                            if (response.getBody() != null) {
                                GetConsumerStatusBody body = GetConsumerStatusBody.decode(response.getBody(), GetConsumerStatusBody.class);
                                consumerStatusTable.put(clientId, body.getMessageQueueTable());
                                log.info("[get-consumer-status] get consumer status success. topic={}, group={}, channelRemoteAddr={}", topic, group, clientId);
                            }
                        }
                    default:
                        break;
                }
            } catch (Exception e) {
                log.error("[get-consumer-status] get consumer status exception. topic={}, group={}, offset={}", new Object[] { topic, group }, e);
            }
            if (!UtilAll.isBlank(originClientId) && originClientId.equals(clientId)) {
                break;
            }
        }
    }
    result.setCode(ResponseCode.SUCCESS);
    GetConsumerStatusBody resBody = new GetConsumerStatusBody();
    resBody.setConsumerTable(consumerStatusTable);
    result.setBody(resBody.encode());
    return result;
}
Also used : ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) HashMap(java.util.HashMap) Channel(io.netty.channel.Channel) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) GetConsumerStatusBody(org.apache.rocketmq.common.protocol.body.GetConsumerStatusBody) GetConsumerStatusRequestHeader(org.apache.rocketmq.common.protocol.header.GetConsumerStatusRequestHeader) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Aggregations

Channel (io.netty.channel.Channel)889 Test (org.junit.Test)242 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)227 ChannelFuture (io.netty.channel.ChannelFuture)204 Bootstrap (io.netty.bootstrap.Bootstrap)201 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)193 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)176 InetSocketAddress (java.net.InetSocketAddress)169 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)152 IOException (java.io.IOException)143 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)131 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)105 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)97 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)88