List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.lampard.netty4.protocol.netty.server.NettyServer.java
License:Apache License
public void bind() throws Exception { // ??NIO/*from w w w . ja va 2 s.c o m*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws IOException { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast(new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast(new LoginAuthRespHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler()); } }); // ??? b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync(); LOG.info("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT)); }
From source file:com.lb.mysession.server.TestPro.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w. java2 s. c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ProtobufServerInitializer()); Channel ch = b.bind(port).sync().channel(); System.out.println("Web socket server started at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.lb.netty.protoc.SubReqServer.java
License:Apache License
public void bind(int port) throws Exception { // ??NIO//from w ww. j av a 2 s.co m EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); // protobuffer ch.pipeline().addLast( new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance())); ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); ch.pipeline().addLast(new ProtobufEncoder()); ch.pipeline().addLast(new SubReqServerHandler()); } }); // ??? ChannelFuture f = b.bind(port).sync(); // ??? f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.leadtone.riders.server.RidersWebSocketServer.java
License:Apache License
public void start() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww .ja v a2 s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(serverInitializer); Channel ch = b.bind(port).sync().channel(); log.info("Web socket server started at port " + port + '.'); log.info("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.liferay.nativity.control.findersync.FSNativityControlImpl.java
License:Open Source License
@Override public boolean connect() { if (_connected) { return true; }/* w w w .j ava2 s . co m*/ _childEventLoopGroup = new NioEventLoopGroup(); _parentEventLoopGroup = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(_parentEventLoopGroup, _childEventLoopGroup); serverBootstrap.channel(NioServerSocketChannel.class); ChannelInitializer channelInitializer = new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { DelimiterBasedFrameDecoder messageDecoder = new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()); FinderSyncChannelHandler finderSyncChannelHandler = new FinderSyncChannelHandler(); socketChannel.pipeline().addLast(messageDecoder, finderSyncChannelHandler); } }; serverBootstrap.childHandler(channelInitializer); serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture channelFuture = serverBootstrap.bind(0).sync(); InetSocketAddress inetSocketAddress = (InetSocketAddress) channelFuture.channel().localAddress(); _writePortToFile(inetSocketAddress.getPort()); } catch (Exception e) { _logger.error(e.getMessage(), e); _connected = false; return false; } _connected = true; return true; }
From source file:com.liferay.sync.engine.lan.server.file.LanFileServer.java
License:Open Source License
public void start() throws Exception { _childEventLoopGroup = new NioEventLoopGroup(); _parentEventLoopGroup = new NioEventLoopGroup(1); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(_parentEventLoopGroup, _childEventLoopGroup); serverBootstrap.channel(NioServerSocketChannel.class); _syncTrafficShapingHandler = new SyncTrafficShapingHandler(_childEventLoopGroup); _lanFileServerInitializer = new LanFileServerInitializer(_syncTrafficShapingHandler); serverBootstrap.childHandler(_lanFileServerInitializer); serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture channelFuture = serverBootstrap.bind(PropsValues.SYNC_LAN_SERVER_PORT); try {/*from w w w. j a va 2 s. co m*/ channelFuture.sync(); } catch (Exception e) { // Compiling fails when directly catching BindException. Netty seems // to throw an undeclared exception. if (e instanceof BindException) { channelFuture = serverBootstrap.bind(0); channelFuture.sync(); } else { throw e; } } Channel channel = channelFuture.channel(); InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.localAddress(); _port = inetSocketAddress.getPort(); channelFuture.sync(); Runnable runnable = new Runnable() { @Override public void run() { long count = SyncFileService.getSyncFilesCount(SyncFile.UI_EVENT_DOWNLOADING, SyncFile.UI_EVENT_UPLOADING); long writeDelay = 0; if (count > 0) { _syncTrafficShapingHandler.setWriteDelay(PropsValues.SYNC_LAN_SERVER_WRITE_DELAY); } _syncTrafficShapingHandler.setWriteDelay(writeDelay); } }; ScheduledExecutorService scheduledExecutorService = LanEngine.getScheduledExecutorService(); scheduledExecutorService.scheduleWithFixedDelay(runnable, 0, 500, TimeUnit.MILLISECONDS); }
From source file:com.lin.studytest.netty.server.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*w w w . jav a 2s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // .option(ChannelOption.SO_BACKLOG, 100) // .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.linecorp.armeria.server.Server.java
License:Apache License
private ChannelFuture start(ServerPort port) { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(/*from ww w. j av a 2 s.com*/ NativeLibraries.isEpollAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class); b.childHandler(new HttpServerPipelineConfigurator(config, port, sslContexts, Optional.ofNullable(gracefulShutdownHandler))); return b.bind(port.localAddress()); }
From source file:com.linkedin.mitm.proxy.ProxyServer.java
License:Open Source License
/** * Start proxy server//w ww .ja va 2s.c o m * */ public void start() throws InterruptedException { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(_acceptorGroup, _upstreamWorkerGroup); serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() { @Override public ServerChannel newChannel() { return new NioServerSocketChannel(); } }); serverBootstrap.childHandler(new ProxyInitializer(this)); //bind ChannelFuture future = serverBootstrap.bind(_host, _port); //wait for the future future.awaitUninterruptibly(); if (!future.isSuccess()) { future.channel().closeFuture().awaitUninterruptibly(); throw new ChannelException(String.format("Failed to bind to: %s:%d", _host, _port), future.cause()); } else { _allChannels.add(future.channel()); } }
From source file:com.linkedin.pinot.transport.netty.NettyTCPServer.java
License:Apache License
@Override protected ServerBootstrap getServerBootstrap() { ServerBootstrap b = new ServerBootstrap(); b.group(_bossGroup, _workerGroup).channel(NioServerSocketChannel.class) .childHandler(createChannelInitializer()).option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); return b;/*from ww w. j a v a 2 s.c o m*/ }