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