List of usage examples for io.netty.channel ChannelPipeline addLast
ChannelPipeline addLast(EventExecutorGroup group, ChannelHandler... handlers);
From source file:com.janlr.sanxiao.game.handler.GpbTcpServer.java
License:Apache License
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override/*from ww w.j a va 2s . c o m*/ protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); cp.addLast("decoder", GPB_DECODER_HANDLER); cp.addLast("encoder", GPB_ENCODER_HANDLER); // handler cp.addLast("handler", serverHandler); // cp.addLast("handler", new ServerHandler()); } }; }
From source file:com.jansegre.jwar.webapi.ApiServer.java
License:Open Source License
@Override public void start() { // Reference: http://netty.io/wiki/user-guide-for-4.x.html EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww.j av a 2 s .c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add the text line codec combination first, pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); // the encoder and decoder are static as these are sharable pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // and then business logic. pipeline.addLast("handler", apiSocket); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(apiPort).syncUninterruptibly(); log.info("ApiSocket started at port: {}", apiPort); // Also start parent super.start(); // Wait until the server socket is closed. f.channel().closeFuture().syncUninterruptibly(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter11.websocket.WebSocketServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from ww w . j ava2 s .co m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-codec", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); pipeline.addLast("handler", new WebSocketServerHandler()); } }); Channel ch = b.bind(port).sync().channel(); System.out.println("Web socket server started at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.khs.microservice.whirlpool.whirlpoolserver.WhirlpoolServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from www. ja v a 2s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("encoder", new HttpResponseEncoder()); p.addLast("decoder", new HttpRequestDecoder()); p.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8)); p.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); p.addLast("aggregator", new HttpObjectAggregator(65536)); p.addLast("handler", new WhirlpoolServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); logger.info("Whirlpool Server started"); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { logger.info("Whirlpool Server shutdown started"); // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); logger.info("Whirlpool Server shutdown completed"); } }
From source file:com.khs.stockticker.StockTickerServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from www . j a va 2 s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("encoder", new HttpResponseEncoder()); p.addLast("decoder", new HttpRequestDecoder()); p.addLast("aggregator", new HttpObjectAggregator(65536)); p.addLast("handler", new StockTickerServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); logger.info("Ticket Symbol Server started"); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { logger.info("Ticket Symbol Server shutdown started"); // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); logger.info("Ticket Symbol Server shutdown completed"); } }
From source file:com.king.platform.net.http.netty.ChannelManager.java
License:Apache License
public ChannelManager(NioEventLoopGroup nioEventLoop, final HttpClientHandler httpClientHandler, Timer nettyTimer, TimeProvider timeProvider, ChannelPool channelPool, final ConfMap confMap, RootEventBus rootEventBus) {//from w ww . j a v a 2s .c om this.eventLoopGroup = nioEventLoop; this.nettyTimer = nettyTimer; this.timeProvider = timeProvider; this.channelPool = channelPool; this.confMap = confMap; plainBootstrap = new Bootstrap().channel(NioSocketChannel.class).group(eventLoopGroup); plainBootstrap.handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); addLoggingIfDesired(pipeline, confMap.get(ConfKeys.NETTY_TRACE_LOGS)); pipeline.addLast("http-codec", newHttpClientCodec()); pipeline.addLast("inflater", new HttpContentDecompressor()); pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("httpClientHandler", httpClientHandler); } }); secureBootstrap = new Bootstrap().channel(NioSocketChannel.class).group(eventLoopGroup); secureBootstrap.handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); SslInitializer sslInitializer = new SslInitializer( new SSLFactory(confMap.get(ConfKeys.SSL_ALLOW_ALL_CERTIFICATES)), confMap.get(ConfKeys.SSL_HANDSHAKE_TIMEOUT_MILLIS)); pipeline.addLast(SslInitializer.NAME, sslInitializer); addLoggingIfDesired(pipeline, confMap.get(ConfKeys.NETTY_TRACE_LOGS)); pipeline.addLast("http-codec", newHttpClientCodec()); pipeline.addLast("inflater", new HttpContentDecompressor()); pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("httpClientHandler", httpClientHandler); } }); secureBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, confMap.get(ConfKeys.CONNECT_TIMEOUT_MILLIS)); plainBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, confMap.get(ConfKeys.CONNECT_TIMEOUT_MILLIS)); NettyChannelOptions nettyChannelOptions = confMap.get(ConfKeys.NETTY_CHANNEL_OPTIONS); for (ChannelOption channelOption : nettyChannelOptions.keys()) { plainBootstrap.option(channelOption, nettyChannelOptions.get(channelOption)); secureBootstrap.option(channelOption, nettyChannelOptions.get(channelOption)); } rootEventBus.subscribePermanently(Event.ERROR, new ErrorCallback()); rootEventBus.subscribePermanently(Event.COMPLETED, new CompletedCallback()); rootEventBus.subscribePermanently(Event.EXECUTE_REQUEST, new ExecuteRequestCallback()); }
From source file:com.king.platform.net.http.netty.ChannelManager.java
License:Apache License
private void addLoggingIfDesired(ChannelPipeline pipeline, boolean desired) { if (desired) { pipeline.addLast("logging", new LoggingHandler(LogLevel.TRACE)); }/*from ww w .j a v a 2 s . c o m*/ }
From source file:com.kingmed.bidir.gateway.server.GatewayServerInitializer.java
License:Apache License
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); SSLEngine engine = null;//from w w w . j a v a2 s . c o m if (SSLMODE.CA.toString().equals(tlsMode)) { engine = GatewaySslContextFactory .getServerContext(tlsMode, System.getProperty("user.dir") + "/src/main/java/com/kingmed/bidir/gateway/conf/sGateway.jks", null) .createSSLEngine(); } else if (SSLMODE.CSA.toString().equals(tlsMode)) { engine = GatewaySslContextFactory.getClientContext(tlsMode, System.getProperty("user.dir") + "/src/main/java/com/kingmed/bidir/gateway/conf/sGateway.jks", System.getProperty("user.dir") + "/src/main/java/com/kingmed/bidir/gateway/conf/sGateway.jks") .createSSLEngine(); } else { System.err.println("ERROR: " + tlsMode); System.exit(-1); } engine.setUseClientMode(false); if (SSLMODE.CSA.toString().equals(tlsMode)) engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8 * 1024, Delimiters.lineDelimiter())); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("handler", new GatewayServerHandler()); }
From source file:com.lb.mysession.client.SessionClientInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder()); p.addLast("protobufDecoder", new ProtobufDecoder(ResultProto.Result.getDefaultInstance())); p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()); p.addLast("protobufEncoder", new ProtobufEncoder()); p.addLast("handler", new SessionClientHandler()); }
From source file:com.lb.mysession.server.ProtobufServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); /*/*w w w .ja v a 2s . c o m*/ * pipeline.addLast("codec-http", new HttpServerCodec()); * pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); */ p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder()); p.addLast("protobufDecoder", new ProtobufDecoder(MessageProto.Message.getDefaultInstance())); p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()); p.addLast("protobufEncoder", new ProtobufEncoder()); ProtobufChannelHandler pf = new ProtobufChannelHandler(); p.addLast("handler", pf); }