public class ConnectionlessBootstrap extends Bootstrap
Channel
for a
connectionless transport.
ServerBootstrap
instead for connection oriented transports.
Do not use this helper if you are using a connection oriented transport such
as TCP/IP and local transport which accepts an incoming connection and lets
the accepted child channels handle received messages.
Options
are used to configure a channel:
For the detailed list of available options, please refer toConnectionlessBootstrap
b = ...; // Options for a new channel b.setOption("localAddress", newInetSocketAddress
(8080)); b.setOption("tcpNoDelay", true); b.setOption("receiveBufferSize", 1048576);
ChannelConfig
and its sub-types.
ChannelPipeline
and you can configure it
in two ways.
The recommended approach is to specify a ChannelPipelineFactory
by
calling Bootstrap.setPipelineFactory(ChannelPipelineFactory)
.
ConnectionlessBootstrap
b = ...; b.setPipelineFactory(new MyPipelineFactory()); public class MyPipelineFactory implementsChannelPipelineFactory
{ publicChannelPipeline
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; } }
The alternative approach, which works only in a certain situation, is to use the default pipeline and let the bootstrap to shallow-copy the default pipeline for each new channel:
Please note 'shallow-copy' here means that the addedConnectionlessBootstrap
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());
ChannelHandler
s
are not cloned but only their references are added to the new pipeline.
Therefore, you cannot use this approach if you are going to open more than
one Channel
s or run a server that accepts incoming connections to
create its child channels.
Channel
sConnectionlessBootstrap
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
ConnectionlessBootstrap
. Therefore, it is OK to create as
many ConnectionlessBootstrap
instances as you want with the same
ChannelFactory
to apply different settings for different
Channel
s.构造器和说明 |
---|
ConnectionlessBootstrap()
Creates a new instance with no
ChannelFactory set. |
ConnectionlessBootstrap(ChannelFactory channelFactory)
Creates a new instance with the specified initial
ChannelFactory . |
限定符和类型 | 方法和说明 |
---|---|
Channel |
bind()
Creates a new channel which is bound to the local address which was
specified in the current
"localAddress" option. |
Channel |
bind(java.net.SocketAddress localAddress)
Creates a new channel which is bound to the specified local address.
|
ChannelFuture |
connect()
Creates a new connected channel with the current
"remoteAddress"
and "localAddress" option. |
ChannelFuture |
connect(java.net.SocketAddress remoteAddress)
Creates a new connected channel with the specified
"remoteAddress" and the current "localAddress" option. |
ChannelFuture |
connect(java.net.SocketAddress remoteAddress,
java.net.SocketAddress localAddress)
Creates a new connected channel with the specified
"remoteAddress" and the specified "localAddress" . |
getFactory, getOption, getOptions, getPipeline, getPipelineAsMap, getPipelineFactory, releaseExternalResources, setFactory, setOption, setOptions, setPipeline, setPipelineAsMap, setPipelineFactory, shutdown
public ConnectionlessBootstrap()
ChannelFactory
set.
Bootstrap.setFactory(ChannelFactory)
must be called before any I/O
operation is requested.public ConnectionlessBootstrap(ChannelFactory channelFactory)
ChannelFactory
.public Channel bind()
"localAddress"
option. This method is
similar to the following code:
ConnectionlessBootstrap
b = ...;
b.bind(b.getOption("localAddress"));
java.lang.IllegalStateException
- if "localAddress"
option was not setjava.lang.ClassCastException
- if "localAddress"
option's value is
neither a SocketAddress
nor null
ChannelException
- if failed to create a new channel and
bind it to the local addresspublic Channel bind(java.net.SocketAddress localAddress)
ChannelException
- if failed to create a new channel and
bind it to the local addresspublic 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:
ConnectionlessBootstrap
b = ...;
b.connect(b.getOption("remoteAddress"), b.getOption("localAddress"));
java.lang.IllegalStateException
- if "remoteAddress"
option was not setjava.lang.ClassCastException
- 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(java.net.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:
ConnectionlessBootstrap
b = ...;
b.connect(remoteAddress, b.getOption("localAddress"));
java.lang.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(java.net.SocketAddress remoteAddress, java.net.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