public class ClientBootstrap extends Bootstrap
Channel
and makes a
connection attempt.
Options
are used to configure a channel:
ClientBootstrap b = ...;
// Options for a new channel
b.setOption("remoteAddress", new InetSocketAddress
("example.com", 8080));
b.setOption("tcpNoDelay", true);
b.setOption("receiveBufferSize", 1048576);
For the detailed list of available options, please refer to
ChannelConfig
and its sub-types
ChannelPipeline
and you can configure it
in two ways.
The first approach is to use the default pipeline and let the bootstrap to shallow-copy the default pipeline for each new channel:
ClientBootstrap b = ...;
ChannelPipeline
p = b.getPipeline();
// Add handlers to the pipeline.
p.addLast("encoder", new EncodingHandler());
p.addLast("decoder", new DecodingHandler());
p.addLast("logic", new LogicHandler());
Please note 'shallow-copy' here means that the added ChannelHandler
s
are not cloned but only their references are added to the new pipeline.
Therefore, you have to choose the second approach if you are going to open
more than one Channel
whose ChannelPipeline
contains any
ChannelHandler
whose ChannelPipelineCoverage
is "one"
.
The second approach
is to specify a ChannelPipelineFactory
by yourself and have full
control over how a new pipeline is created. This approach is more complex
than the first approach while it is much more flexible:
ClientBootstrap b = ...;
b.setPipelineFactory(new MyPipelineFactory());
public class MyPipelineFactory implements ChannelPipelineFactory
{
// Create a new pipeline for a new channel and configure it here ...
}
Channel
sClientBootstrap
is just a helper class. It neither allocates nor
manages any resources. What manages the resources is the
ChannelFactory
implementation you specified in the constructor of
ClientBootstrap
. Therefore, it is OK to create as many
ClientBootstrap
instances as you want to apply different settings
for different Channel
s.Constructor and Description |
---|
ClientBootstrap()
Creates a new instance with no
ChannelFactory set. |
ClientBootstrap(ChannelFactory channelFactory)
Creates a new instance with the specified initial
ChannelFactory . |
Modifier and Type | Method and Description |
---|---|
ChannelFuture |
connect()
Attempts a new connection with the current
"remoteAddress" and
"localAddress" option. |
ChannelFuture |
connect(SocketAddress remoteAddress)
Attempts a new connection with the specified
remoteAddress and
the current "localAddress" option. |
ChannelFuture |
connect(SocketAddress remoteAddress,
SocketAddress localAddress)
Attempts a new connection with the specified
remoteAddress and
the specified localAddress . |
getFactory, getOption, getOptions, getPipeline, getPipelineAsMap, getPipelineFactory, releaseExternalResources, setFactory, setOption, setOptions, setPipeline, setPipelineAsMap, setPipelineFactory
public ClientBootstrap()
ChannelFactory
set.
Bootstrap.setFactory(ChannelFactory)
must be called before any I/O
operation is requested.public ClientBootstrap(ChannelFactory channelFactory)
ChannelFactory
.public ChannelFuture connect()
"remoteAddress"
and
"localAddress"
option. If the "localAddress"
option is
not set, the local address of a new channel is determined automatically.
This method is similar to the following code:
ClientBootstrap b = ...; b.connect(b.getOption("remoteAddress"), b.getOption("localAddress"));
IllegalStateException
- if "remoteAddress"
option was not setClassCastException
- if "remoteAddress"
or "localAddress"
option's
value is neither a SocketAddress
nor null
ChannelPipelineException
- if this bootstrap's pipelineFactory
failed to create a new ChannelPipeline
public ChannelFuture connect(SocketAddress remoteAddress)
remoteAddress
and
the current "localAddress"
option. If the "localAddress"
option is not set, the local address of a new channel is determined
automatically. This method is identical with the following code:
ClientBootstrap b = ...; b.connect(remoteAddress, b.getOption("localAddress"));
ClassCastException
- if "localAddress"
option's value is
neither a SocketAddress
nor null
ChannelPipelineException
- if this bootstrap's pipelineFactory
failed to create a new ChannelPipeline
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress)
remoteAddress
and
the specified localAddress
. If the specified local address is
null
, the local address of a new channel is determined
automatically.ChannelPipelineException
- if this bootstrap's pipelineFactory
failed to create a new ChannelPipeline
Copyright © 2008–2013 JBoss, by Red Hat. All rights reserved.