Class AbstractChannel<OUT,IN>

java.lang.Object
io.bytestreams.exchange.core.AbstractChannel<OUT,IN>
Type Parameters:
OUT - the outbound (written) message type
IN - the inbound (read) message type
All Implemented Interfaces:
Channel
Direct Known Subclasses:
MultiplexedChannel, PipelinedChannel, ServerChannel

public abstract class AbstractChannel<OUT,IN> extends Object implements Channel
Base class for Transport-backed channels with managed I/O loops.

Provides write queue, read/write virtual threads, OTel channel span and metrics, graceful close semantics, and error handling. Subclasses implement onInbound(IN), hasPending(), stopOnInboundError(java.lang.Exception, IN), and stopOnOutboundError(java.lang.Exception, OUT).

Lifecycle: construct → start()close(). The channel is not usable until start() is called, which starts the reader and writer virtual threads.

  • Field Details

    • DEFAULT_ERROR_BACKOFF_NANOS

      public static final long DEFAULT_ERROR_BACKOFF_NANOS
    • SHUTTING_DOWN

      protected static final Set<ChannelStatus> SHUTTING_DOWN
    • writeQueue

      protected final LinkedBlockingQueue<OUT> writeQueue
    • writeQueueSize

      protected final io.opentelemetry.api.metrics.LongUpDownCounter writeQueueSize
    • transport

      protected final Transport transport
    • readFuture

      protected final CompletableFuture<Void> readFuture
    • requestActive

      protected final io.opentelemetry.api.metrics.LongUpDownCounter requestActive
    • requestTotal

      protected final io.opentelemetry.api.metrics.LongCounter requestTotal
    • requestErrors

      protected final io.opentelemetry.api.metrics.LongCounter requestErrors
    • requestDuration

      protected final io.opentelemetry.api.metrics.DoubleHistogram requestDuration
    • channelSpan

      protected final io.opentelemetry.api.trace.Span channelSpan
    • status

      protected final AtomicReference<ChannelStatus> status
    • closeFuture

      protected final CompletableFuture<Void> closeFuture
    • defaultTimeout

      protected final Duration defaultTimeout
    • meterAttributes

      protected final io.opentelemetry.api.common.Attributes meterAttributes
  • Method Details

    • id

      public String id()
      Description copied from interface: Channel
      Returns a stable identifier for this channel.
      Specified by:
      id in interface Channel
      Returns:
      the channel identifier
    • status

      public ChannelStatus status()
      Description copied from interface: Channel
      Returns the current lifecycle status of this channel.
      Specified by:
      status in interface Channel
      Returns:
      the channel status
    • start

      public void start()
      Description copied from interface: Channel
      Starts the channel's I/O loops. Must be called exactly once after construction.
      Specified by:
      start in interface Channel
    • closeFuture

      public CompletableFuture<Void> closeFuture()
      Returns a defensive copy of the close future. Each call returns a new instance; all copies complete when the channel finishes shutting down.
      Specified by:
      closeFuture in interface Channel
      Returns:
      a read-only view of the close future
    • close

      public CompletableFuture<Void> close()
      Description copied from interface: Channel
      Closes this channel gracefully.
      Specified by:
      close in interface Channel
      Returns:
      a future that completes when the channel is fully closed
    • interruptIfDrained

      protected void interruptIfDrained()
      Interrupts the reader thread if the channel is shutting down and no work remains.
    • buildMessageAttributes

      protected io.opentelemetry.api.common.Attributes buildMessageAttributes(Object message)
      Returns metric attributes for a message, including message_type set to the simple class name of the given message. Subclasses use this to build request-scoped metric attributes.
    • hasPending

      protected abstract boolean hasPending()
    • onInbound

      protected abstract void onInbound(IN inbound)