Class Emitter<PAYLOAD>

  • Type Parameters:
    PAYLOAD - message payload type
    All Implemented Interfaces:
    Publisher<Message<PAYLOAD>>

    public final class Emitter<PAYLOAD>
    extends Object
    implements Publisher<Message<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 Detail

      • 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.
        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 int send​(Message<PAYLOAD> msg)
        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.
      • error

        public void error​(Exception e)
        Send onError signal to all subscribers.
        Parameters:
        e - error to send in onError signal downstream
      • 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