Class HandlerPublisher<T>

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.micronaut.http.netty.reactive.HandlerPublisher<T>
Type Parameters:
T - The publisher type
All Implemented Interfaces:
HotObservable<T>, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, io.netty.channel.ChannelOutboundHandler, Publisher<T>

@Internal public abstract class HandlerPublisher<T> extends io.netty.channel.ChannelDuplexHandler implements HotObservable<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.

Since:
1.0
Author:
Graeme Rocher
  • Nested Class Summary

    Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

    io.netty.channel.ChannelHandler.Sharable
  • Constructor Summary

    Constructors
    Constructor
    Description
    HandlerPublisher(io.netty.util.concurrent.EventExecutor executor)
    Create a handler publisher.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract boolean
    Returns true if the given message should be handled.
    protected void
    Override to handle when a subscriber cancels the subscription.
    void
    channelActive(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    channelInactive(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    channelRead(io.netty.channel.ChannelHandlerContext ctx, Object message)
     
    void
    channelRegistered(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    Releases buffered data if there is no subscriber.
    void
    exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
     
    void
    handlerAdded(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
     
    protected void
    Override to intercept when demand is requested.
    void
    subscribe(Subscriber<? super T> subscriber)
     

    Methods inherited from class io.netty.channel.ChannelDuplexHandler

    bind, close, connect, deregister, disconnect, flush, read, write

    Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

    channelReadComplete, channelUnregistered, channelWritabilityChanged, userEventTriggered

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, isSharable

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HandlerPublisher

      public HandlerPublisher(io.netty.util.concurrent.EventExecutor executor)
      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.
  • Method Details

    • subscribe

      public void subscribe(Subscriber<? super T> subscriber)
      Specified by:
      subscribe in interface Publisher<T>
    • acceptInboundMessage

      protected abstract boolean acceptInboundMessage(Object msg)
      Returns true if the given message should be handled. If false it will be passed to the next ChannelInboundHandler in the ChannelPipeline.
      Parameters:
      msg - The message to check.
      Returns:
      True if the message should be accepted.
    • 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.

    • handlerAdded

      public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      handlerAdded in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerAdded in class io.netty.channel.ChannelHandlerAdapter
    • channelRegistered

      public void channelRegistered(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      channelRegistered in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelRegistered in class io.netty.channel.ChannelInboundHandlerAdapter
    • channelActive

      public void channelActive(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      channelActive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelActive in class io.netty.channel.ChannelInboundHandlerAdapter
    • channelRead

      public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object message)
      Specified by:
      channelRead in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelRead in class io.netty.channel.ChannelInboundHandlerAdapter
    • channelInactive

      public void channelInactive(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      channelInactive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
    • handlerRemoved

      public void handlerRemoved(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      handlerRemoved in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
    • exceptionCaught

      public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelHandler
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      exceptionCaught in class io.netty.channel.ChannelInboundHandlerAdapter
    • closeIfNoSubscriber

      public void closeIfNoSubscriber()
      Description copied from interface: HotObservable
      Releases buffered data if there is no subscriber.
      Specified by:
      closeIfNoSubscriber in interface HotObservable<T>