public class StreamSource
extends java.lang.Object
PushableStream<Integer> pushable = StreamSource.ofUnbounded()
.stream();
pushable.getInput()
.offer(10);
Stream<Integer> stream = pushable.getStream();
stream.forEach(System.out::println);
//print 10
pushable.getInput()
.offer(20);
//print 20
pushable.getInput()
.close();
Pushing data into a FutureStream
PushableLazyFutureStream<Integer> pushable = StreamSource.ofUnbounded()
.futureStream(new LazyReact());
pushable.getInput()
.offer(100);
//on another thread
pushable.getStream()
.forEach(this:process);
//close input into Stream -
pushable.getInput().close();
Multiple Streams reading the same data across threads
MultipleStreamSource<Integer> multi = StreamSource.ofMultiple();
multi.getInput()
.offer(100);
//example on separate threads
//thread 1
LazyFutureStream<Integer> futureStream = multi.futureStream(new LazyReact());
futureStream.forEach(System.out::println);
//print 100
//thread 2
ReactiveSeq<Integer> seq = multi.reactiveSeq();
seq.forEach(System.out::println);
//print 100
//thread 3
Stream<Integer> stream = multi.stream();
stream.forEach(System.out::println);
//print 100
multi.getInput()
.offer(200);
//thread 1
//print 200
//thread 2
//print 200
//thread 3
//print 200
multi.getInput()
.close();
Modifier and Type | Method and Description |
---|---|
static <T> LazyFutureStream<T> |
futureStream(Adapter<T> adapter,
LazyReact react)
Create a LazyFutureStream.
|
<T> PushableLazyFutureStream<T> |
futureStream(LazyReact s)
Create a pushable LazyFutureStream using the supplied ReactPool
|
static StreamSource |
of(int backPressureAfter)
A builder for pushable Streams that apply backpressure if producing Streams exceed the capacity of consuming Streams.
|
static StreamSource |
of(QueueFactory<?> q)
Construct a Pushable StreamSource using the provided QueueFactory as a push mechanism
|
static <T> MultipleStreamSource<T> |
ofMultiple()
Connect multiple Streams to a Pushable datasource, each Stream will recieve the same
data.
|
static <T> MultipleStreamSource<T> |
ofMultiple(int backPressureAfter)
Connect multiple Streams to a Pushable datasource, each Stream will recieve the same
data.
|
static <T> MultipleStreamSource<T> |
ofMultiple(QueueFactory<?> q)
Construct a StreamSource that supports multiple readers of the same data backed by a Queue created
from the supplied QueueFactory
|
static StreamSource |
ofUnbounded()
Construct a Pushable StreamSource with no max size.
|
<T> PushableReactiveSeq<T> |
reactiveSeq()
Create a pushable
PushableReactiveSeq |
static <T> ReactiveSeq<T> |
reactiveSeq(Adapter<T> adapter)
Create a pushable
ReactiveSeq |
<T> PushableStream<T> |
stream()
Create a pushable JDK 8 Stream
|
static <T> java.util.stream.Stream<T> |
stream(Adapter<T> adapter)
Create a JDK 8 Stream from the supplied Adapter
|
public static <T> MultipleStreamSource<T> ofMultiple()
MultipleStreamSource<Integer> multi = StreamSource.ofMultiple();
multi.getInput()
.offer(100);
//example on separate threads
//thread 1
LazyFutureStream<Integer> futureStream = multi.futureStream(new LazyReact());
futureStream.forEach(System.out::println);
//print 100
//thread 2
ReactiveSeq<Integer> seq = multi.reactiveSeq();
seq.forEach(System.out::println);
//print 100
//thread 3
Stream<Integer> stream = multi.stream();
stream.forEach(System.out::println);
//print 100
multi.getInput()
.offer(200);
//thread 1
//print 200
//thread 2
//print 200
//thread 3
//print 200
multi.getInput()
.close();
public static <T> MultipleStreamSource<T> ofMultiple(int backPressureAfter)
MultipleStreamSource<Integer> multi = StreamSource.ofMultiple(2);
multi.getInput()
.offer(100);
//example on separate threads
//thread 1
LazyFutureStream<Integer> futureStream = multi.futureStream(new LazyReact());
futureStream.map(this::slowProcess)
.forEach(System.out::println);
//print 100
//thread 2
ReactiveSeq<Integer> seq = multi.reactiveSeq();
seq.forEach(System.out::println);
//print 100
//thread 3
Stream<Integer> stream = multi.stream();
stream.forEach(System.out::println);
//print 100
multi.getInput()
.offer(200);
//thread 1
//print 200
//thread 2
//print 200
//thread 3
//print 200
multi.getInput()
.offer(300);
multi.getInput()
.offer(400);
multi.getInput()
.offer(500); //blocked as backpressure applied
multi.getInput()
.close();
backPressureAfter
- Excess number of produced records over consumed (by all connected Streams
after which backPressure will be applied).public static <T> MultipleStreamSource<T> ofMultiple(QueueFactory<?> q)
q
- QueueFactory used to create the Adapter to back the pushable StreamSourcefor Factory creation options and various backpressure strategies
{@code
MultipleStreamSource multi = StreamSource
.ofMultiple(QueueFactories.boundedQueue(100));
LazyFutureStream pushable = multi.futureStream(new LazyReact());
ReactiveSeq seq = multi.reactiveSeq();
multi.getInput().offer(100);
multi.getInput().close();
pushable.collect(Collectors.toList()); //[100]
seq.collect(Collectors.toList()); //[100]
}
public static StreamSource of(QueueFactory<?> q)
q
- QueueFactory used to create the Adapter to back the pushable StreamSourcefor Factory creation options and various backpressure strategies
{@code
PushableStream pushable = StreamSource.of(QueueFactories.boundedQueue(10))
.stream();
pushable.getInput()
.offer(10);
Stream stream = pushable.getStream();
stream.forEach(System.out::println);
//print 10
pushable.getInput()
.offer(20);
//print 20
pushable.getInput()
.close();
}
public static StreamSource ofUnbounded()
PushableStream<Integer> pushable = StreamSource.ofUnbounded()
.stream();
pushable.getInput()
.offer(10);
Stream<Integer> stream = pushable.getStream();
stream.forEach(System.out::println);
//print 10
pushable.getInput()
.offer(20);
//print 20
pushable.getInput()
.close();
public static StreamSource of(int backPressureAfter)
StreamSource source = StreamSource.of(10);
pushable.getInput()
.offer(10);
//on a separate thread
source.reactiveSeq()
.forEach(System.out::println);
backPressureAfter
- Excess number of produced records over consumed (by all connected Streams
after which backPressure will be applied).public <T> PushableLazyFutureStream<T> futureStream(LazyReact s)
PushableLazyFutureStream<Integer> pushable = StreamSource.ofUnbounded()
.futureStream(new LazyReact());
pushable.getInput().add(100);
pushable.getInput().close();
assertThat(pushable.getStream().collect(Collectors.toList()),
hasItem(100));
s
- ReactPool to use to create the Streampublic static <T> LazyFutureStream<T> futureStream(Adapter<T> adapter, LazyReact react)
PushableLazyFutureStream<Integer> pushable = StreamSource.futureStream(QueueFactories.boundedNonBlockingQueue(1000),new LazyReact());
pushable.getInput().add(100);
pushable.getInput().close();
assertThat(pushable.getStream().collect(Collectors.toList()),
hasItem(100));
adapter
- Adapter to create a LazyFutureStream frompublic <T> PushableStream<T> stream()
PushableStream<Integer> pushable = StreamSource.ofUnbounded()
.stream();
pushable.getInput()
.add(10);
pushable.getInput()
.close();
pushable.getStream().collect(Collectors.toList()) //[10]
public <T> PushableReactiveSeq<T> reactiveSeq()
PushableReactiveSeq
PushableReactiveSeq<Integer> pushable = StreamSource.ofUnbounded()
.reactiveSeq();
pushable.getInput()
.add(10);
//on another thread
pushable.getStream()
.collect(Collectors.toList()) //[10]
ReactiveSeq
to push it to the Streampublic static <T> java.util.stream.Stream<T> stream(Adapter<T> adapter)
Queue<Integer> q = QueueFactories.boundedNonBlockingQueue(1000);
Stream<Integer> stream = StreamSource.stream(q);
stream.forEach(System.out::println);
//on a separate thread
q.offer(10);
adapter
- Adapter to create a Steam frompublic static <T> ReactiveSeq<T> reactiveSeq(Adapter<T> adapter)
ReactiveSeq
Signal<Integer> signal = Signal.queueBackedSignal();
ReactiveSeq<Integer> pushable = StreamSource.reactiveSeq(signal
.getDiscrete());
signal.set(100);
signal.close();
assertThat(pushable.collect(Collectors.toList()), hasItem(100));
adapter
- Adapter to create a Seq from