All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.tramchamploo.bufferslayer.SenderConsumerBridge Maven / Gradle / Ivy

The newest version!
package io.github.tramchamploo.bufferslayer;

import io.github.tramchamploo.bufferslayer.internal.MessagePromise;
import io.github.tramchamploo.bufferslayer.internal.Promises;
import io.reactivex.functions.Consumer;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class SenderConsumerBridge {

  private static final Logger logger = LoggerFactory.getLogger(SenderConsumerBridge.class);

  /**
   * Adapt a {@link Sender} to rx-java's {@link Consumer}
   */
  static  Consumer>> toConsumer(final Sender sender) {
    return new Consumer>>() {
      @Override
      public void accept(List> promises) {
        if (promises.isEmpty()) {
          return;
        }
        logger.debug("Sending {} messages.", promises.size());

        List messages = extractMessages(promises);
        try {
          List result = sender.send(messages);
          Promises.allSuccess(result, promises);
        } catch (Throwable t) {
          Promises.allFail(MessageDroppedException.dropped(t, messages), promises, messages);
        }
      }
    };
  }

  @SuppressWarnings("unchecked")
  private static  List extractMessages(List> promises) {
    List messages = new ArrayList<>();
    for (MessagePromise promise : promises) {
      messages.add((M) promise.message());
    }
    return messages;
  }


  private SenderConsumerBridge() {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy