Package org.jboss.netty.bootstrap
Class ConnectionlessBootstrap
- java.lang.Object
-
- org.jboss.netty.bootstrap.Bootstrap
-
- org.jboss.netty.bootstrap.ConnectionlessBootstrap
-
- All Implemented Interfaces:
ExternalResourceReleasable
public class ConnectionlessBootstrap extends Bootstrap
A helper class which creates a new server-sideChannelfor a connectionless transport.Only for connectionless transports
This bootstrap is for connectionless transports only such as UDP/IP. UseServerBootstrapinstead 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.Configuring channels
Optionsare used to configure a channel:
For the detailed list of available options, please refer toConnectionlessBootstrapb = ...; // Options for a new channel b.setOption("localAddress", newInetSocketAddress(8080)); b.setOption("tcpNoDelay", true); b.setOption("receiveBufferSize", 1048576);ChannelConfigand its sub-types.Configuring a channel pipeline
Every channel has its ownChannelPipelineand you can configure it in two ways. The recommended approach is to specify aChannelPipelineFactoryby callingBootstrap.setPipelineFactory(ChannelPipelineFactory).ConnectionlessBootstrapb = ...; b.setPipelineFactory(new MyPipelineFactory()); public class MyPipelineFactory implementsChannelPipelineFactory{ publicChannelPipelinegetPipeline() throws Exception { // Create and configure a new pipeline for a new channel.ChannelPipelinep =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 addedConnectionlessBootstrapb = ...;ChannelPipelinep = b.getPipeline(); // Add handlers to the default pipeline. p.addLast("encoder", new EncodingHandler()); p.addLast("decoder", new DecodingHandler()); p.addLast("logic", new LogicHandler());ChannelHandlers 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 oneChannels or run a server that accepts incoming connections to create its child channels.Applying different settings for different
ChannelsConnectionlessBootstrapis just a helper class. It neither allocates nor manages any resources. What manages the resources is theChannelFactoryimplementation you specified in the constructor ofConnectionlessBootstrap. Therefore, it is OK to create as manyConnectionlessBootstrapinstances as you want with the sameChannelFactoryto apply different settings for differentChannels.
-
-
Constructor Summary
Constructors Constructor Description ConnectionlessBootstrap()Creates a new instance with noChannelFactoryset.ConnectionlessBootstrap(ChannelFactory channelFactory)Creates a new instance with the specified initialChannelFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Channelbind()Creates a new channel which is bound to the local address which was specified in the current"localAddress"option.Channelbind(java.net.SocketAddress localAddress)Creates a new channel which is bound to the specified local address.ChannelFutureconnect()Creates a new connected channel with the current"remoteAddress"and"localAddress"option.ChannelFutureconnect(java.net.SocketAddress remoteAddress)Creates a new connected channel with the specified"remoteAddress"and the current"localAddress"option.ChannelFutureconnect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)Creates a new connected channel with the specified"remoteAddress"and the specified"localAddress".-
Methods inherited from class org.jboss.netty.bootstrap.Bootstrap
getFactory, getOption, getOptions, getPipeline, getPipelineAsMap, getPipelineFactory, isOrderedMap, releaseExternalResources, setFactory, setOption, setOptions, setPipeline, setPipelineAsMap, setPipelineFactory, shutdown
-
-
-
-
Constructor Detail
-
ConnectionlessBootstrap
public ConnectionlessBootstrap()
Creates a new instance with noChannelFactoryset.Bootstrap.setFactory(ChannelFactory)must be called before any I/O operation is requested.
-
ConnectionlessBootstrap
public ConnectionlessBootstrap(ChannelFactory channelFactory)
Creates a new instance with the specified initialChannelFactory.
-
-
Method Detail
-
bind
public Channel bind()
Creates a new channel which is bound to the local address which was specified in the current"localAddress"option. This method is similar to the following code:ConnectionlessBootstrapb = ...; b.bind(b.getOption("localAddress"));- Returns:
- a new bound channel which accepts incoming connections
- Throws:
java.lang.IllegalStateException- if"localAddress"option was not setjava.lang.ClassCastException- if"localAddress"option's value is neither aSocketAddressnornullChannelException- if failed to create a new channel and bind it to the local address
-
bind
public Channel bind(java.net.SocketAddress localAddress)
Creates a new channel which is bound to the specified local address.- Returns:
- a new bound channel which accepts incoming connections
- Throws:
ChannelException- if failed to create a new channel and bind it to the local address
-
connect
public ChannelFuture connect()
Creates a new connected channel with the current"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:ConnectionlessBootstrapb = ...; b.connect(b.getOption("remoteAddress"), b.getOption("localAddress"));- Returns:
- a future object which notifies when the creation of the connected channel succeeds or fails
- Throws:
java.lang.IllegalStateException- if"remoteAddress"option was not setjava.lang.ClassCastException- if"remoteAddress"or"localAddress"option's value is neither aSocketAddressnornullChannelPipelineException- if this bootstrap'spipelineFactoryfailed to create a newChannelPipeline
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress)
Creates a new connected channel with the specified"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:ConnectionlessBootstrapb = ...; b.connect(remoteAddress, b.getOption("localAddress"));- Returns:
- a future object which notifies when the creation of the connected channel succeeds or fails
- Throws:
java.lang.ClassCastException- if"localAddress"option's value is neither aSocketAddressnornullChannelPipelineException- if this bootstrap'spipelineFactoryfailed to create a newChannelPipeline
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)
Creates a new connected channel with the specified"remoteAddress"and the specified"localAddress". If the specified local address isnull, the local address of a new channel is determined automatically.- Returns:
- a future object which notifies when the creation of the connected channel succeeds or fails
- Throws:
ChannelPipelineException- if this bootstrap'spipelineFactoryfailed to create a newChannelPipeline
-
-