Netty 4 퍼 가기 서비스의 실현 방안

만약 에 Netty 로 퍼 가기 서 비 스 를 한다 면(동기 응답 이 필요 없 음)Netty 에는 이 수 요 를 만족 시 키 는 특수 한 모델 이 있 습 니 다.
Handler 에서 새로운 Bootstrap 을 사용 하 는 원리 입 니 다.이 새로운 Bootstrap 과 바깥쪽 Bootstrap 은 같은 채널 을 공유 할 수 있 습 니 다.
새로운 Bootstrap 은 클 라 이언 트 모드 로 전송 목표 서비스 주소 에 연결 되 며 구체 적 으로 다음 과 같이 실현 합 니 다.
import java.net.InetSocketAddress;  
   
import io.netty.bootstrap.Bootstrap;  
import io.netty.bootstrap.ServerBootstrap;  
import io.netty.buffer.ByteBuf;  
import io.netty.channel.Channel;  
import io.netty.channel.ChannelFuture;  
import io.netty.channel.ChannelFutureListener;  
import io.netty.channel.ChannelHandlerContext;  
import io.netty.channel.ChannelInitializer;  
import io.netty.channel.SimpleChannelInboundHandler;  
import io.netty.channel.nio.NioEventLoopGroup;  
import io.netty.channel.socket.nio.NioServerSocketChannel;  
import io.netty.channel.socket.nio.NioSocketChannel;  
import io.netty.handler.codec.string.StringDecoder;  
import io.netty.handler.codec.string.StringEncoder;  
   
public class Aaa {  
    public static void main(String[] params) {  
        ServerBootstrap bootstrap = new ServerBootstrap();  
        bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())  
            .channel(NioServerSocketChannel.class)  
            .childHandler(new ChannelInitializer() {  
                @Override  
                protected void initChannel(Channel ch) throws Exception {  
                    // TODO Auto-generated method stub  
                    ch.pipeline().addLast(new StringDecoder());   
                    ch.pipeline().addLast(new StringEncoder());  
                    ch.pipeline().addLast(new SimpleChannelInboundHandler() {  
                        private ChannelHandlerContext innerCtx;  
                        ChannelFuture connectFuture;  
                        @Override  
                        public void channelActive(ChannelHandlerContext ctx)  
                                throws Exception {  
                            Bootstrap bootstrap = new Bootstrap();  
                            bootstrap.channel(NioSocketChannel.class).handler(  
                                new SimpleChannelInboundHandler() {  
                                    //       ,          ,            
                                    @Override  
                                    protected void channelRead0(  
                                        ChannelHandlerContext ctx, ByteBuf in)  
                                        throws Exception {  
                                        innerCtx = ctx;  
                                          
                                        byte[] dst = new byte[in.readableBytes()];  
                                        in.readBytes(dst);  
                                          
                                        System.out.println("Received data" + new String(dst));  
                                    }  
                                      
                                    @Override  
                                    public void channelActive(ChannelHandlerContext ctx) throws Exception {  
                                        System.out.println("    "+ctx.channel().toString());  
                                    }  
                                } );  
                            bootstrap.group(ctx.channel().eventLoop());//     。   channel eventLoop        
                            connectFuture = bootstrap.connect(  
//                                new InetSocketAddress("192.168.60.49", 23456));  
                                    new InetSocketAddress("localhost", 23456));  
                        }  
   
                        @Override  
                        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {  
                            // TODO Auto-generated method stub  
                            if (connectFuture.isDone()) {  
                                // do something with the data  
                                //channel    ,      EventLoop,              
                                //       channel  
   
                                if (innerCtx != null && innerCtx.channel().isActive()) {  
                                    innerCtx.writeAndFlush(msg);  
                                }  
                            }  
                        }  
                    });  
                    ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));  
                    future.addListener(new ChannelFutureListener() {  
                        @Override  
                        public void operationComplete(ChannelFuture channelFuture)  
                                throws Exception {  
                            if (channelFuture.isSuccess()) {  
                                System.out.println("Server bound 8080");  
                            } else {  
                                System.err.println("Bind attempt failed");  
                                channelFuture.cause().printStackTrace();  
                            }  
                        }   
                    });  
                }  
            });  
    }  
}

좋은 웹페이지 즐겨찾기