Netty3 Source Analysis-Clientbootstrap

Source: Internet
Author: User
Tags shallow copy



Bootstrap isThe Channel initialization helper class provides the data structure required to initialize the channel or sub-channel, so the Clientbootstrap is the client and the connection operation is performed.
To Configure a channel is to pass the corresponding key-value pair option to the underlying:Clientbootstrap b = ...;//Options for a new channelb.setoption ("Remoteaddress", New Inetsocketaddress ("example.com", 8080));b.setoption ("Tcpnodelay", true);b.setoption ("Receivebuffersize", 1048576);
Configure the channel pipeline, each channel has its own pipeline, the recommended way is to call bootstrap.setpipelinefactory (channelpipelinefactory) to develop channelpipelinefactory : Clientbootstrap b = ...;b.setpipelinefactory (New Mypipelinefactory ());
Public class Mypipelinefactory implements Channelpipelinefactory {Public channelpipeline getpipeline() throws Exception {//Create and configure a new pipeline for a new channel.Channelpipeline p = channels.pipeline ();p.addlast ("encoder", New Encodinghandler ());p.addlast ("Decoder", New Decodinghandler ());p.addlast ("logic", New Logichandler ());return p;   } }
another way, which is only applicable in certain cases (explained in the source code of Bootstrap), is to use the default pipeline and allow the new channel to light copy it:Clientbootstrap b = ...;Channelpipeline p = b.getpipeline ();
//ADD handlers to the default pipeline.p.addlast ("encoder", New Encodinghandler ());p.addlast ("Decoder", New Decodinghandler ());p.addlast ("logic", New Logichandler ());
shallow copy means that the default pipeline inside the handler, only their reference to the new channelpipeline, so pay attention to those handler whether there are side effects. Therefore, it is not suitable to open a channel for each new connection on the server side, and to customize different configurations for different channel.
alsoClientbootstrap is just an auxiliary class that does not allocate and manage any resources, and really manages resources that are passed in from the constructorChannelFactory Implementation, so use the sameChannelFactoryto construct multiple bootstrap instances, applying different settings for different channels is completely OK.
Bind method:The binding operation is used when the bindings and connections need to be separated. For example, before attempting to connect, you can set a attachment for this channel via Channel.setattachment (Object)This allows you to access this attachment once the connection is suggested. channelfuture bindfuture = bootstrap.bind (New inetsocketaddress ("192.168.0.15", 0));Channel channel = Bindfuture.getchannel ();channel.setattachment (dataobj);Channel.connect (New inetsocketaddress ("192.168.0.30", 8080));in Channelhandler, you can access the following. Public class Yourhandler extends Simplechannelupstreamhandler {Public void channelconnected(Channelhandlercontext ctx, channelstateevent e) throws Exception {Object dataObject = Ctx.getchannel (). GetAttachment ();      }  }
detailed source code comments can be seen on my github.





Netty3 Source Analysis-Clientbootstrap

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.