List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap
public ServerBootstrap()
From source file:com.dinstone.rpc.netty.server.NettyServer.java
License:Apache License
public void start() { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); try {//from ww w. jav a 2s . c om ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new RpcProtocolDecoder(true)); ch.pipeline().addLast(new RpcProtocolEncoder(true)); ch.pipeline().addLast(new NettyServerHandler(handler)); } }); boot.option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_KEEPALIVE, true); int port = config.getInt(Constants.SERVICE_PORT, Constants.DEFAULT_SERVICE_PORT); InetSocketAddress localAddress = new InetSocketAddress(port); String host = config.get(Constants.SERVICE_HOST); if (host != null) { localAddress = new InetSocketAddress(host, port); } LOG.info("RPC service works on " + localAddress); boot.bind(localAddress).sync(); } catch (InterruptedException e) { throw new RpcException(500, "Server can't bind to the specified local address ", e); } }
From source file:com.diwayou.hybrid.remoting.netty.NettyRemotingServer.java
License:Apache License
public NettyRemotingServer(final NettyServerConfig nettyServerConfig, final ChannelEventListener channelEventListener) { super(nettyServerConfig.getServerOnewaySemaphoreValue(), nettyServerConfig.getServerAsyncSemaphoreValue()); this.serverBootstrap = new ServerBootstrap(); this.nettyServerConfig = nettyServerConfig; this.channelEventListener = channelEventListener; int publicThreadNums = nettyServerConfig.getServerCallbackExecutorThreads(); if (publicThreadNums <= 0) { publicThreadNums = 4;//from www . j a v a 2s.com } this.publicExecutor = Executors.newFixedThreadPool(publicThreadNums, new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "NettyServerPublicExecutor_" + this.threadIndex.incrementAndGet()); } }); this.eventLoopGroupBoss = new NioEventLoopGroup(1, new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, String.format("NettyBossSelector_%d", this.threadIndex.incrementAndGet())); } }); this.eventLoopGroupWorker = new NioEventLoopGroup(nettyServerConfig.getServerSelectorThreads(), new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); private int threadTotal = nettyServerConfig.getServerSelectorThreads(); @Override public Thread newThread(Runnable r) { return new Thread(r, String.format("NettyServerSelector_%d_%d", threadTotal, this.threadIndex.incrementAndGet())); } }); }
From source file:com.dlc.server.DLCHttpServer.java
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*w w w. j av a2s. co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new DLCHttpServerInitializer(sslCtx)); 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:com.doctor.netty5.ebook.netty_the_definitive_guide.Chapter4ForTimerServer.java
License:Apache License
public void bind(int port) throws InterruptedException { // ??NIO/*from www . j a v a 2 s .c om*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new TimeServerHander()); } }); ChannelFuture future = b.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.ebook.netty_the_definitive_guide.TimeServer.java
License:Apache License
public void bind(int port) throws InterruptedException { // ??NIO/*from w w w .j a va 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, 1024).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeServerHander()); } }); ChannelFuture future = b.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.chatroom.ChatRoomServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww .j a v a 2s . c om*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter())); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new ChatRoomServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(ChatRoomServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo.EchoServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*w w w . j a v a 2s . co m*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(2048)); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo_object.EchoServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww . jav a 2 s . c o m bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new ObjectDecoder( ClassResolvers.softCachingResolver(this.getClass().getClassLoader()))); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.factorial_algorithm.FactorialServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w . j av a2s . co m bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NumberEncoder()); ch.pipeline().addLast(new BigIntegerDecoder()); ch.pipeline().addLast(new FactorialServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(FactorialServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.http.helloworld.HelloWorldServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from www . j a va 2 s . com bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .option(ChannelOption.SO_BACKLOG, 1024).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HelloWorldServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(HelloWorldServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }