Netty Learning Record 1: netty Learning Record
I recently learned about netty and thought the book netty's authoritative guide is quite good for beginners like me. in addition to the many examples provided by netty itself, it is quite interesting to learn. simple record,
The general server code is as follows:
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 ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoServerHandler(), new MyServerHandler()); } }); // Bind and start to accept incoming connections. b.bind(port).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
After reading the book netty's authoritative guide, the following code should be easier to understand, just like its name: bossGroup (boss) and workerGroup (worker ), bossGroup is used to receive the request thread group, workerGroup is used to process the IO operation thread group, and bossGroup is called workerGroup to process after receiving the request,
EventLoopGroup bossGroup = new NioEventLoopGroup ();
EventLoopGroup workerGroup = new NioEventLoopGroup ();
ServerBootstrap is a startup tool class. The childHandler () method is used to add operation classes for channel Processing to it and initialize them. ChannelPipeline is used for channel Processing classes within the chain management.
B. bind (port). sync (). channel (). closeFuture (). sync (); this sentence is split into the following two statements for better understanding:
B. bind (port). sync (); thread synchronization blocking wait for the server to bind to the specified port,
... Channel (). closeFuture (). after sync () is successfully bound to a port, add a listener for the channel to close the pipeline and implement synchronization blocking until the channel is closed. The thread will continue to run and end the process.
How to Use netty to write an http persistent connection Server
I read the source code of play again, And messageRecived in the play Custom handler:
Public void messageReceived (ChannelHandlerContext ctx, MessageEvent e)
Throws Exception {
Invoker. invoke (new NettyInvokation (request, response, ctx, nettyRequest, e);} After messageReceived is over, the worker thread is about to come soon (some system scheduled final work should be done) leave this request, that is, this request will no longer occupy worker. However
Invoker. invoke this method will submit a task in the thread pool inside the play framework to continue processing the request and complete the real business logic.
The above is the practice of play. To sum up, you can enable a new thread in messageReceived or submit a task to the thread pool to complete the business logic processing part of the request, in this way, not only long connections are not closed, but netty worker processes are not occupied. After processing the business logic, use the callback function to return the result to the client using the HttpResponse provided by netty.
Request-netty master-netty worker-(now worker can process other requests) Start a new thread-netty response
I didn't test it. Let's take a look later.
Interview a person who wants to visit a successful person or college student who wants to work or study outside, write an interview record, no less than 300 words
Write by yourself