Package org.playframework.netty
Class HandlerPublisher<T>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
org.playframework.netty.HandlerPublisher<T>
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler,org.reactivestreams.Publisher<T>
public class HandlerPublisher<T>
extends io.netty.channel.ChannelDuplexHandler
implements org.reactivestreams.Publisher<T>
Publisher for a Netty Handler.
This publisher supports only one subscriber.
All interactions with the subscriber are done from the handlers executor, hence, they provide the same happens before
semantics that Netty provides.
The handler publishes all messages that match the type as specified by the passed in class. Any non matching messages
are forwarded to the next handler.
The publisher will signal complete if it receives a channel inactive event.
The publisher will release any messages that it drops (for example, messages that are buffered when the subscriber
cancels), but other than that, it does not release any messages. It is up to the subscriber to release messages.
If the subscriber cancels, the publisher will send a close event up the channel pipeline.
All errors will short circuit the buffer, and cause publisher to immediately call the subscribers onError method,
dropping the buffer.
The publisher can be subscribed to or placed in a handler chain in any order.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class(package private) static enumNested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
io.netty.channel.ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ObjectUsed for buffering a completion signal.private io.netty.channel.ChannelHandlerContextprivate final io.netty.util.concurrent.EventExecutorprivate final AtomicBooleanWhether a subscriber has been provided.private final io.netty.util.internal.TypeParameterMatcherprivate Throwableprivate longprivate HandlerPublisher.Stateprivate org.reactivestreams.Subscriber<? super T> -
Constructor Summary
ConstructorsConstructorDescriptionHandlerPublisher(io.netty.util.concurrent.EventExecutor executor, Class<? extends T> subscriberMessageType) Create a handler publisher. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanReturnstrueif the given message should be handled.private booleanaddDemand(long demand) protected voidOverride to handle when a subscriber cancels the subscription.voidchannelActive(io.netty.channel.ChannelHandlerContext ctx) voidchannelInactive(io.netty.channel.ChannelHandlerContext ctx) voidchannelRead(io.netty.channel.ChannelHandlerContext ctx, Object message) voidchannelReadComplete(io.netty.channel.ChannelHandlerContext ctx) voidchannelRegistered(io.netty.channel.ChannelHandlerContext ctx) private voidcleanup()Release all elements from the buffer.private voidcomplete()voidexceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) private voidvoidhandlerAdded(io.netty.channel.ChannelHandlerContext ctx) voidhandlerRemoved(io.netty.channel.ChannelHandlerContext ctx) private voidprivate voidprovideChannelContext(io.netty.channel.ChannelHandlerContext ctx) private voidprovideSubscriber(org.reactivestreams.Subscriber<? super T> subscriber) private voidpublishMessage(Object message) private voidprivate voidreceivedDemand(long demand) protected voidOverride to intercept when demand is requested.voidprivate voidverifyRegisteredWithRightExecutor(io.netty.channel.ChannelHandlerContext ctx) Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read, writeMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelUnregistered, channelWritabilityChanged, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Field Details
-
executor
private final io.netty.util.concurrent.EventExecutor executor -
matcher
private final io.netty.util.internal.TypeParameterMatcher matcher -
buffer
-
hasSubscriber
Whether a subscriber has been provided. This is used to detect whether two subscribers are subscribing simultaneously. -
state
-
subscriber
-
ctx
private io.netty.channel.ChannelHandlerContext ctx -
outstandingDemand
private long outstandingDemand -
noSubscriberError
-
COMPLETE
Used for buffering a completion signal.
-
-
Constructor Details
-
HandlerPublisher
public HandlerPublisher(io.netty.util.concurrent.EventExecutor executor, Class<? extends T> subscriberMessageType) Create a handler publisher. The supplied executor must be the same event loop as the event loop that this handler is eventually registered with, if not, an exception will be thrown when the handler is registered.- Parameters:
executor- The executor to execute asynchronous events from the subscriber on.subscriberMessageType- The type of message this publisher accepts.
-
-
Method Details
-
acceptInboundMessage
Returnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelInboundHandlerin theChannelPipeline.- Parameters:
msg- The message to check.- Returns:
- True if the message should be accepted.
- Throws:
Exception
-
cancelled
protected void cancelled()Override to handle when a subscriber cancels the subscription. By default, this method will simply close the channel. -
requestDemand
protected void requestDemand()Override to intercept when demand is requested. By default, a channel read is invoked. -
subscribe
- Specified by:
subscribein interfaceorg.reactivestreams.Publisher<T>
-
provideSubscriber
-
handlerAdded
- Specified by:
handlerAddedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerAddedin classio.netty.channel.ChannelHandlerAdapter- Throws:
Exception
-
channelRegistered
- Specified by:
channelRegisteredin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelRegisteredin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
provideChannelContext
private void provideChannelContext(io.netty.channel.ChannelHandlerContext ctx) -
verifyRegisteredWithRightExecutor
private void verifyRegisteredWithRightExecutor(io.netty.channel.ChannelHandlerContext ctx) -
channelActive
- Specified by:
channelActivein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelActivein classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
receivedDemand
private void receivedDemand(long demand) -
addDemand
private boolean addDemand(long demand) -
illegalDemand
private void illegalDemand() -
flushBuffer
private void flushBuffer() -
receivedCancel
private void receivedCancel() -
channelRead
public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object message) throws Exception - Specified by:
channelReadin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelReadin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
publishMessage
-
channelReadComplete
- Specified by:
channelReadCompletein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelReadCompletein classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
channelInactive
- Specified by:
channelInactivein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelInactivein classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
handlerRemoved
- Specified by:
handlerRemovedin interfaceio.netty.channel.ChannelHandler- Overrides:
handlerRemovedin classio.netty.channel.ChannelHandlerAdapter- Throws:
Exception
-
complete
private void complete() -
exceptionCaught
public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception - Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelHandler- Specified by:
exceptionCaughtin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
exceptionCaughtin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
cleanup
private void cleanup()Release all elements from the buffer.
-