How to route calls to another system

67 views
Skip to first unread message

crunc...@gmail.com

unread,
May 17, 2017, 11:16:03 AM5/17/17
to Netty discussions
Looking at Netty in Action and some SO posts, I saw that in order to talk to another system, you can use Bootstrap() to create a new client and attach it to the current eventLoop.  Looking at the proxy example, it's done when the initial server is bootstrapped as well.  So I tried


public void start(final ApplicationContext applicationContext) throws Exception {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ServerHandlerImpl());
                            ch.pipeline().addLast(new SomeOtherHandler());
                        }

                        @Override
                        protected void channelActive(ChannelHandlerContext ctx) throws Exception {
                            Bootstrap clientBootstrap = new Bootstrap();
                            clientBootstrap.channel(NioSocketChannel.class)
                                    .handler(new ChannelInitializer<SocketChannel>() {
                                        @Override
                                        protected void initChannel(SocketChannel ch) throws Exception {
                                            ch.pipeline().addLast(new CredentialsClientHandler());
                                        }
                                    });
                            clientBootstrap.group(ctx.channel().eventLoop());
                            ChannelFuture future = clientBootstrap.connect(host, port);
                        }
                    });

So when a message comes in and it reaches the ServerHandlerImpl, I want it to then route its message to CredentialsClientHandler that would send a message to another server, check the credentials, and then resume the rest of the pipeline.  In ServerHandlerImpl, how do I "send a message" down the other handler and not down SomeOtherHandler?  

Zhu Ran

unread,
May 17, 2018, 10:46:53 PM5/17/18
to Netty discussions
1.don't create new client bootstrap in the channelActive, create a client bootstrap at the application level, as same as the server bootstrap;
2.add the SomeOtherHandler to the client bootstrap pipeline;
3.after the client bootstrap connected to the remote server, save the ChannelHandlerContext in somewhere such as a global object;
4.when you want to send something to the remote server, use the ChannelHandlerContext you save in the global object
Reply all
Reply to author
Forward
0 new messages