- A EventLoopGroup which contains one or more EventLoop.
- A EventLoop will only be bound to a single Thread in which its entire life cycle.
- All the various I processed by the EventLoop / O events will be processed on the Thread which it is associated.
- A Channel throughout its life cycle will only be registered on a EventLoop.
- A EventLoop during operation, will be assigned to one or a plurality of Channel.
- Execution order and submitted the order with the task of submitting a Channel is the same (Advanced to go out first, the task queue).
Important conclusion: in the realization of netty of which must be thread-safe, based on this we can be a channel of reference memory storage, and when you need to send data to the remote endpoint, by this reference to call the appropriate Channel method; even though there were many threads it also does not appear in the use of multi-threading issues, and the message will be sent out in the order.
Important conclusion: time-consuming tasks in our business development, not to put the long-running execution queue EventLoop, since it will have to block all other tasks on the Channel corresponding to the thread, if we need to blocking calls or time-consuming operation (the actual development are common), then we need to use a special EventExecutor (business thread pool).
Usually we have two kinds of ways:
1, in ChannelHandler callback method, using a thread pool their definition of business, so that you can achieve asynchronous calls.
2, by means of the methods to be invoked addLast ChannelHandler added to ChannelPipeLine netty provided to pass EventExecutor.
Note: By default (call addLast (handler)), in ChannelHandler callback method is performed by the I / O thread, if you call ChannelPipeline addLast (EventExecutorGroup group, ChannelHandler ... handlers); method, then ChannelHandler callback method It is performed by parameter group thread group.
netty asynchronous:
You can see from the chart, ChannelPromise inherited the Promise interface, Promise can write (writable), what can be written, before the Future are get, isSuccess method and the like, can be seen in ChannelPromise inside setSuccess ( void result) [setSuccess can be written only once, the next time an error writing] write method and the like. ChannelPromise literally means commitment, regardless of success or failure would promise to give you a result.
Future JDK provided only by manually checking the implementation of the results, and this operation will be blocked; Netty is to ChannelFuture been enhanced, here is related to the observer mode to execute the callback way to get results ChannelFutureListener, in addition to the operating manual inspection obstruction, it is worth noting that: operationcomplete ChannelFutureListener method is performed by the I / O thread, so pay attention to is not to perform time-consuming operations here, or need required by another thread or thread pool carried out.
For example: jdk's Future returns the results obtained using isDone get or acquire, and these two are blocking the way, even if the method timeout time to also get less return null, these things are developers to do their own while Netty solved the drawbacks, netty by adding the listener mode on the Future, registration number Listner to the Future, Future hold Channel, when a particular event occurs, Future Listner call the corresponding method, the method participants have a reference to the current Future, we will get in the Channel Future Listener inside, after obtaining the data is processed in the Channel Listener inside, which is the method Listener not to say inside the top reasons for handling time-consuming business.
To tell you the ChannelHandler, ChannelHandler have pushed and popped the Handler, took ChannelInboundHandlerAdapter, we want to write a stack processors, you need all the methods inside interfaces must be rewritten, but we only use part of the method, and the Adapter is a kind of adapter mode, all methods will achieve, when we use the direct use of the adapted category (either directly or rewritten) to implement business logic on it, greatly facilitate and reduce the developer to the developer's work the amount.
1 |
public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter implements ChannelInboundHandler { |
ChannelInboundHandlerAdapter concrete implementation class has SimpleChannelInboundHandler, he and ChannelInboundHandlerAdapter What difference does it make?
. 1 |
abstract class SimpleChannelInboundHandler public <the I> the extends ChannelInboundHandlerAdapter |
It is intuitive that the addition of a generic I, Type I is to accept the message, such as String, Object, etc., while in ChannelInboundHandlerAdapter inside four messages need to be cast, which is their biggest difference. In addition SimpleChannelInboundHandler message will perform ReferenceCountUtil.release (Object) and ReferenceCountUtil.retain (Object), respectively release a message reference and to maintain a message reference (flow to the next handler).
We generally use ChannelInboundHandlerAdapter and processing into SimpleChannelInboundHandler stack data.
Practical application:
ReferenceCountUtil method of release:
1 |
public static boolean release(Object msg) { |
ReferenceCounted used is the class of operation:
1 |
/** |