/** * Build the ThriftServerDef */ public ThriftServerDef build() { checkState(processor != null, "Processor not defined!"); checkState(maxConnections >= 0, "maxConnections should be 0 (for unlimited) or positive"); if (executor == null) { executor = new DefaultExecutorServiceFactory("NettyThrift") .newExecutorService(Runtime.getRuntime().availableProcessors() + 1); } if (protocolFactorySelectorFactory == null) { protocolFactorySelectorFactory = new DefaultProtocolFactorySelectorFactory(); } if (httpResourceHandler == null) { httpResourceHandler = new HttpFileResourceHandler(); } return new ThriftServerDef(name, serverPort, maxFrameSize, maxConnections, queuedResponseLimit, nettyProcessorFactory, contextHandlerInstaller, processor, executor, clientIdleTimeout, protocolFactorySelectorFactory, httpResourceHandler, voidMethodDirectReturn, httpHandlerFactory, trafficForecastFactory, logicExecutionStatistics); }
public NettyConnector(InetSocketAddress isa, final TransportConfig transportConfig) { workerGroup = new NioEventLoopGroup(1, new DefaultExecutorServiceFactory("N5C-Work")); clientBoot = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class); clientBoot.option(ChannelOption.TCP_NODELAY, true); clientBoot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, transportConfig.getConnectTimeout()); clientBoot.option(ChannelOption.SO_RCVBUF, 8 * 1024).option(ChannelOption.SO_SNDBUF, 8 * 1024); clientBoot.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(0, intervalSeconds, 0)); ch.pipeline().addLast("NettyClientHandler", new NettyClientHandler()); } }); clientBoot.remoteAddress(isa); }
@Override public Acceptance bind() { bossGroup = new NioEventLoopGroup(1, new DefaultExecutorServiceFactory("N5A-Boss")); workGroup = new NioEventLoopGroup(transportConfig.getNioProcessorCount(), new DefaultExecutorServiceFactory("N5A-Work")); ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(intervalSeconds * 2, 0, 0)); ch.pipeline().addLast("NettyServerHandler", new NettyServerHandler()); } }); boot.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_RCVBUF, 16 * 1024).childOption(ChannelOption.SO_SNDBUF, 16 * 1024) .childOption(ChannelOption.TCP_NODELAY, true); try { boot.bind(serviceAddress).sync(); int processorCount = transportConfig.getBusinessProcessorCount(); if (processorCount > 0) { NamedThreadFactory threadFactory = new NamedThreadFactory("N5A-BusinssProcessor"); executorService = Executors.newFixedThreadPool(processorCount, threadFactory); } } catch (Exception e) { throw new RuntimeException("can't bind service on " + serviceAddress, e); } LOG.info("netty5 acceptance bind on {}", serviceAddress); return this; }
@Override protected EventLoopGroup createEventLoopGroup() { return new NioEventLoopGroup(0, new DefaultExecutorServiceFactory(name)); }