List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.metasoft.empire.net.websocket.WebSocketServer.java
License:Apache License
public static void start() throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from www . j a v a2 s . co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new WebSocketServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); //System.out.println("Open your web browser and navigate to " + //(SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.miko.s4netty.config.WorkerNettyConfig.java
License:Open Source License
@SuppressWarnings("unchecked") @Bean(name = "serverBootstrap") public ServerBootstrap bootstrap() { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup(), workerGroup()).channel(NioServerSocketChannel.class).childHandler(workerInitializer); Map<ChannelOption<?>, Object> tcpChannelOptions = tcpChannelOptions(); Set<ChannelOption<?>> keySet = tcpChannelOptions.keySet(); for (@SuppressWarnings("rawtypes") ChannelOption option : keySet) {/*from w w w . j av a 2s .co m*/ b.option(option, tcpChannelOptions.get(option)); } return b; }
From source file:com.mnxfst.stream.server.StreamAnalyzerServer.java
License:Apache License
public void run(final String configurationFilename, final int port) throws Exception { ObjectMapper mapper = new ObjectMapper(); StreamAnalyzerConfiguration streamAnalyzerConfiguration = mapper.readValue(new File(configurationFilename), StreamAnalyzerConfiguration.class); // set up the actor runtime environment this.rootActorSystem = ActorSystem.create("streamanalyzer"); this.componentRegistryRef = componentRegistryInitialization(); pipelineInitialization(streamAnalyzerConfiguration.getPipelines()); dispatcherInitialization(streamAnalyzerConfiguration.getDispatchers(), componentRegistryRef); listenerInitialization(streamAnalyzerConfiguration.getListeners(), componentRegistryRef); EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .ja va2 s .com*/ ServerBootstrap b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StreamAnalyzerStatsHandler()); } }).option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); // (7) // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.mpcc.springmvc.configuration.Start.java
public void run() throws Exception { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try {/* w w w . j a va2s .com*/ ServerBootstrap server = new ServerBootstrap(); server.group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new ServerInitializer()); Channel ch = server.bind(PORT).sync().channel(); ch.closeFuture().sync(); } finally { // boss.shutdownGracefully(); // worker.shutdownGracefully(); shutdownWorkers(boss, worker); } }
From source file:com.mpush.netty.server.NettyServer.java
License:Apache License
private void createServer(final Listener listener, EventLoopGroup boss, EventLoopGroup work, Class<? extends ServerChannel> clazz) { /***//w w w . j a va 2 s . c o m * NioEventLoopGroup ??I/O? * Netty????EventLoopGroup?????? * ?2NioEventLoopGroup * ???boss?? * ???worker??? * boss?worker * ???Channels??EventLoopGroup * ??? */ this.bossGroup = boss; this.workerGroup = work; try { /** * ServerBootstrap ?NIO?? * ??Channel */ ServerBootstrap b = new ServerBootstrap(); /** * groupjava.lang.IllegalStateException: group not set */ b.group(bossGroup, workerGroup); /*** * ServerSocketChannelNIOselector? * Channel?. */ b.channel(clazz); /*** * ?????Channel * ChannelInitializer? * ?Channel * ?NettyServerHandler??Channel * ChannelPipeline?? * ??????pipeline * ?????? */ b.childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { initPipeline(ch.pipeline()); } }); initOptions(b); /*** * ??? */ ChannelFuture f = b.bind(port).sync().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { Logs.Console.error("server start success on:{}", port); if (listener != null) listener.onSuccess(port); } else { Logs.Console.error("server start failure on:{}", port, future.cause()); if (listener != null) listener.onFailure(future.cause()); } } }); if (f.isSuccess()) { serverState.set(State.Started); /** * socket */ f.channel().closeFuture().sync(); } } catch (Exception e) { logger.error("server start exception", e); if (listener != null) listener.onFailure(e); throw new ServiceException("server start exception, port=" + port, e); } finally { /*** * */ stop(null); } }
From source file:com.mpush.netty.server.NettyTCPServer.java
License:Apache License
private void createServer(Listener listener, EventLoopGroup boss, EventLoopGroup work, ChannelFactory<? extends ServerChannel> channelFactory) { /***/*ww w. j a v a2 s.c o m*/ * NioEventLoopGroup ??I/O? * Netty????EventLoopGroup?????? * ?2NioEventLoopGroup * ???boss?? * ???worker??? * boss?worker * ???Channels??EventLoopGroup * ??? */ this.bossGroup = boss; this.workerGroup = work; try { /** * ServerBootstrap ?NIO?? * ??Channel */ ServerBootstrap b = new ServerBootstrap(); /** * groupjava.lang.IllegalStateException: group not set */ b.group(bossGroup, workerGroup); /*** * ServerSocketChannelNIOselector? * Channel?. */ b.channelFactory(channelFactory); /*** * ?????Channel * ChannelInitializer? * ?Channel * ?NettyServerHandler??Channel * ChannelPipeline?? * ??????pipeline * ?????? */ b.childHandler(new ChannelInitializer<Channel>() { // (4) @Override public void initChannel(Channel ch) throws Exception {//? initPipeline(ch.pipeline()); } }); initOptions(b); /*** * ??? */ b.bind(port).addListener(future -> { if (future.isSuccess()) { serverState.set(State.Started); logger.info("server start success on:{}", port); if (listener != null) listener.onSuccess(port); } else { logger.error("server start failure on:{}", port, future.cause()); if (listener != null) listener.onFailure(future.cause()); } }); } catch (Exception e) { logger.error("server start exception", e); if (listener != null) listener.onFailure(e); throw new ServiceException("server start exception, port=" + port, e); } }
From source file:com.my.netty.object.ObjectEchoServer.java
License:Apache License
public void run() throws Exception { ServerBootstrap b = new ServerBootstrap(); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* ww w . ja va2s.c o m*/ b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast(new ObjectEncoder()); cp.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null))); cp.addLast(new ObjectEchoServerHandler()); } }); // Bind and start to accept incoming connections. System.out.println("server prepare staring"); b.bind(port).sync().channel().closeFuture().sync(); System.out.println("server start ok"); } finally { System.out.println("server do finally"); workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); System.out.println("server closing ok"); } }
From source file:com.mycompany.ffserver.FFServer.java
@Override public void run() { FFDevice ff_device;// w w w . j a v a2s.c o m FFRequest ff_request; int dev_count; int req_count; NioEventLoopGroup bossGroup = new NioEventLoopGroup(); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { device_lst.add(new FFDevice(ch)); logger.info("have a new connnection"); } }); bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); try { bootstrap.bind(port).sync(); } catch (InterruptedException ex) { logger.error(ex.getMessage()); } ResultSet rs; try { rs = DbUtils.getDeviceList(); String reg_str; while (rs.next()) { reg_str = rs.getString("regs"); addRegDevice(reg_str); DbUtils.updateAllToOffline(); } } catch (ClassNotFoundException ex) { logger.error(ex.getMessage()); } catch (SQLException ex) { logger.error(ex.getMessage()); } while (!m_stop_flag) { dev_count = device_lst.size(); for (int i = dev_count - 1; i >= 0; i--) { ff_device = (FFDevice) device_lst.get(i); if (ff_device.isClosed()) { device_lst.remove(i); logger.info(String.format("remove device with reg_str '%s' from device_lst", ff_device.getRegStr())); continue; } if (ff_device.getRegStr().isEmpty()) { if (ff_device.connect_time + 5000 < System.currentTimeMillis()) { ff_device.Close(); logger.info("close connection of un-reg device"); } continue; } if (!dev_info.isAvailable(ff_device.getRegStr())) { ff_device.Close(); logger.info(String.format("close old connection of device that have reg_str '%s'", ff_device.getRegStr())); continue; } dev_info.lockRegStr(ff_device.getRegStr()); req_count = req_lst.size(); for (int j = 0; j < req_count; j++) { ff_request = req_lst.get(j); if (ff_request.reg_str.equals(ff_device.getRegStr()) && ff_device.req == null) { logger.info(String.format("send req to %s", ff_device.getRegStr())); ff_device.req = ff_request; req_lst.remove(j); break; } } ff_device.Process(); } dev_info.freeAllRegStr(); try { Thread.sleep(10); } catch (InterruptedException ex) { logger.error(ex.getMessage()); } } }
From source file:com.mycompany.nettyweb.HttpServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w.j a v a 2 s .co m*/ ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.option(ChannelOption.SO_BACKLOG, 1024); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new HttpServerInitializer()); Channel ch = bootstrap.bind(port).sync().channel(); System.out.println("Server started, port:" + port); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.myseti.framework.monitor.net.MonitorServer.java
@Override public void run() { try {/*w w w. ja v a2s . c o m*/ // Configure SSL. final SslContext sslCtx; if (SSL) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new MonitorServerHandler()).childHandler(new MonitorServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } catch (Exception ex) { } super.run(); //To change body of generated methods, choose Tools | Templates. }