Handwritten MQ Framework - combing with netty transformation

Keywords: Java Netty codec socket network

1. Background

Copy above Handwritten MQ Framework (3) - Client Implementation , which previously implemented mq as a server and client via the web, now plans to use netty to transform it.A while ago, I learned how to use netty (https://www.w3cschool.cn/netty4userguide/52ki1iey.html).There are probably some ideas.

netty encapsulates the use of socket s, and we can build high-performance network applications with simple calls.I plan to use the following examples to transform gmq.

This article mainly refers to: https://www.w3cschool.cn/netty4userguide/, https://www.w3cschool.cn/essential_netty_in_action/

2. What is netty

Netty is a java open source framework provided by JBOSS.Netty provides an asynchronous, event-driven framework and tools for rapidly developing high-performance, highly reliable network servers and clients.

--From https://www.w3cschool.cn/netty4userguide/52ki1iey.html

netty is a java framework and a network programming framework that supports asynchronous, event-driven features, so it performs well.

 

3. Simple implementation of netty

1. Server

1)SimpleServerHandler

Handler is a processor, and handler is generated by Netty to handle I/O events.

package me.lovegao.netty.learnw3c.mqdemo;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

public class SimpleServerHandler extends SimpleChannelInboundHandler<String> {
    public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("[SERVER] - " + incoming.remoteAddress() + " join\n");
        channels.add(ctx.channel());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("[SERVER] - " + incoming.remoteAddress() + " leave\n");
        channels.remove(ctx.channel());
    }
    
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("[" + incoming.remoteAddress() + "]" + s);
        if(s == null || s.length() == 0) {
            incoming.writeAndFlush("The news is empty!\n");
        } else {
//            MqRouter<?> mqRouter = JSONObject.parseObject(s, MqRouter.class);
//            System.out.println(mqRouter.getUri());
            String responseMsg = "I got it," + s + "\n";
            incoming.writeAndFlush(responseMsg);
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SimpleChatClient:"+incoming.remoteAddress()+"On-line");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SimpleChatClient:"+incoming.remoteAddress()+"Drop line");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SimpleChatClient:"+incoming.remoteAddress()+"abnormal");
        
        cause.printStackTrace();
        ctx.close();
    }

}

2)SimpleServerInitializer

SimpleServerInitializer is used to add multiple processing classes to ChannelPipeline, including encoding, decoding, SimpleServerHandler, and so on.

package me.lovegao.netty.learnw3c.mqdemo;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class SimpleServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new SimpleServerHandler());
        
        System.out.println("SimpleChatClient:" + ch.remoteAddress() + "Connect");
    }

}

 

3)SimpleServer

package me.lovegao.netty.learnw3c.mqdemo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class SimpleServer {
    private int port;

    public SimpleServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new SimpleServerInitializer()).option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            System.out.println("SimpleChatServer Started");

            ChannelFuture f = b.bind(port).sync();

            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();

            System.out.println("SimpleChatServer Closed");
        }
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new SimpleServer(port).run();
    }
}

 

2. Client

1)SimpleClientHandler

package me.lovegao.netty.learnw3c.mqdemo;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class SimpleClientHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        System.out.println("Information received:" + s);
    }

}

 

2)SimpleClientInitializer

package me.lovegao.netty.learnw3c.mqdemo;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class SimpleClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new SimpleClientHandler());
    }

}

 

3)SimpleClient

package me.lovegao.netty.learnw3c.mqdemo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class SimpleClient {
    private final String host;
    private final int port;
    
    public SimpleClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        new SimpleClient("localhost", 8080).run();
    }
    
    public void run() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new SimpleClientInitializer());
            Channel channel = bootstrap.connect(host, port).sync().channel();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            while(true) {
                String line = in.readLine();
                if(line.equals("exit!")) {
                    break;
                }
                channel.writeAndFlush(line + "\r\n");
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }



}

 

3. Some things in learning

When I slightly changed the code in the tutorial, I found that the client could send messages, the server could receive messages, and the server went to the process of replying to the client, but the client could not receive messages.After restoring the code, it is normal to think about it for half a day before finally finding out that the identity'\n'was missing when the code was changed, thus causing the client to never print the message.

4. How netty is used in gmq

1. What's wrong with the application?

netty only encapsulates network interaction, gmq uses the gmvc framework as a whole, and the gmvc framework cannot be separated from the servlet at present.And I don't really want to change all the code I wrote to my new style.

2. Solutions

1) Modify gmvc framework

The gmvc framework is refactored so that it can be used without a servlet.That is to peel off the IOC function.

Advantages: One step in place, in line with the overall plan.

Disadvantages: gmq iteration can be delayed for a while.

2) Temporarily discard gmvc framework

Temporarily remove the currently dependent gmvc framework and complete the gmq iteration first.After the later transformation of gmvc framework is completed, the transformation will be carried out again.

Advantages: can complete gmq function as soon as possible.

Disadvantages: Removing the frame before putting it on later is equivalent to doing two redundant work.It takes time and effort.

3. Conclusion

Writing a framework is for learning, writing GMVC, writing GMQ are the same purpose.Time is precious and redundant work is reduced. First, GMVC framework is transformed.

4. Some other things

Another thing with netty is the routing problem.Using netty instead of servlet requires resolving routing issues.

5. Preparing to renovate GMVC

Please look forward to...

Posted by iankent on Thu, 02 Jan 2020 16:10:04 -0800