- java.lang.Object
-
- io.netty5.channel.ChannelInitializer<C>
-
- Type Parameters:
C- A sub-type ofChannel
- All Implemented Interfaces:
ChannelHandler
public abstract class ChannelInitializer<C extends Channel> extends Object implements ChannelHandler
A specialChannelHandlerwhich offers an easy way to initialize aChannelonce it was registered to itsEventLoop. Implementations are most often used in the context ofAbstractBootstrap.handler(ChannelHandler),AbstractBootstrap.handler(ChannelHandler)andServerBootstrap.childHandler(ChannelHandler)to setup theChannelPipelineof aChannel.public class MyChannelInitializer extends
Be aware that this class is marked asChannelInitializer{ public void initChannel(Channelchannel) { channel.pipeline().addLast("myHandler", new MyHandler()); } }ServerBootstrapbootstrap = ...; ... bootstrap.childHandler(new MyChannelInitializer()); ...isSharable()and so the implementation must be safe to be re-used.
-
-
Constructor Summary
Constructors Constructor Description ChannelInitializer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidchannelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)voidhandlerAdded(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.protected abstract voidinitChannel(C ch)This method will be called once theChannelwas registered.booleanisSharable()Returnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelInactive, channelInboundEvent, channelRead, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerRemoved, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown, write
-
-
-
-
Method Detail
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandlerReturnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline. By default, this method returnsfalse. If this method returnsfalse, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.- Specified by:
isSharablein interfaceChannelHandler
-
initChannel
protected abstract void initChannel(C ch) throws Exception
This method will be called once theChannelwas registered. After the method returns this instance will be removed from theChannelPipelineof theChannel.- Parameters:
ch- theChannelwhich was registered.- Throws:
Exception- is thrown if an error occurs. In that case it will be handled bychannelExceptionCaught(ChannelHandlerContext, Throwable)which will by default close theChannel.
-
channelExceptionCaught
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
- Specified by:
channelExceptionCaughtin interfaceChannelHandler- Throws:
Exception
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events. If override this method ensure you call super!- Specified by:
handlerAddedin interfaceChannelHandler- Throws:
Exception
-
-