List of usage examples for io.netty.channel ChannelPipeline addLast
ChannelPipeline addLast(ChannelHandler... handlers);
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 . j av a 2s . 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.device.FFDevice.java
public FFDevice(SocketChannel ch) { this.req = null; this.soc = ch; this.data_rcv = soc.alloc().buffer(512); this.reg_str = ""; this.connect_time = System.currentTimeMillis(); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new IdleStateHandler(0, 0, idle_time_interval_s)); pipeline.addLast(new MessageToByteEncoder<byte[]>() { @Override/*from w w w . j av a 2 s.co m*/ protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception { // TODO Auto-generated method stub out.writeBytes(msg); } }); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub ByteBuf bb = (ByteBuf) msg; if (reg_str.equals("")) { reg_str = bb.toString(Charset.defaultCharset()); FFServer.logger.info(String.format("device that has regs %s is registed", reg_str)); } else { FFServer.logger.debug(String.format("%s receive: %d bytes", reg_str, bb.readableBytes())); data_rcv.writeBytes(bb); } ReferenceCountUtil.release(msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub FFServer.logger.error(cause); Close(); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.ALL_IDLE) { FFServer.logger.info(String.format("%s in idle state", reg_str)); ByteBuf hb = ctx.alloc().buffer(1).writeByte('.'); Send(hb); } } } }); ChannelFuture f = soc.closeFuture(); f.addListener((ChannelFutureListener) new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { is_closed = true; FFServer.logger.info(String.format("%s disconnected", reg_str)); } }); }
From source file:com.mycompany.nettyweb.HttpServerInitializer.java
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); GlobalTrafficShapingHandler globalTrafficShapingHandler = new GlobalTrafficShapingHandler(ch.eventLoop()); trafficCounter = globalTrafficShapingHandler.trafficCounter(); trafficCounter.start();//from ww w .ja v a 2 s . c om p.addLast(globalTrafficShapingHandler); p.addLast("codec", new HttpServerCodec()); p.addLast("handler", new HttpServerHandler(trafficCounter, statistics)); }
From source file:com.myseti.framework.monitor.net.MonitorServerInitializer.java
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); }// ww w . ja va 2s .c o m // Add the number codec first, pipeline.addLast(new DecodeProtocol()); pipeline.addLast(new EncodeProtocol()); // and then business logic. // Please note we create a handler for every new channel // because it has stateful properties. pipeline.addLast(new MonitorServerHandler()); }
From source file:com.netflix.hystrix.examples.reactivesocket.HystrixMetricsReactiveSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { System.out.println("Starting HystrixMetricsReactiveSocketServer..."); final ReactiveSocketServerHandler handler = ReactiveSocketServerHandler .create((setupPayload, rs) -> new EventStreamRequestHandler()); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w .j a v a 2 s . c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(handler); } }); Channel localhost = b.bind("127.0.0.1", 8025).sync().channel(); executeCommands(); localhost.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.netty.fileTest.file.FileReaderClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*from w ww. java 2 s . com*/ 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 { ChannelPipeline p = ch.pipeline(); p.addLast(new FileReaderClientHandler()); } }); // Start the client. ChannelFuture f = b.connect("127.0.0.1", 8007).sync(); // 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.netty.fileTest.http.upload.HttpUploadServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); }//from ww w .j a v a 2s. c om pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new HttpResponseEncoder()); // Remove the following line if you don't want automatic content compression. // pipeline.addLast(new HttpContentCompressor()); // pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new HttpUploadServerHandler()); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { System.out.println("---------------------------------MockGrpcServerInitializer-----------------"); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new GrpcProxyFrontendHandler(remoteHosts, remotePorts, counter)); }
From source file:com.netty.HttpHelloWorldServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }/*from ww w . j a v a2 s .co m*/ // p.addLast(new HttpServerCodec(4096, 8192, 8192, false)); p.addLast(new HttpServerCodec(4096, 8192, 8192, false)); p.addLast("decompressor", new HttpContentDecompressor()); p.addLast("aggegator", new HttpObjectAggregator(512 * 1024)); // p.addLast(new HttpAggregatorInitializer(false)); ServerBaseHandler channelHandler = applicationContext.getBean(ServerBaseHandler.class); channelHandler.setApplicationContext(applicationContext); // p.addLast(new HttpHelloWorldServerHandler()); p.addLast(channelHandler); p.addLast(new HttpContentCompressor()); }
From source file:com.nettyim.server.boot.SecureChatServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add SSL handler first to encrypt and decrypt everything. // In this example, we use a bogus certificate in the server side // and accept any invalid certificates in the client side. // You will need something more complicated to identify both // and server in the real world. if (null != sslCtx) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); }//from w w w . ja v a 2s. c om // On top of the SSL handler, add the text line codec. pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); // and then business logic. pipeline.addLast(new SecureChatServerHandler()); }