- 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 specialChannelHandler
which offers an easy way to initialize aChannel
once it was registered to itsEventLoop
. Implementations are most often used in the context ofAbstractBootstrap.handler(ChannelHandler)
,AbstractBootstrap.handler(ChannelHandler)
andServerBootstrap.childHandler(ChannelHandler)
to setup theChannelPipeline
of aChannel
.public class MyChannelInitializer extends
Be aware that this class is marked asChannelInitializer
{ public void initChannel(Channel
channel) { channel.pipeline().addLast("myHandler", new MyHandler()); } }ServerBootstrap
bootstrap = ...; ... 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 void
channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause)
void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.protected abstract void
initChannel(C ch)
This method will be called once theChannel
was registered.boolean
isSharable()
Returnstrue
if 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:ChannelHandler
Returnstrue
if 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:
isSharable
in interfaceChannelHandler
-
initChannel
protected abstract void initChannel(C ch) throws Exception
This method will be called once theChannel
was registered. After the method returns this instance will be removed from theChannelPipeline
of theChannel
.- Parameters:
ch
- theChannel
which 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:
channelExceptionCaught
in interfaceChannelHandler
- Throws:
Exception
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events. If override this method ensure you call super!- Specified by:
handlerAdded
in interfaceChannelHandler
- Throws:
Exception
-
-