@Override public void start() throws KairosDBException { // Configure the server. serverBootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("telnet-boss-%d").build()), Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("telnet-worker-%d").build()))); // Configure the pipeline factory. serverBootstrap.setPipelineFactory(this); serverBootstrap.setOption("child.tcpNoDelay", true); serverBootstrap.setOption("child.keepAlive", true); serverBootstrap.setOption("reuseAddress", true); // Bind and start to accept incoming connections. serverBootstrap.bind(new InetSocketAddress(address, port)); }
/** * Closes the Netty Channel and releases all resources */ @Override protected void stopInner() { /* * TODO REST-Server Need to handle inflight operations. What happens to * the existing async operations when a channel.close() is issued in * Netty? */ if(this.nettyServerChannel != null) { this.nettyServerChannel.close(); } if(allChannels != null) { allChannels.close().awaitUninterruptibly(); } this.bootstrap.releaseExternalResources(); }
NettyServerCnxnFactory() { bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // parent channel bootstrap.setOption("reuseAddress", true); // child channels bootstrap.setOption("child.tcpNoDelay", true); /* set socket linger to off, so that socket close does not block */ bootstrap.setOption("child.soLinger", -1); bootstrap.getPipeline().addLast("servercnxnfactory", channelHandler); }
this.channelFactory = channelFactory; this.executionHandler = executionHandler; ServerBootstrap bootstrap = new ServerBootstrap(channelFactory); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { serverChannel = bootstrap.bind(addr); allChannels.add(serverChannel);
public void start() throws Exception { ServerBootstrap bootstrap = new ServerBootstrap(selector); // Timer is shared across entire factory and must be released separately timer = new HashedWheelTimer(); try { pipelineFact = new HttpPipelineFactory(conf, timer); } catch (Exception ex) { throw new RuntimeException(ex); } bootstrap.setPipelineFactory(pipelineFact); bootstrap.setOption("backlog", NetUtil.SOMAXCONN); port = conf.getInt(SHUFFLE_PORT_CONFIG_KEY, DEFAULT_SHUFFLE_PORT); Channel ch = bootstrap.bind(new InetSocketAddress(port)); accepted.add(ch); port = ((InetSocketAddress)ch.getLocalAddress()).getPort(); conf.set(SHUFFLE_PORT_CONFIG_KEY, Integer.toString(port)); pipelineFact.SHUFFLE.setPort(port); if (dirWatcher != null) { dirWatcher.start(); } LOG.info("LlapShuffleHandler" + " listening on port " + port + " (SOMAXCONN: " + bootstrap.getOption("backlog") + ")"); }
@Override protected void startInner() { // Configure the server. this.workerPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(config.getNumRestServiceNettyWorkerThreads()); this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newFixedThreadPool(config.getNumRestServiceNettyBossThreads()), workerPool)); this.bootstrap.setOption("backlog", config.getRestServiceNettyServerBacklog()); this.bootstrap.setOption("child.tcpNoDelay", true); this.bootstrap.setOption("child.keepAlive", true); this.bootstrap.setOption("child.reuseAddress", true); this.bootstrap.setPipelineFactory(new RestPipelineFactory(storeRepository, config, localZoneId, storeDefinitions, allChannels)); // Bind and start to accept incoming connections. this.nettyServerChannel = this.bootstrap.bind(new InetSocketAddress(this.port)); allChannels.add(nettyServerChannel); logger.info("REST service started on port " + this.port); // Register MBeans for Netty worker pool stats if(config.isJmxEnabled()) { JmxUtils.registerMbean(this, JmxUtils.createObjectName(JmxUtils.getPackageName(this.getClass()), JmxUtils.getClassName(this.getClass()))); } }
@Override public synchronized boolean open() { if (isAvailable()) { LoggerUtil.warn("NettyServer ServerChannel already Open: url=" + url); return true; } LoggerUtil.info("NettyServer ServerChannel start Open: url=" + url); initServerBootstrap(); serverChannel = bootstrap.bind(new InetSocketAddress(url.getPort())); state = ChannelState.ALIVE; StatsUtil.registryStatisticCallback(this); LoggerUtil.info("NettyServer ServerChannel finish Open: url=" + url); return state.isAliveState(); }
@SuppressWarnings({"UnnecessaryBoxing"}) public void start(final NettyTransportProvider provider) { pipelineFactory = new ServerPipelineFactory(provider); bootstrap.setPipelineFactory(pipelineFactory); bootstrap.setOption("child.tcpNoDelay", Boolean.valueOf(true)); bootstrap.setOption("child.keepAlive", Boolean.valueOf(true)); channel = bootstrap.bind(new InetSocketAddress(0)); InetAddress inetAddress; try { inetAddress = InetAddress.getLocalHost(); } catch (UnknownHostException e) { // inetAddress = ((InetSocketAddress) channel.getLocalAddress()).getAddress(); } address = new InetSocketAddress(inetAddress, ((InetSocketAddress) channel.getLocalAddress()).getPort()); }
public boolean start() { Executor bossPool = Executors.newCachedThreadPool(); Executor workerPool = Executors.newCachedThreadPool(); ChannelFactory factory = new NioServerSocketChannelFactory(bossPool, workerPool); this.bootstrap = new ServerBootstrap(factory); new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()); this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() { Channel acceptor = this.bootstrap.bind(new InetSocketAddress(12345)); if (acceptor.isBound()) { System.err.println("+++ SERVER - bound to *:12345"); this.channelGroup.add(acceptor); return true; } else { System.err.println("+++ SERVER - Failed to bind to *:12345"); this.bootstrap.releaseExternalResources(); return false;
public void start(ServerChannelFactory serverChannelFactory) { bootstrap = new ServerBootstrap(serverChannelFactory); bootstrap.setOptions(nettyServerConfig.getBootstrapOptions()); bootstrap.setPipelineFactory(pipelineFactory); serverChannel = bootstrap.bind(new InetSocketAddress(requestedPort)); InetSocketAddress actualSocket = (InetSocketAddress) serverChannel.getLocalAddress(); actualPort = actualSocket.getPort(); Preconditions.checkState(actualPort != 0 && (actualPort == requestedPort || requestedPort == 0)); log.info("started transport %s:%s", def.getName(), actualPort); if (def.getTransportAttachObserver() != null) { def.getTransportAttachObserver().attachTransport(this); } }
@Test public void testNettyConfigBuilder() { NettyServerConfigBuilder configBuilder = new NettyServerConfigBuilder(); configBuilder.getServerSocketChannelConfig().setReceiveBufferSize(10000); configBuilder.getServerSocketChannelConfig().setBacklog(1000); configBuilder.getServerSocketChannelConfig().setReuseAddress(true); ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory()); bootstrap.setOptions(configBuilder.getBootstrapOptions()); bootstrap.setPipelineFactory(Channels.pipelineFactory(Channels.pipeline())); Channel serverChannel = bootstrap.bind(new InetSocketAddress(port)); Assert.assertEquals(((ServerSocketChannelConfig) serverChannel.getConfig()).getReceiveBufferSize(), 10000); Assert.assertEquals(((ServerSocketChannelConfig) serverChannel.getConfig()).getBacklog(), 1000); Assert.assertTrue(((ServerSocketChannelConfig) serverChannel.getConfig()).isReuseAddress()); } }
public Bootstrap createServerBootstrap() { // TODO The thread pools should be injected from spring. serverBootstrap = new ServerBootstrap( new NioServerSocketChannelFactory(Executors .newFixedThreadPool(1,new NamedThreadFactory( "Flash-Server-Boss")), Executors .newFixedThreadPool(1,new NamedThreadFactory( "Flash-Server-Worker")))); return serverBootstrap; }
@Override protected void doResume() throws Exception { if (channel != null) { LOG.debug("ServerBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort()); ChannelFuture future = channel.bind(new InetSocketAddress(configuration.getHost(), configuration.getPort())); future.awaitUninterruptibly(); if (!future.isSuccess()) { // if we cannot bind, the re-create channel allChannels.remove(channel); channel = serverBootstrap.bind(new InetSocketAddress(configuration.getHost(), configuration.getPort())); allChannels.add(channel); } } }
@Override public void start() { boundedChannel = bootstrap.bind(new InetSocketAddress(port)); allChannels.add(boundedChannel); }
private InetSocketTransportAddress bindAddress(final InetAddress hostAddress) { final AtomicReference<Exception> lastException = new AtomicReference<>(); final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>(); boolean success = port.iterate(portNumber -> { try { synchronized (serverChannels) { Channel channel = serverBootstrap.bind(new InetSocketAddress(hostAddress, portNumber)); serverChannels.add(channel); boundSocket.set((InetSocketAddress) channel.getLocalAddress()); } } catch (Exception e) { lastException.set(e); return false; } return true; }); if (!success) { throw new BindHttpException("Failed to bind to [" + port.getPortRangeString() + "]", lastException.get()); } if (logger.isDebugEnabled()) { logger.debug("Bound http to address {{}}", NetworkAddress.format(boundSocket.get())); } return new InetSocketTransportAddress(boundSocket.get()); }
bootstrap = new ServerBootstrap(); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ScheduledExecutorService staleCheckExecutor = Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory("WEBBIT-STALE-CONNECTION-CHECK-THREAD")); staleCheckExecutor.scheduleWithFixedDelay(new Runnable() { @Override ExecutorService bossExecutor = Executors.newSingleThreadExecutor(new NamingThreadFactory("WEBBIT-BOSS-THREAD")); executorServices.add(bossExecutor); ExecutorService workerExecutor = Executors.newSingleThreadExecutor(new NamingThreadFactory("WEBBIT-WORKER-THREAD")); executorServices.add(workerExecutor); bootstrap.setFactory(new NioServerSocketChannelFactory(bossExecutor, workerExecutor, 1)); channel = bootstrap.bind(socketAddress); return NettyWebServer.this;
/** * Create a server for this cluster. * @param cluster */ public GossipServer(GossipCluster cluster) { this.cluster = cluster; serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newFixedThreadPool(1), Executors.newFixedThreadPool(8))); serverBootstrap.setPipelineFactory(new GossipPipelineFactory(cluster,false)); clientBootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newFixedThreadPool(1), Executors.newFixedThreadPool(8))); clientBootstrap.setPipelineFactory(new GossipPipelineFactory(cluster,true)); }
public Bootstrap createServerBootstrap() { // TODO The thread pools should be injected from spring. serverBootstrap = new ServerBootstrap( new NioServerSocketChannelFactory(Executors .newCachedThreadPool(new NamedThreadFactory( "TCP-Server-Boss")), Executors .newCachedThreadPool(new NamedThreadFactory( "TCP-Server-Worker")))); return serverBootstrap; }
if (channel != null) { channel.close(); if (bootstrap != null) { bootstrap.releaseExternalResources();
protected void stop() throws Exception { accepted.close().awaitUninterruptibly(10, TimeUnit.SECONDS); if (selector != null) { ServerBootstrap bootstrap = new ServerBootstrap(selector); bootstrap.releaseExternalResources(); } if (pipelineFact != null) { pipelineFact.destroy(); } if (timer != null) { // Release this shared timer resource timer.stop(); } if (dirWatcher != null) { dirWatcher.stop(); } }