|
1 | 1 | package reactor.core.scala.publisher |
2 | 2 |
|
| 3 | +import java.util |
| 4 | +import java.util.function.Supplier |
| 5 | + |
| 6 | +import org.reactivestreams.Publisher |
3 | 7 | import reactor.core.publisher.{ParallelFlux => JParallelFlux} |
| 8 | +import reactor.util.concurrent.Queues |
4 | 9 |
|
5 | | -class SParallelFlux[T]private(private val jParallelFlux: JParallelFlux[T]) { |
| 10 | +class SParallelFlux[T] private(private val jParallelFlux: JParallelFlux[T]) { |
6 | 11 | def asJava: JParallelFlux[T] = jParallelFlux |
7 | 12 | } |
8 | 13 |
|
9 | 14 | object SParallelFlux { |
10 | 15 | def apply[T](jParallelFlux: JParallelFlux[T]) = new SParallelFlux[T](jParallelFlux) |
| 16 | + |
| 17 | + /** |
| 18 | + * Take a Publisher and prepare to consume it on multiple 'rails' (one per CPU core) |
| 19 | + * in a round-robin fashion. |
| 20 | + * |
| 21 | + * @tparam T the value type |
| 22 | + * @param source the source Publisher |
| 23 | + * @param parallelism the number of parallel rails |
| 24 | + * @param prefetch the number of values to prefetch from the source |
| 25 | + * @param queueSupplier the queue structure supplier to hold the prefetched values |
| 26 | + * from the source until there is a rail ready to process it. |
| 27 | + * @return the [[SParallelFlux]] instance |
| 28 | + */ |
| 29 | + def from[T](source: Publisher[_ <: T], |
| 30 | + parallelism: Int = Runtime.getRuntime.availableProcessors(), |
| 31 | + prefetch: Int = Queues.SMALL_BUFFER_SIZE, |
| 32 | + queueSupplier: Supplier[util.Queue[T]] = Queues.small()) = SParallelFlux(JParallelFlux.from(source, parallelism, prefetch, queueSupplier)) |
| 33 | + |
| 34 | + /** |
| 35 | + * Wraps multiple Publishers into a [[SParallelFlux]] which runs them in parallel and |
| 36 | + * unordered. |
| 37 | + * |
| 38 | + * @tparam T the value type |
| 39 | + * @param publishers the array of publishers |
| 40 | + * @return the [[SParallelFlux]] instance |
| 41 | + */ |
| 42 | + def fromPublishers[T](publishers: Publisher[T]*) = SParallelFlux(JParallelFlux.from(publishers: _*)) |
11 | 43 | } |
0 commit comments