PacketConsumerCaller


@ExperimentalApi
public final class PacketConsumerCaller<T extends Object>


A utility that connects a producer of Packet of type T to a PacketConsumer via a Channel.

This class is experimental and will be renamed or removed in a future release.

The owner of this class is responsible for invoking the run method, which will start sending the queued Packet to the wrapped PacketConsumer. The owner is also responsible for calling release to close the underlying Channel once this consumer is no longer needed.

Summary

Public methods

static final @NonNull PacketConsumerCaller<@NonNull T>
<T extends Object> create(
    @NonNull PacketConsumer<@NonNull T> packetConsumer,
    @NonNull ExecutorService executorService,
    @NonNull Consumer<@NonNull Exception> errorConsumer
)

Creates a PacketConsumerCaller instance.

final @NonNull ListenableFuture<Void>

Queues a Packet for processing.

final void

Releases the internal Channel and cancels coroutine to queue Packet to the wrapped PacketConsumer.

final void
run()

Start sending queued Packet the wrapped PacketConsumer

final boolean

Attempts to queue a Packet for processing without suspending.

Public methods

create

public static final @NonNull PacketConsumerCaller<@NonNull T> <T extends Object> create(
    @NonNull PacketConsumer<@NonNull T> packetConsumer,
    @NonNull ExecutorService executorService,
    @NonNull Consumer<@NonNull Exception> errorConsumer
)

Creates a PacketConsumerCaller instance.

Parameters
@NonNull PacketConsumer<@NonNull T> packetConsumer

The PacketConsumer to wrap.

@NonNull ExecutorService executorService

The ExecutorService on which the PacketConsumer.queuePacket is invoked.

@NonNull Consumer<@NonNull Exception> errorConsumer

A Consumer for thrown Exception.

queuePacket

public final @NonNull ListenableFuture<VoidqueuePacket(@NonNull PacketConsumer.Packet<@NonNull T> packet)

Queues a Packet for processing.

The ownership of the packet is transferred to the wrapped PacketConsumer and the caller must not modify the packet.

release

public final void release()

Releases the internal Channel and cancels coroutine to queue Packet to the wrapped PacketConsumer.

run

public final void run()

Start sending queued Packet the wrapped PacketConsumer

tryQueuePacket

public final boolean tryQueuePacket(@NonNull PacketConsumer.Packet<@NonNull T> packet)

Attempts to queue a Packet for processing without suspending.

If this method returns true, the ownership of the packet is transferred to the wrapped PacketConsumer and the caller must not modify the packet. If returns false, it means the consumer is at capacity and cannot accept the packet at the moment.

Throws
ClosedSendChannelException

If the channel to send packet to the PacketConsumer is closed.