Class Emitter<PAYLOAD>

java.lang.Object
io.helidon.messaging.Emitter<PAYLOAD>
Type Parameters:
PAYLOAD - message payload type
All Implemented Interfaces:
Emitter<PAYLOAD>, Publisher<Message<PAYLOAD>>

public final class Emitter<PAYLOAD> extends Object implements Publisher<Message<PAYLOAD>>, Emitter<PAYLOAD>
Emitter is convenience publisher for one or multiple channels, publishing is as easy as calling send(Object) method.

  Channel<String> simpleChannel = Channel.create();

  Emitter<String> emitter = Emitter.create(simpleChannel);

  Messaging messaging = Messaging.builder()
          .emitter(emitter)
          .listener(simpleChannel, System.out::println)
          .build();

  messaging.start();

  emitter.send(Message.of("Hello world!"));
 
  • Method Details

    • send

      public CompletionStage<Void> send(PAYLOAD msg)
      Send raw payload to downstream, wrapped to Message when demand is higher than 0. Publishes the given item to each current subscriber by asynchronously invoking its onNext method, blocking uninterruptibly while resources for any subscriber are unavailable.
      Specified by:
      send in interface Emitter<PAYLOAD>
      Parameters:
      msg - payload to be wrapped and sent(or buffered if there is no demand)
      Returns:
      callback being invoked when message is acked
    • send

      public <M extends Message<? extends PAYLOAD>> void send(M msg)
      Specified by:
      send in interface Emitter<PAYLOAD>
    • emit

      @Deprecated(forRemoval=true, since="3.0.0") public int emit(Message<PAYLOAD> msg)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Send Message to downstream when demand is higher than 0. Publishes the given item to each current subscriber by asynchronously invoking its onNext method, blocking uninterruptibly while resources for any subscriber are unavailable.
      Parameters:
      msg - message wrapper with payload
      Returns:
      estimate of the maximum lag (number of items submitted but not yet consumed) among all current subscribers. This value is at least one (accounting for this submitted item) if there are any subscribers, else zero.
      Throws:
      IllegalStateException - if emitter has been already completed
      NullPointerException - if message is null
      RejectedExecutionException - if thrown by Executor
    • complete

      public void complete()
      Send onComplete signal to all subscribers.
      Specified by:
      complete in interface Emitter<PAYLOAD>
    • error

      public void error(Exception e)
      Send onError signal to all subscribers.
      Specified by:
      error in interface Emitter<PAYLOAD>
      Parameters:
      e - error to send in onError signal downstream
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface Emitter<PAYLOAD>
    • hasRequests

      public boolean hasRequests()
      Specified by:
      hasRequests in interface Emitter<PAYLOAD>
    • subscribe

      public void subscribe(Subscriber<? super Message<PAYLOAD>> s)
      Specified by:
      subscribe in interface Publisher<PAYLOAD>
    • create

      public static <PAYLOAD> Emitter<PAYLOAD> create(Channel<PAYLOAD> channel)
      Create new Emitter to serve as a publisher for supplied channel.
      Type Parameters:
      PAYLOAD - message payload type
      Parameters:
      channel - to serve as publisher in
      Returns:
      new emitter
    • create

      public static <PAYLOAD> Emitter<PAYLOAD> create(Channel<PAYLOAD> channel, Channel<PAYLOAD>... channels)
      Create new Emitter to serve as a broadcast publisher for supplied channels.
      Type Parameters:
      PAYLOAD - message payload type
      Parameters:
      channel - to serve as publisher in
      channels - to serve as publisher for
      Returns:
      new emitter
    • builder

      public static <PAYLOAD> Emitter.Builder<PAYLOAD> builder()
      Prepare new builder for Emitter construction.
      Type Parameters:
      PAYLOAD - message payload type
      Returns:
      new emitter builder