List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap
public ServerBootstrap()
From source file:cn.david.socks.SocksServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w.j a va 2s. c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new SocksServerInitializer()); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:cn.dennishucd.nettyhttpserver.HttpServer.java
License:Apache License
public void start(String host, int port) throws Exception { //create a reactor thread pool for NIO acceptor EventLoopGroup bossGroup = new NioEventLoopGroup(); //create a reactor thread pool for NIO handler EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww . j ava2 s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new PushServerInitializer()); ChannelFuture f = b.bind(host, port).sync(); logger.info("The http server is started on " + host + ":" + port); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:cn.npt.net.websocket.WebSocketServer.java
License:Apache License
private void init(int port, boolean ssl, String webSocketPath) throws Exception { final SslContext sslCtx; if (this.ssl) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else {/*from ww w .j a v a2 s.com*/ sslCtx = null; } bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new WebSocketServerInitializer(sslCtx, webSocketPath, ssl)); Channel ch = b.bind(this.port).sync().channel(); ch.closeFuture().sync();// } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } System.out.println("start websocket..."); }
From source file:cn.wantedonline.puppy.Bootstrap.java
License:Apache License
/** * ?HttpServerBootstrap,??/* w w w . j a v a 2 s. co m*/ */ private ServerBootstrap initHttpServerBootstrap() { EventLoopGroup bossGroup = httpServerConfig.getBossEventLoopGroup(); NioEventLoopGroup workerGroup = httpServerConfig.getWorkerEventLoopGroup(); nioWorkerStat.registerWorkers(workerGroup); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(httpServerConfig.getHttpServerHandler()); return serverBootstrap; }
From source file:cn.wcl.test.netty.server.netty.NettyHttpServer.java
License:Apache License
private void init() throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// w w w .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(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.childHandler(new HttpUploadServerInitializer(sslCtx, applicationContext, needlogin)); Channel ch = b.bind(port).sync().channel(); System.err.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:cn.yesway.demo.book.protocol.http.fileServer.HttpFileServer.java
License:Apache License
public void run(final int port, final String url) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w. jav a 2 s . com*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url)); } }); ChannelFuture future = b.bind("127.0.0.1", port).sync(); System.out.println("HTTP??? : " + "http://127.0.0.1:" + port + url); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:cn.yesway.demo.book.protocol.http.xml.server.HttpXmlServer.java
License:Apache License
public void run(final int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w w w .j a va2 s .c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("xml-decoder", new HttpXmlRequestDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder()); ch.pipeline().addLast("xmlServerHandler", new HttpXmlServerHandler()); } }); ChannelFuture future = b.bind(new InetSocketAddress(port)).sync(); System.out.println("HTTP??? : " + "http://localhost:" + port); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:cn.zyf.ObjectStorageServer.java
License:Open Source License
private void run(String host, int port, int packageSize) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); EventLoopGroup backendGroup = new NioEventLoopGroup(); try {/*from w w w .j a va2 s. c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(packageSize)); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("oss-decoder", new ParseRequestHandler()); ch.pipeline().addLast("oss-filter", new FilterRequestHandler()); ch.pipeline().addLast("oss-authen", new AuthenticationHandler()); ch.pipeline().addLast("oss-author", new AuthorizationHandler()); ch.pipeline().addLast(backendGroup, "oss-backend", new ObjectStorageHandler()); } }); ChannelFuture future = b.bind(host, port).sync(); LOG.info("start Http Server with hostname=" + host + ":" + port); future.channel().closeFuture().sync(); } catch (InterruptedException e) { LOG.error("occur InterruptedException ", e); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:co.paralleluniverse.comsat.webactors.netty.WebActorTest.java
License:Open Source License
@Before public void setUp() throws InterruptedException, IOException { System.out.println("Clearing sessions"); WebActorHandler.sessions.clear();// ww w. j a v a 2s .c om group = new NioEventLoopGroup(); final ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(HTTP_RESPONSE_ENCODER_KEY, new HttpResponseEncoder()); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(webActorHandlerCreatorInEffect.call()); } }); ch = b.bind(INET_PORT).sync(); AbstractEmbeddedServer.waitUrlAvailable("http://localhost:" + INET_PORT); System.err.println("Server is up"); }
From source file:co.rsk.rpc.netty.Web3WebSocketServer.java
License:Open Source License
public void start() throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override// w w w . ja v a 2 s . c om protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); p.addLast(new WebSocketServerProtocolHandler("/websocket")); p.addLast(jsonRpcHandler); p.addLast(web3ServerHandler); p.addLast(new Web3ResultWebSocketResponseHandler()); } }); webSocketChannel = b.bind(host, port); webSocketChannel.sync(); }