
public class ServerBootstrap extends Bootstrap
Channel and accepts
incoming connections.
ConnectionlessBootstrap instead for
connectionless transports. Do not use this helper if you are using a
connectionless transport such as UDP/IP which does not accept an incoming
connection but receives messages by itself without creating a child channel.
ChannelFactory via
bind() and bind(SocketAddress).
Once successfully bound, the parent channel starts to accept incoming connections, and the accepted connections become the children of the parent channel.
Options are used to configure both a
parent channel and its child channels. To configure the child channels,
prepend "child." prefix to the actual option names of a child
channel:
ServerBootstrap b = ...;
// Options for a parent channel
b.setOption("localAddress", new InetSocketAddress(8080));
b.setOption("reuseAddress", true);
// Options for its children
b.setOption("child.tcpNoDelay", true);
b.setOption("child.receiveBufferSize", 1048576);
For the detailed list of available options, please refer to
ChannelConfig and its sub-types.
parentHandler property is
provided.
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 child channel:
ServerBootstrap 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 ChannelHandlers
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 accept
more than one child 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:
ServerBootstrap b = ...;
b.setPipelineFactory(new MyPipelineFactory());
public class MyPipelineFactory implements ChannelPipelineFactory {
// Create a new pipeline for a new child channel and configure it here ...
}
ChannelsServerBootstrap 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
ServerBootstrap. Therefore, it is OK to create as many
ServerBootstrap instances as you want to apply different settings
for different Channels.| Constructor and Description |
|---|
ServerBootstrap()
Creates a new instance with no
ChannelFactory set. |
ServerBootstrap(ChannelFactory channelFactory)
Creates a new instance with the specified initial
ChannelFactory. |
| Modifier and Type | Method and Description |
|---|---|
Channel |
bind()
Creates a new channel which is bound to the local address which was
specified in the current
"localAddress" option. |
Channel |
bind(SocketAddress localAddress)
Creates a new channel which is bound to the specified local address.
|
ChannelHandler |
getParentHandler()
Returns an optional
ChannelHandler which intercepts an event
of a newly bound server-side channel which accepts incoming connections. |
void |
setFactory(ChannelFactory factory)
Sets the
ChannelFactory that will be used to perform an I/O
operation. |
void |
setParentHandler(ChannelHandler parentHandler)
Sets an optional
ChannelHandler which intercepts an event of
a newly bound server-side channel which accepts incoming connections. |
getFactory, getOption, getOptions, getPipeline, getPipelineAsMap, getPipelineFactory, releaseExternalResources, setOption, setOptions, setPipeline, setPipelineAsMap, setPipelineFactorypublic ServerBootstrap()
ChannelFactory set.
setFactory(ChannelFactory) must be called before any I/O
operation is requested.public ServerBootstrap(ChannelFactory channelFactory)
ChannelFactory.public void setFactory(ChannelFactory factory)
ChannelFactory that will be used to perform an I/O
operation. This method can be called only once and can't be called at
all if the factory was specified in the constructor.setFactory in class BootstrapIllegalArgumentException - if the specified factory is not a
ServerChannelFactorypublic ChannelHandler getParentHandler()
ChannelHandler which intercepts an event
of a newly bound server-side channel which accepts incoming connections.null if no parent channel handler is set.public void setParentHandler(ChannelHandler parentHandler)
ChannelHandler which intercepts an event of
a newly bound server-side channel which accepts incoming connections.parentHandler - the parent channel handler.
null to unset the current parent channel handler.public Channel bind()
"localAddress" option. This method is
similar to the following code:
ServerBootstrap b = ...;
b.connect(b.getOption("localAddress"));
IllegalStateException - if "localAddress" option was not setClassCastException - if "localAddress" option's value is
neither a SocketAddress nor nullChannelException - if failed to create a new channel and
bind it to the local addresspublic Channel bind(SocketAddress localAddress)
ChannelException - if failed to create a new channel and
bind it to the local addressCopyright © 2008-2013 JBoss, by Red Hat. All Rights Reserved.