Netty3 Source Analysis-Channelpipeline

Source: Internet
Author: User


Channelpipeline's role is to organize a series of channelhandlersserve a channel and handle various events. Implements the advanced form of the Interceptor filter pattern (A advanced form of the intercepting Filter pattern, which effectively controls how an event is handled andhow channelhandlers interact with each other. The type structure diagram is:

Pipeline creation: For each new channel, you must create and add a pipeline for it, once set, the coupling between them is permanent, this channel different add another pipeline, can not decouple the current pipeline. The recommended way to create pipeline is to take advantage of the helper methods in channels instead of invoking the constructor of an implementation. import static org.jboss.netty.channel.channels.*;Channelpipeline pipeline = Pipeline ();//Same with channels.pipeline ()
event flow in pipeline: Shows events being channelhandlerstypical ways of handling. A channelevent can be processed by channelupstreamhandle or Channeldownstreamhandler and then called by aChannelhandlercontext.sendupstream (channelevent) orChannelhandlercontext.senddownstream (channelevent) the eventforwards to the nearest one handler. There are different interpretations depending on the direction of the event (seechannelevent ).


the upstream and downstream events are divided into the corresponding direction of the handler processing. A upstream handler typically handles in-band data generated by the IO thread shown at the bottom of the figure ( Inbound Data). in-band data is usually the real input stream from the remote host to read the data, i.e. Inputstream.read (byte[]). If an upstream event arrivesupstream handler The top, then will silently discard. Similarly, aDownstream Handlergenerate and transmit out-of- band data (Outbound TrafficFor example, write a request, if a downstream event reaches the lowest end, it will be processed by the IO thread associated with the channel (that is, the worker thread that was mentioned earlier). The IO threads here typically perform true output stream operations, i.e.Outputstream.write (byte[]).
look at the following pipeline: Channelpipeline p = channels.pipeline ();p.addlast ("1", New Upstreamhandlera ());P.addlast ("2", New Upstreamhandlerb ());P.addlast ("3", New Downstreamhandlera ());P.addlast ("4", New Downstreamhandlerb ());P.addlast ("5", New Upstreamhandlerx ());It is important to Note that the interpretation of handler order in different directions is not the same, and it depends on the scope of the handler's responsibilities. For example, forThe handler of the upstream event followed by 1,2,5, handler in 4,3 order for downstream event.
Build Pipeline: We may need multiple handlers in the program to handle IO requests such as read,write,close. For example, a typical server requires each channel to have the following handlers, and will depend on the protocol and the complexity of the business Roger. Protocol decoder-translates binary data (e.g. Channelbuffer) into a Java object.Protocol encoder-translates A Java object into binary data.executionhandler-applies a thread model.Business Logic handler-performs The actual business logic (e.g. database access).
The code form looks like this:Channelpipeline pipeline = Channels.pipeline ();pipeline.addlast ("Decoder", New Myprotocoldecoder ());pipeline.addlast ("encoder", New Myprotocolencoder ());pipeline.addlast ("executor", New Executionhandler (New Orderedmemoryawarethreadpoolexecutor (1048576, 1048576));pipeline.addlast ("handler", New Mybusinesslogichandler ());
thread Safety: because Channelpipeline is thread-safe, so you can add or remove a channelhandler at any time . For example, when sensitive information is present, addSslhandler, remove it after the transaction is complete.
defect (Pitfall): Due to default now Channelpipeline The details of the internal implementation, the following code will not work as expected, if theFirsthandlerand the last handler in the pipeline. (Do not know how netty5 now do?) )Public class Firsthandler extends Simplechannelupstreamhandler {
@OverridePublic void messagereceived (Channelhandlercontext ctx, messageevent e) {//Remove This handler from the pipeline,ctx.getpipeline (). Remove (this);//And let SecondHandler handle the current event.ctx.getpipeline (). AddLast ("2nd", New SecondHandler ());Ctx.sendupstream (e);     } }The right thing to do is to make sure there is at least one handler (because of the Pre,next in handler context) before removal. ), or increase and remove it first.
See my github for detailed source comments.


Netty3 Source Analysis-Channelpipeline

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.