List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:com.github.brandtg.switchboard.HdfsLogIterator.java
License:Apache License
/** * Process listens to edit log events and prints their toString to STDOUT. *///from ww w.java 2 s .c o m public static void main(String[] args) throws Exception { if (args.length != 2) { throw new Exception("usage: sourceHost:sourcePort serverPort"); } String[] sourceHostPort = args[0].split(":"); InetSocketAddress sourceAddress = new InetSocketAddress(sourceHostPort[0], Integer.valueOf(sourceHostPort[1])); InetSocketAddress serverAddress = new InetSocketAddress(Integer.valueOf(args[1])); PipedOutputStream outputStream = new PipedOutputStream(); final EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); final LogReceiver logReceiver = new LogReceiver(serverAddress, eventLoopGroup, outputStream); final LogPuller logPuller = new LogPuller(sourceAddress, serverAddress, "*", 0); final ExecutorService pullerExecutor = Executors.newSingleThreadExecutor(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { logPuller.shutdown(); pullerExecutor.shutdown(); logReceiver.shutdown(); } catch (Exception e) { LOG.error("Exception while shutting down log receiver", e); } eventLoopGroup.shutdownGracefully(); } }); logReceiver.registerListener(logPuller); logReceiver.start(); pullerExecutor.submit(logPuller); // Print edit log ops to console PipedInputStream inputStream = new PipedInputStream(outputStream, 1024 * 1024); LogIterator<FSEditLogOp> itr = new HdfsLogIterator(inputStream); while (itr.hasNext()) { System.out.println(itr.next()); } }
From source file:com.github.herong.rpc.netty.protobuf.demo1.ProtobufClient.java
License:Apache License
public void run() throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/* w ww . ja v a 2 s . co m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline() // .addLast("frameDecoder", new ProtobufVarint32FrameDecoder()) // .addLast("protobufDecoder", new ProtobufDecoder(AddressBookProtos.AddressBook.getDefaultInstance())) // .addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()) .addLast("protobufEncoder", new ProtobufEncoder()) // .addLast("handler", new ProtobufClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); sendMsg(f.channel()); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.github.herong.rpc.netty.protobuf.demo1.ProtobufServer.java
License:Apache License
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from www .j a va 2s .co 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 { ch.pipeline() // .addLast("frameDecoder", new ProtobufVarint32FrameDecoder()) // .addLast("protobufDecoder", new ProtobufDecoder(AddressBookProtos.AddressBook.getDefaultInstance())) // .addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()) .addLast("protobufEncoder", new ProtobufEncoder()) // .addLast("handler", new ProtobufServerHandler()); } }); // 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.github.herong.rpc.netty.protobuf.demo2.Demo2ProtobufClient.java
License:Apache License
public void run() throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w. j av a 2s. c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline() // .addLast("frameDecoder", new ProtobufVarint32FrameDecoder()) // .addLast("protobufDecoder", new ProtobufDecoder(Message.DTO.getDefaultInstance())) // .addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()) .addLast("protobufEncoder", new ProtobufEncoder()) // .addLast("handler", new Demo2ProtobufClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); sendMsg(f.channel()); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.github.herong.rpc.netty.protobuf.demo2.Demo2ProtobufServer.java
License:Apache License
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww. j ava2s. c om*/ 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 { ch.pipeline() // .addLast("frameDecoder", new ProtobufVarint32FrameDecoder()) // .addLast("protobufDecoder", new ProtobufDecoder(Message.DTO.getDefaultInstance())) // .addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()) .addLast("protobufEncoder", new ProtobufEncoder()) // .addLast("handler", new Demo2ProtobufServerHandler()); } }); // 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.github.jonbonazza.puni.core.Application.java
License:Apache License
private void bootstrap() throws Exception { SslContext sslContext = null;/*from ww w. ja v a2 s .c o m*/ SSLConfiguration sslConfig = config.getSsl(); if (config.getSsl().isEnabled()) { sslContext = SslContext.newServerContext(new File(sslConfig.getCert()), new File(sslConfig.getPrivateKey())); } EventLoopGroup eventGroup = new NioEventLoopGroup(config.getEventLoopThreadCount()); try { ServerBootstrap b = new ServerBootstrap(); b.group(eventGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new HttpInitializer(sslContext, muxer)); Channel ch = b.bind(config.getPort()).sync().channel(); ch.closeFuture().sync(); bootstrapped = true; } finally { eventGroup.shutdownGracefully(); } }
From source file:com.github.liyp.netty.App.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try {/*from ww w .j a va2 s .co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ReplayingDecoder() { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { short magicHeader = in.readShort(); logger.debug("Receive magic header: {}.", magicHeader); if (magicHeader != HEADER) { logger.error("Receive illegal magic header: {}, close channel: {}.", magicHeader, ctx.channel().remoteAddress()); ctx.close(); } short dataLen = in.readShort(); logger.debug("Receive message data length: {}.", dataLen); if (dataLen < 0) { logger.error("Data length is negative, close channel: {}.", ctx.channel().remoteAddress()); ctx.close(); } ByteBuf payload = in.readBytes(dataLen); String cloudMsg = payload.toString(CharsetUtil.UTF_8); logger.debug("Receive data: {}.", cloudMsg); out.add(cloudMsg); } }).addLast(new MessageToByteEncoder<String>() { @Override protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception { out.writeBytes(msg.getBytes()); } }).addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("start receive msg..."); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { logger.info("receive msg: {}", msg); logger.info("echo msg"); ctx.writeAndFlush(msg); } }); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(PORT).sync(); logger.info("9999"); f.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); boss.shutdownGracefully(); } }
From source file:com.github.liyp.netty.HandlerChainApp.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try {/* www .j a va2 s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("1"); ctx.fireChannelActive(); } }).addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("2"); ctx.fireChannelActive(); } }).addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("3"); ctx.fireChannelActive(); } }); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(PORT).sync(); logger.info("9999"); f.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); boss.shutdownGracefully(); } }
From source file:com.github.nettybook.ch4.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/*w ww . j av a 2 s . c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new EchoClientHandler1()); p.addLast(new LoggingHandler()); } }); ChannelFuture f = b.connect("localhost", 8888).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.github.nettybook.ch7.junit.TelnetServerV3.java
License:Apache License
public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from www. j av a 2s. com*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new TelnetServerInitializerV3()); ChannelFuture future = b.bind(address).sync(); future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }