/** * <pre> * {@code * ReactiveSeq.of(1,2,3) .flatMapP(i->ReactiveSeq.range(i,1500),1000,QueueFactories.unboundedQueue()) .listX() * } * </pre> * * @return A QueueFactory for unbounded Queues backed by a LinkedBlockingQueue */ public static <T> QueueFactory<T> unboundedQueue() { return () -> new Queue<T>(); }
/** * Creates an async.Queue backed by a JDK Wait Free unbounded ConcurrentLinkedQueue * The provided WaitStrategy is used to determine behaviour of both producers and consumers when the Queue is full (producer) * or zero (consumer). {@see WaitStrategy#spinWait() , @see WaitStrategy#exponentialBackOff() , @see WaitStrategy#noWaitRetry() } * * @param strategy Strategy to be employed by producers when Queue is full, or consumers when Queue is zero * @return Factory for unbounded wait free queue backed by ConcurrentLinkedQueue */ public static <T> QueueFactory<T> unboundedNonBlockingQueue(final WaitStrategy<T> strategy) { return () -> new Queue<T>( new ConcurrentLinkedQueue<>(), strategy, strategy); }
/** * @return async.Queue backed by a Synchronous Queue */ public static <T> QueueFactory<T> synchronousQueue() { return () -> new Queue<T>( new SynchronousQueue<>()); }
public static <T> Queue<T> createMergeQueue() { final Queue<T> q = new Queue<>(); q.continuationStrategy = new AdaptersModule.StreamOfContinuations( q); return q; }
/** * Create a QueueFactory for boundedQueues where bound is determined by the provided queueSize parameter * Generated Queues will be backed by a LinkedBlockingQueue * * <pre> * {@code * Queue<String> transferQueue = QueueFactories.<String>boundedQueue(4) .build(); new LazyReact(Executors.newFixedThreadPool(4)).generate(()->"data") .map(d->"emitted on " + Thread.currentThread().getId()) .peek(System.out::println) .peek(d->transferQueue.offer(d)) .run(); transferQueue.stream() .map(e->"Consumed on " + Thread.currentThread().getId()) .futureOperations(Executors.newFixedThreadPool(1)) .forEach(System.out::println); * * } * </pre> * * @param queueSize Max queue size * @return QueueFactory for bounded Queues backed by a LinkedBlockingQueue */ public static <T> QueueFactory<T> boundedQueue(final int queueSize) { return () -> new Queue<T>( new LinkedBlockingQueue<>( queueSize)); }
/** * Construct a new Topic */ public Topic() { final Queue<T> q = new Queue<T>(); factory = QueueFactories.unboundedQueue(); distributor.addQueue(q); }
/** * Generate QueueFactory for bounded non blocking queues. Max queue size is determined by the input parameter. * The provided WaitStrategy is used to determine behaviour of both producers and consumers when the Queue is full (producer) * or zero (consumer). {@see WaitStrategy#spinWait() , @see WaitStrategy#exponentialBackOff() , @see WaitStrategy#noWaitRetry() } * * @param queueSize Max Queue size * @param strategy Strategy to be employed by producers when Queue is full, or consumers when Queue is zero * @return bounded wait free Queue Factory backed by an Agrona OneToOneConcurrentArrayQueue */ public static <T> QueueFactory<T> singleWriterboundedNonBlockingQueue(final int queueSize, final WaitStrategy<T> strategy) { return () -> new Queue<T>( new OneToOneConcurrentArrayQueue<>( queueSize), strategy, strategy); }
/** * Generate QueueFactory for bounded non blocking queues. Max queue size is determined by the input parameter. * The provided WaitStrategy is used to determine behaviour of both producers and consumers when the Queue is full (producer) * or zero (consumer). {@see WaitStrategy#spinWait() , @see WaitStrategy#exponentialBackOff() , @see WaitStrategy#noWaitRetry() } * * @param queueSize Max Queue size * @param strategy Strategy to be employed by producers when Queue is full, or consumers when Queue is zero * @return bounded wait free Queue Factory backed by an Agrona ManyToOneConcurrentArrayQueue */ public static <T> QueueFactory<T> boundedNonBlockingQueue(final int queueSize, final WaitStrategy<T> strategy) { return () -> new Queue<T>( new ManyToOneConcurrentArrayQueue<>( queueSize), strategy, strategy); }
/** * Creates an async.Queue backed by a JDK Wait Free unbounded ConcurrentLinkedQueue * Wait strategy used is NoWaitRetry by default for both Consumers and Producers * (both Consumers and Producers will repeatedly retry until successful). Use * withConsumerWaitStrategy & withProducerWaitStrategy methods on the returned queue to change the * wait strategy * <pre> * {@code * queue.withConsumerWaitStrategy(new DirectWaitStrategy()) * .withProducerWaitStrategy(new YieldWait()); * }</pre> * * * @return Factory for unbounded wait free queue backed by ConcurrentLinkedQueue */ public static <T> QueueFactory<T> unboundedNonBlockingQueue() { return () -> new Queue<T>( new ConcurrentLinkedQueue<>(), new NoWaitRetry<>(), new NoWaitRetry<>()); }
/** * Topic will maintain a queue for each Subscribing Stream * If a Stream is finished with a Topic it is good practice to disconnect from the Topic * so messages will no longer be stored for that Stream * * @param stream */ @Synchronized("lock") public void disconnect(final ReactiveSeq<T> stream) { Option<Queue<T>> o = streamToQueue.get(stream); distributor.removeQueue(streamToQueue.getOrElse(stream, new Queue<>())); this.streamToQueue = streamToQueue.remove(stream); this.index--; }
/** * Creates an async.Queue backed by an Agrona ManyToOneConcurrentArrayQueue bounded by specified queueSize * Wait strategy used is NoWaitRetry by default for both Consumers and Producers * (both Consumers and Producers will repeatedly retry until successful). Use * withConsumerWaitStrategy & withProducerWaitStrategy methods on the returned queue to change the * wait strategy * <pre> * {@code * queue.withConsumerWaitStrategy(new DirectWaitStrategy()) * .withProducerWaitStrategy(new YieldWait()); * }</pre> * * @param queueSize upper bound for Queue * @return bounded wait free Queue Factory backed by an Agrona ManyToOneConcurrentArrayQueue */ public static <T> QueueFactory<T> boundedNonBlockingQueue(final int queueSize) { return () -> new Queue<T>( new ManyToOneConcurrentArrayQueue<>( queueSize), new NoWaitRetry<>(), new NoWaitRetry<>()); }
/** * Creates an async.Queue backed by an Agrona OneToOneConcurrentArrayQueue bounded by specified queueSize * Wait strategy used is NoWaitRetry by default for both Consumers and Producers * (both Consumers and Producers will repeatedly retry until successful). Use * withConsumerWaitStrategy & withProducerWaitStrategy methods on the returned queue to change the * wait strategy * <pre> * {@code * queue.withConsumerWaitStrategy(new DirectWaitStrategy()) * .withProducerWaitStrategy(new YieldWait()); * }</pre> * * @param queueSize * @return */ public static <T> QueueFactory<T> singleWriterboundedNonBlockingQueue(final int queueSize) { return () -> new Queue<T>( new OneToOneConcurrentArrayQueue<>( queueSize), new NoWaitRetry<>(), new NoWaitRetry<>()); } /**
/** * @return async.Queue backed by a Synchronous Queue */ public static <T> QueueFactory<T> synchronousQueue() { return () -> new Queue<T>( new SynchronousQueue<>()); }
/** * Creates an async.Queue backed by a JDK Wait Free unbounded ConcurrentLinkedQueue * The provided WaitStrategy is used to determine behaviour of both producers and consumers when the Queue is full (producer) * or zero (consumer). {@see WaitStrategy#spinWait() , @see WaitStrategy#exponentialBackOff() , @see WaitStrategy#noWaitRetry() } * * @param strategy Strategy to be employed by producers when Queue is full, or consumers when Queue is zero * @return Factory for unbounded wait free queue backed by ConcurrentLinkedQueue */ public static <T> QueueFactory<T> unboundedNonBlockingQueue(final WaitStrategy<T> strategy) { return () -> new Queue<T>( new ConcurrentLinkedQueue<>(), strategy, strategy); }
public static <T> Queue<T> createMergeQueue() { final Queue<T> q = new Queue<>(); q.continuationStrategy = new AdaptersModule.StreamOfContinuations( q); return q; }
@Test public void queueTest(){ com.oath.cyclops.async.adapters.Queue<Integer> q = new Queue<>(); q.add(1); q.add(2); q.add(3); q.stream().limit(3).forEach(System.out::println); q.add(4); q.add(5); q.stream().limit(2).forEach(System.out::println); } @Test
/** * Construct a new Topic */ public Topic() { final Queue<T> q = new Queue<T>(); factory = QueueFactories.unboundedQueue(); distributor.addQueue(q); }
/** * Topic will maintain a queue for each Subscribing Stream * If a Stream is finished with a Topic it is good practice to disconnect from the Topic * so messages will no longer be stored for that Stream * * @param stream */ @Synchronized("lock") public void disconnect(final ReactiveSeq<T> stream) { Option<Queue<T>> o = streamToQueue.get(stream); distributor.removeQueue(streamToQueue.getOrElse(stream, new Queue<>())); this.streamToQueue = streamToQueue.remove(stream); this.index--; }