Package org.jboss.netty.bootstrap
Class ServerBootstrap
- java.lang.Object
-
- org.jboss.netty.bootstrap.Bootstrap
-
- org.jboss.netty.bootstrap.ServerBootstrap
-
- All Implemented Interfaces:
ExternalResourceReleasable
public class ServerBootstrap extends Bootstrap
A helper class which creates a new server-sideChanneland accepts incoming connections.Only for connection oriented transports
This bootstrap is for connection oriented transports only such as TCP/IP and local transport. UseConnectionlessBootstrapinstead 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.Parent channel and its children
A parent channel is a channel which is supposed to accept incoming connections. It is created by this bootstrap'sChannelFactoryviabind()andbind(SocketAddress).Once successfully bound, the parent channel starts to accept incoming connections, and the accepted connections become the children of the parent channel.
Configuring channels
Optionsare 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:
For the detailed list of available options, please refer toServerBootstrapb = ...; // Options for a parent channel b.setOption("localAddress", newInetSocketAddress(8080)); b.setOption("reuseAddress", true); // Options for its children b.setOption("child.tcpNoDelay", true); b.setOption("child.receiveBufferSize", 1048576);ChannelConfigand its sub-types.Configuring a parent channel pipeline
It is rare to customize the pipeline of a parent channel because what it is supposed to do is very typical. However, you might want to add a handler to deal with some special needs such as degrading the process UID from a superuser to a normal user and changing the current VM security manager for better security. To support such a case, theparentHandlerproperty is provided.Configuring a child channel pipeline
Every channel has its ownChannelPipelineand you can configure it in two ways. The recommended approach is to specify aChannelPipelineFactoryby callingBootstrap.setPipelineFactory(ChannelPipelineFactory).ServerBootstrapb = ...; 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 addedServerBootstrapb = ...;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
ChannelsServerBootstrapis just a helper class. It neither allocates nor manages any resources. What manages the resources is theChannelFactoryimplementation you specified in the constructor ofServerBootstrap. Therefore, it is OK to create as manyServerBootstrapinstances as you want with the sameChannelFactoryto apply different settings for differentChannels.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classServerBootstrap.Binder
-
Field Summary
Fields Modifier and Type Field Description private ChannelHandlerparentHandler
-
Constructor Summary
Constructors Constructor Description ServerBootstrap()Creates a new instance with noChannelFactoryset.ServerBootstrap(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.ChannelFuturebindAsync()Bind a channel asynchronous to the local address specified in the current"localAddress"option.ChannelFuturebindAsync(java.net.SocketAddress localAddress)Bind a channel asynchronous to the specified local address.ChannelHandlergetParentHandler()Returns an optionalChannelHandlerwhich intercepts an event of a newly bound server-side channel which accepts incoming connections.voidsetFactory(ChannelFactory factory)Sets theServerChannelFactorythat will be used to perform an I/O operation.voidsetParentHandler(ChannelHandler parentHandler)Sets an optionalChannelHandlerwhich intercepts an event of a newly bound server-side channel which accepts incoming connections.-
Methods inherited from class org.jboss.netty.bootstrap.Bootstrap
getFactory, getOption, getOptions, getPipeline, getPipelineAsMap, getPipelineFactory, isOrderedMap, releaseExternalResources, setOption, setOptions, setPipeline, setPipelineAsMap, setPipelineFactory, shutdown
-
-
-
-
Field Detail
-
parentHandler
private volatile ChannelHandler parentHandler
-
-
Constructor Detail
-
ServerBootstrap
public ServerBootstrap()
Creates a new instance with noChannelFactoryset.setFactory(ChannelFactory)must be called before any I/O operation is requested.
-
ServerBootstrap
public ServerBootstrap(ChannelFactory channelFactory)
Creates a new instance with the specified initialChannelFactory.
-
-
Method Detail
-
setFactory
public void setFactory(ChannelFactory factory)
Sets theServerChannelFactorythat 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.- Overrides:
setFactoryin classBootstrap- Throws:
java.lang.IllegalStateException- if the factory is already setjava.lang.IllegalArgumentException- if the specifiedfactoryis not aServerChannelFactory
-
getParentHandler
public ChannelHandler getParentHandler()
Returns an optionalChannelHandlerwhich intercepts an event of a newly bound server-side channel which accepts incoming connections.- Returns:
- the parent channel handler.
nullif no parent channel handler is set.
-
setParentHandler
public void setParentHandler(ChannelHandler parentHandler)
Sets an optionalChannelHandlerwhich intercepts an event of a newly bound server-side channel which accepts incoming connections.- Parameters:
parentHandler- the parent channel handler.nullto unset the current parent channel handler.
-
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:
This operation will block until the channel is bound.ServerBootstrapb = ...; 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. This operation will block until the channel is bound.- 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
-
bindAsync
public ChannelFuture bindAsync()
Bind a channel asynchronous to the local address specified in the current"localAddress"option. This method is similar to the following code:ServerBootstrapb = ...; b.bindAsync(b.getOption("localAddress"));- Returns:
- a new
ChannelFuturewhich will be notified once the Channel is bound and 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
-
bindAsync
public ChannelFuture bindAsync(java.net.SocketAddress localAddress)
Bind a channel asynchronous to the specified local address.- Returns:
- a new
ChannelFuturewhich will be notified once the Channel is bound and accepts incoming connections
-
-