K
- Key typeV
- Value type transferred via managed Adapterspublic class Pipes<K,V>
extends java.lang.Object
Adapter
Constructor and Description |
---|
Pipes() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear all managed Adapters (without closing them or performing any other operation on them)
|
void |
close(java.lang.String key)
Close the Adapter identified by the provided Key if it exists
|
Maybe<LazyFutureStream<V>> |
futureStream(K key)
Create a FutureStream using default Parallelism from the Adapter
identified by the provided key
|
Maybe<LazyFutureStream<V>> |
futureStream(K key,
LazyReact builder)
Create a FutureStream using the provided LazyReact futureStream builder
from the Adapter identified by the provided Key
|
Maybe<Adapter<V>> |
get(K key)
Get the Adapter identified by the specified key
|
Eval<V> |
nextOrNull(K key)
Return an Eval that allows retrieval of the next value from the attached pipe when get() is called
A value is returned if a value is present, otherwise null is returned if the publisher is complete or an error occurs
|
Eval<Maybe<V>> |
nextValue(K key)
Return an Eval that allows retrieval of the next value from the attached pipe when get() is called,
can be used as an Iterator over the future & present values in the Adapter
Maybe.some is returned if a value is present, Maybe.none is returned if the publisher is complete or an error occurs
|
static <K,V> Pipes<K,V> |
of() |
static <K,V> Pipes<K,V> |
of(java.util.Map<K,Adapter<V>> registered)
Construct a Pipes instance to manage a predefined Map of Adapaters
|
Xor<java.lang.Throwable,V> |
oneOrError(K key)
Extact one value from the selected pipe or an error if it doesn't exist (NoSuchElementException).
|
FutureW<V> |
oneOrErrorAsync(K key,
java.util.concurrent.Executor ex)
Asynchronously extract a value from the Adapter identified by the provided Key
|
Maybe<V> |
oneValue(K key)
Extract one value from the selected pipe, if it exists
|
Maybe<Try<V,java.lang.Throwable>> |
oneValueOrError(K key)
Deprecated.
|
<X extends java.lang.Throwable> |
oneValueOrError(K key,
java.lang.Class<X>... classes)
Deprecated.
|
void |
publishTo(K key,
org.reactivestreams.Publisher<V> publisher)
Synchronously publish data to the Adapter specified by the provided Key, blocking the current thread
|
void |
publishToAsync(K key,
org.reactivestreams.Publisher<V> publisher)
Asynchronously publish data to the Adapter specified by the provided Key
|
void |
push(K key,
V value)
Push a single value synchronously into the Adapter identified by the supplied Key,
if it exists
|
Maybe<ReactiveSeq<V>> |
reactiveSeq(K key)
Create a ReactiveSeq from the Adapter identified by the provided Key
|
void |
register(K key,
Adapter<V> adapter)
Register a Queue, and get back a listening LazyFutureStream that runs on a single thread
(not the calling thread)
|
PMapX<K,Adapter<V>> |
registered() |
int |
size() |
void |
subscribeTo(K key,
org.reactivestreams.Subscriber<V> subscriber)
Subscribe synchronously to a pipe
|
void |
subscribeTo(K key,
org.reactivestreams.Subscriber<V> subscriber,
java.util.concurrent.Executor subscribeOn)
Subscribe asynchronously to a pipe
|
ListX<V> |
xValues(K key,
long x)
Extract the next x values from the Adapter identified by the provided Key
If the Adapter doesn't exist an empty List is returned
|
public int size()
public PMapX<K,Adapter<V>> registered()
public static <K,V> Pipes<K,V> of()
public static <K,V> Pipes<K,V> of(java.util.Map<K,Adapter<V>> registered)
registered
- Adapters to registerpublic void push(K key, V value)
Pipes<String,String> pipes = Pipes.of();
pipes.register("hello", new Queue<String>());
pipes.push("hello", "world");
//on another thread
pipes.reactiveSeq("hello")
.get()
.forEach(System.out::println);
key
- Adapter keyvalue
- Value to push to Adapterpublic Maybe<Adapter<V>> get(K key)
//close an adapter
pipes.get("adapter-key")
.map(a->a.close())
.orElse(false); //Maybe is lazy - trigger action
key
- : Adapter identifierpublic Maybe<LazyFutureStream<V>> futureStream(K key)
key
- : Adapter identifier
{@code
Pipes bus = Pipes.of();
bus.register("reactor", QueueFactories.boundedNonBlockingQueue(1000)
.build());
bus.publishTo("reactor",ReactiveSeq.of(10,20,30));
bus.close("reactor");
//on another thread
List res = bus.futureStream("reactor")
.get()
.map(i->"fan-out to handle blocking I/O:" + Thread.currentThread().getId() + ":"+i)
.toList();
System.out.println(res);
assertThat(res.size(),equalTo(3));
}
public Maybe<LazyFutureStream<V>> futureStream(K key, LazyReact builder)
Pipes<String, Integer> bus = Pipes.of();
bus.register("reactor", QueueFactories.<Integer>boundedNonBlockingQueue(1000)
.build());
bus.publishTo("reactor",ReactiveSeq.of(10,20,30));
bus.close("reactor");
//on another thread
List<String> res = bus.futureStream("reactor", new LazyReact(10,10))
.get()
.map(i->"fan-out to handle blocking I/O:" + Thread.currentThread().getId() + ":"+i)
.toList();
System.out.println(res);
assertThat(res.size(),equalTo(3));
key
- : Adapter identifierbuilder
- LazyReact futureStream builderpublic Maybe<ReactiveSeq<V>> reactiveSeq(K key)
Queue<String> q = new Queue<>();
pipes.register("data-queue", q);
pipes.push("data-queue", "world");
//on a separate thread
ReactiveSeq<String> stream = pipes.reactiveSeq("data-queue");
stream.forEach(System.out::println);
//"world"
key
- : Adapter identifierReactiveSeq
from selected Queuepublic ListX<V> xValues(K key, long x)
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
pipes.push("hello", "world3");
pipes.push("hello", "world4");
//on a separate thread
pipes.xValues("hello",2) //ListX.of("world","world2")
pipes.xValues("hello",2) //ListX.of("world3","world4")
key
- : Adapter identifierx
- Number of elements to returnpublic Maybe<V> oneValue(K key)
key
- : Adapter identifierpublic Xor<java.lang.Throwable,V> oneOrError(K key)
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
pipes.oneOrError("hello")
.get() //"world"
key
- : Adapter identifier@Deprecated public <X extends java.lang.Throwable> Maybe<Try<V,X>> oneValueOrError(K key, java.lang.Class<X>... classes)
oneValue(Object)
or @see oneOrError(Object)
is better at the moment.
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
pipes.oneValueOrError("hello",Throwable.class).get(); //Try["world"]
key
- classes
- @Deprecated public Maybe<Try<V,java.lang.Throwable>> oneValueOrError(K key)
oneValue(Object)
or @see oneOrError(Object)
is better at the moment.
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
pipes.oneValueOrError("hello").get(); //Try["world"]
key
- : Adapter identifierpublic FutureW<V> oneOrErrorAsync(K key, java.util.concurrent.Executor ex)
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
pipes.oneOrErrorAsync("hello", ex) // FutureW.ofResult("world")
key
- : Adapter identifierex
- Executor to extract value from Adapter from onpublic Eval<Maybe<V>> nextValue(K key)
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
q.close();
Eval<Maybe<String>> nextValue = pipes.nextValue("hello");
int values = 0;
while(nextValue.get().isPresent()){
System.out.println(values++);
}
assertThat(values,equalTo(2));
key
- : Adapter identifierpublic Eval<V> nextOrNull(K key)
Queue<String> q = new Queue<>();
pipes.register("hello", q);
pipes.push("hello", "world");
pipes.push("hello", "world2");
q.close();
Eval<String> nextValue = pipes.nextOrNull("hello");
int values = 0;
while(nextValue.get()!=null){
System.out.println(values++);
}
assertThat(values,equalTo(2));
key
- : Adapter identifierpublic void register(K key, Adapter<V> adapter)
Pipes.register("test", QueueFactories.
<String>boundedNonBlockingQueue(100)
.build());
LazyFutureStream<String> stream = PipesToLazyStreams.cpuBoundStream("test");
stream.filter(it->it!=null).peek(System.out::println).run();
key
- : Adapter identifieradapter
- public void clear()
public void subscribeTo(K key, org.reactivestreams.Subscriber<V> subscriber)
key
- for registered simple-react async.Adaptersubscriber
- Reactive Streams subscriber for data on this pipepublic void subscribeTo(K key, org.reactivestreams.Subscriber<V> subscriber, java.util.concurrent.Executor subscribeOn)
SeqSubscriber<String> subscriber = SeqSubscriber.subscriber();
Queue<String> queue = new Queue();
pipes.register("hello", queue);
pipes.subscribeTo("hello",subscriber,ForkJoinPool.commonPool());
queue.offer("world");
queue.close();
assertThat(subscriber.stream().findAny().get(),equalTo("world"));
key
- for registered simple-react async.Adaptersubscriber
- Reactive Streams subscriber for data on this pipepublic void publishTo(K key, org.reactivestreams.Publisher<V> publisher)
key
- for registered cylops-react async.Adapterpublisher
- Reactive Streams publisher to push data onto this pipepublic void publishToAsync(K key, org.reactivestreams.Publisher<V> publisher)
Pipes<String,Integer> pipes = Pipes.of();
Queue<Integer> queue = new Queue();
pipes.register("hello", queue);
pipes.publishToAsync("hello",ReactiveSeq.of(1,2,3));
Thread.sleep(100);
queue.offer(4);
queue.close();
assertThat(queue.stream().toList(),equalTo(Arrays.asList(1,2,3,4)));
key
- for registered simple-react async.Adapterpublisher
- Reactive Streams publisher to push data onto this pipepublic void close(java.lang.String key)
key
- : Adapter identifier