Search in sources :

Example 1 with StandardThreadExecutor

use of com.weibo.api.motan.core.StandardThreadExecutor in project motan by weibocom.

the class NettyServer method open.

@Override
public boolean open() {
    if (isAvailable()) {
        LoggerUtil.warn("NettyServer ServerChannel already Open: url=" + url);
        return state.isAliveState();
    }
    if (bossGroup == null) {
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }
    LoggerUtil.info("NettyServer ServerChannel start Open: url=" + url);
    boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
    int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), URLParamType.workerQueueSize.getIntValue());
    int minWorkerThread, maxWorkerThread;
    if (shareChannel) {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
    } else {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
    }
    standardThreadExecutor = (standardThreadExecutor != null && !standardThreadExecutor.isShutdown()) ? standardThreadExecutor : new StandardThreadExecutor(minWorkerThread, maxWorkerThread, workerQueueSize, new DefaultThreadFactory("NettyServer-" + url.getServerPortStr(), true));
    standardThreadExecutor.prestartAllCoreThreads();
    channelManage = new NettyServerChannelManage(maxServerConnection);
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("channel_manage", channelManage);
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyServer.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder());
            NettyChannelHandler handler = new NettyChannelHandler(NettyServer.this, messageHandler, standardThreadExecutor);
            pipeline.addLast("handler", handler);
        }
    });
    serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(url.getPort()));
    channelFuture.syncUninterruptibly();
    serverChannel = channelFuture.channel();
    setLocalAddress((InetSocketAddress) serverChannel.localAddress());
    if (url.getPort() == 0) {
        url.setPort(getLocalAddress().getPort());
    }
    state = ChannelState.ALIVE;
    StatsUtil.registryStatisticCallback(this);
    LoggerUtil.info("NettyServer ServerChannel finish Open: url=" + url);
    return state.isAliveState();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) StandardThreadExecutor(com.weibo.api.motan.core.StandardThreadExecutor) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) TransportException(com.weibo.api.motan.transport.TransportException) DefaultThreadFactory(com.weibo.api.motan.core.DefaultThreadFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with StandardThreadExecutor

use of com.weibo.api.motan.core.StandardThreadExecutor in project motan by weibocom.

the class GrpcProtocol method createExporter.

@Override
protected <T> Exporter<T> createExporter(Provider<T> provider, URL url) {
    String ipPort = url.getServerPortStr();
    GrpcServer server = serverMap.get(ipPort);
    if (server == null) {
        boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
        int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), URLParamType.workerQueueSize.getIntValue());
        int minWorkerThread = 0, maxWorkerThread = 0;
        if (shareChannel) {
            minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
            maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
        } else {
            minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
            maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
        }
        ExecutorService executor = new StandardThreadExecutor(minWorkerThread, maxWorkerThread, workerQueueSize, new DefaultThreadFactory("GrpcServer-" + url.getServerPortStr(), true));
        server = new GrpcServer(url.getPort(), shareChannel, executor);
        serverMap.putIfAbsent(ipPort, server);
        server = serverMap.get(ipPort);
    }
    return new GrpcExporter<T>(provider, url, server);
}
Also used : DefaultThreadFactory(com.weibo.api.motan.core.DefaultThreadFactory) StandardThreadExecutor(com.weibo.api.motan.core.StandardThreadExecutor) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

DefaultThreadFactory (com.weibo.api.motan.core.DefaultThreadFactory)2 StandardThreadExecutor (com.weibo.api.motan.core.StandardThreadExecutor)2 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 TransportException (com.weibo.api.motan.transport.TransportException)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutorService (java.util.concurrent.ExecutorService)1