Top Description Inners Fields Constructors Methods
org.osgi.util.pushstream

public final Class PushStreamProvider

extends Object
Class Inheritance
Static Imports
java.util.concurrent.TimeUnit.MILLISECONDS, org.osgi.util.pushstream.AbstractPushStreamImpl.State.CLOSED, .PushEvent.data, .PushEvent.error, .PushbackPolicyOption.LINEAR, .QueuePolicyOption.FAIL

A factory for PushStream instances, and utility methods for handling PushEventSources and PushEventConsumers

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final Lock
private int
private ScheduledExecutorService

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private ScheduledExecutorService
public <T, U extends BlockingQueue<PushEvent<? extends T>>> BufferBuilder<PushEventConsumer<T>, T, U>

Returns:

a PushEventConsumer with a buffer directly before it
buildBufferedConsumer
(PushEventConsumer<T> delegate)

Build a buffered PushEventConsumer with custom configuration.

public <T, U extends BlockingQueue<PushEvent<? extends T>>> BufferBuilder<PushEventSource<T>, T, U>

Returns:

a PushEventSource backed by the PushStream
buildEventSourceFromStream
(PushStream<T> stream)

Convert an PushStream into an PushEventSource.

public <T, U extends BlockingQueue<PushEvent<? extends T>>> BufferBuilder<SimplePushEventSource<T>, T, U>
buildSimpleEventSource(Class<T> type)

Build a SimplePushEventSource with the supplied type and custom buffering behaviors.

public <T, U extends BlockingQueue<PushEvent<? extends T>>> PushStreamBuilder<T, U>

Returns:

A PushStreamBuilder for the stream
buildStream
(PushEventSource<T>
The source of the events
eventSource
)

Builds a push stream with custom configuration.

private <T> PushStream<T>
cleanupThreads(boolean closeExecutorOnClose, Executor workerToUse, boolean releaseSchedulerOnClose, PushStream<T> stream)

public <T> PushEventConsumer<T>

Returns:

a PushEventConsumer with a buffer directly before it
createBufferedConsumer
(PushEventConsumer<T> delegate)

Create a buffered PushEventConsumer with the default configured buffer, executor size, queue, queue policy and pushback policy.

public <T> PushEventSource<T>

Returns:

a PushEventSource backed by the PushStream
createEventSourceFromStream
(PushStream<T> stream)

Convert an PushStream into an PushEventSource.

public <T> SimplePushEventSource<T>
createSimpleEventSource(Class<T> type)

Create a SimplePushEventSource with the supplied type and default buffering behaviors.

pack-priv <T, U extends BlockingQueue<PushEvent<? extends T>>> SimplePushEventSource<T>
createSimplePushEventSource(int parallelism, Executor executor, U queue, QueuePolicy<T, U> queuePolicy, Runnable onClose)

public <T> PushStream<T>

Returns:

A PushStream with a default initial buffer
createStream
(PushEventSource<T> eventSource)

Create a stream with the default configured buffer, executor size, queue, queue policy and pushback policy.

pack-priv <T, U extends BlockingQueue<PushEvent<? extends T>>> PushStream<T>
createStream(PushEventSource<T> eventSource, int parallelism, Executor executor, ScheduledExecutorService scheduler, U queue, QueuePolicy<T, U> queuePolicy, PushbackPolicy<T, U> pushbackPolicy)

pack-priv <T> PushStream<T>
private void
public <T> PushStream<T>

Returns:

A PushStream containing the items from the Java Stream
streamOf
(Stream<T>
The items to push into the PushStream
items
)

Create an Unbuffered PushStream from a Java Stream The data from the stream will be pushed into the PushStream synchronously as it is opened.

public <T> PushStream<T>

Returns:

A PushStream containing the items from the Java Stream
streamOf
(Executor
The worker to use to push items from the Stream into the PushStream
executor
,
ScheduledExecutorService
The scheduler to use to trigger timed events in the PushStream
scheduler
,
Stream<T>
The items to push into the PushStream
items
)

Create an Unbuffered PushStream from a Java Stream The data from the stream will be pushed into the PushStream asynchronously using the supplied Executor.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lockback to summary
private final Lock lock
schedulerReferencesback to summary
private int schedulerReferences
sharedSchedulerback to summary
private ScheduledExecutorService sharedScheduler

Constructor Detail

PushStreamProviderback to summary
public PushStreamProvider()

Method Detail

acquireSchedulerback to summary
private ScheduledExecutorService acquireScheduler()
buildBufferedConsumerback to summary
public <T, U extends BlockingQueue<PushEvent<? extends T>>> BufferBuilder<PushEventConsumer<T>, T, U> buildBufferedConsumer(PushEventConsumer<T> delegate)

Build a buffered PushEventConsumer with custom configuration.

The returned consumer will be buffered from the event source, and will honor back pressure requests from its delegate even if the event source does not.

Buffered consumers are useful for "bursty" event sources which produce a number of events close together, then none for some time. These bursts can sometimes overwhelm the consumer. Buffering will not, however, protect downstream components from a source which produces events faster than they can be consumed.

Buffers are also useful as "circuit breakers". If a QueuePolicyOption#FAIL is used then a full buffer will request that the stream close, preventing an event storm from reaching the client.

Note that this buffered consumer will close when it receives a terminal event, or if the delegate returns negative backpressure. No further events will be propagated after this time.

Returns:BufferBuilder<PushEventConsumer<T>, T, U>

a PushEventConsumer with a buffer directly before it

buildEventSourceFromStreamback to summary
public <T, U extends BlockingQueue<PushEvent<? extends T>>> BufferBuilder<PushEventSource<T>, T, U> buildEventSourceFromStream(PushStream<T> stream)

Convert an PushStream into an PushEventSource. The first call to PushEventSource#open(PushEventConsumer) will begin event processing.

The PushEventSource will remain active until the backing stream is closed, and permits multiple consumers to PushEventSource#open(PushEventConsumer) it. Note that this means the caller of this method is responsible for closing the supplied stream if it is not finite in length.

Late joining consumers will not receive historical events, but will immediately receive the terminal event which closed the stream if the stream is already closed.

Returns:BufferBuilder<PushEventSource<T>, T, U>

a PushEventSource backed by the PushStream

buildSimpleEventSourceback to summary
public <T, U extends BlockingQueue<PushEvent<? extends T>>> BufferBuilder<SimplePushEventSource<T>, T, U> buildSimpleEventSource(Class<T> type)

Build a SimplePushEventSource with the supplied type and custom buffering behaviors. The SimplePushEventSource will respond to back pressure requests from the consumers connected to it.

Returns:BufferBuilder<SimplePushEventSource<T>, T, U>

a SimplePushEventSource

buildStreamback to summary
public <T, U extends BlockingQueue<PushEvent<? extends T>>> PushStreamBuilder<T, U> buildStream(PushEventSource<T> eventSource)

Builds a push stream with custom configuration.

The resulting PushStream may be buffered or unbuffered depending on how it is configured.

Parameters
eventSource:PushEventSource<T>

The source of the events

Returns:PushStreamBuilder<T, U>

A PushStreamBuilder for the stream

cleanupThreadsback to summary
private <T> PushStream<T> cleanupThreads(boolean closeExecutorOnClose, Executor workerToUse, boolean releaseSchedulerOnClose, PushStream<T> stream)
createBufferedConsumerback to summary
public <T> PushEventConsumer<T> createBufferedConsumer(PushEventConsumer<T> delegate)

Create a buffered PushEventConsumer with the default configured buffer, executor size, queue, queue policy and pushback policy. This is equivalent to calling

buildBufferedConsumer(delegate).create();

The returned consumer will be buffered from the event source, and will honor back pressure requests from its delegate even if the event source does not.

Buffered consumers are useful for "bursty" event sources which produce a number of events close together, then none for some time. These bursts can sometimes overwhelm the consumer. Buffering will not, however, protect downstream components from a source which produces events faster than they can be consumed.

Returns:PushEventConsumer<T>

a PushEventConsumer with a buffer directly before it

createEventSourceFromStreamback to summary
public <T> PushEventSource<T> createEventSourceFromStream(PushStream<T> stream)

Convert an PushStream into an PushEventSource. The first call to PushEventSource#open(PushEventConsumer) will begin event processing. The PushEventSource will remain active until the backing stream is closed, and permits multiple consumers to PushEventSource#open(PushEventConsumer) it. This is equivalent to:

buildEventSourceFromStream(stream).create();
Returns:PushEventSource<T>

a PushEventSource backed by the PushStream

createSimpleEventSourceback to summary
public <T> SimplePushEventSource<T> createSimpleEventSource(Class<T> type)

Create a SimplePushEventSource with the supplied type and default buffering behaviors. The SimplePushEventSource will respond to back pressure requests from the consumers connected to it. This is equivalent to:

buildSimpleEventSource(type).create();
Returns:SimplePushEventSource<T>

a SimplePushEventSource

createSimplePushEventSourceback to summary
pack-priv <T, U extends BlockingQueue<PushEvent<? extends T>>> SimplePushEventSource<T> createSimplePushEventSource(int parallelism, Executor executor, U queue, QueuePolicy<T, U> queuePolicy, Runnable onClose)
Annotations
@SuppressWarnings:unchecked, rawtypes
createStreamback to summary
public <T> PushStream<T> createStream(PushEventSource<T> eventSource)

Create a stream with the default configured buffer, executor size, queue, queue policy and pushback policy. This is equivalent to calling

buildStream(source).create();

This stream will be buffered from the event producer, and will honor back pressure even if the source does not.

Buffered streams are useful for "bursty" event sources which produce a number of events close together, then none for some time. These bursts can sometimes overwhelm downstream processors. Buffering will not, however, protect downstream components from a source which produces events faster (on average) than they can be consumed.

Event delivery will not begin until a terminal operation is reached on the chain of PushStreams. Once a terminal operation is reached the stream will be connected to the event source.

Returns:PushStream<T>

A PushStream with a default initial buffer

createStreamback to summary
pack-priv <T, U extends BlockingQueue<PushEvent<? extends T>>> PushStream<T> createStream(PushEventSource<T> eventSource, int parallelism, Executor executor, ScheduledExecutorService scheduler, U queue, QueuePolicy<T, U> queuePolicy, PushbackPolicy<T, U> pushbackPolicy)
Annotations
@SuppressWarnings:rawtypes, unchecked
createUnbufferedStreamback to summary
pack-priv <T> PushStream<T> createUnbufferedStream(PushEventSource<T> eventSource, Executor executor, ScheduledExecutorService scheduler)
releaseSchedulerback to summary
private void releaseScheduler()
streamOfback to summary
public <T> PushStream<T> streamOf(Stream<T> items)

Create an Unbuffered PushStream from a Java Stream The data from the stream will be pushed into the PushStream synchronously as it is opened. This may make terminal operations blocking unless a buffer has been added to the PushStream. Care should be taken with infinite Streams to avoid blocking indefinitely.

Parameters
items:Stream<T>

The items to push into the PushStream

Returns:PushStream<T>

A PushStream containing the items from the Java Stream

streamOfback to summary
public <T> PushStream<T> streamOf(Executor executor, ScheduledExecutorService scheduler, Stream<T> items)

Create an Unbuffered PushStream from a Java Stream The data from the stream will be pushed into the PushStream asynchronously using the supplied Executor.

Parameters
executor:Executor

The worker to use to push items from the Stream into the PushStream

scheduler:ScheduledExecutorService

The scheduler to use to trigger timed events in the PushStream

items:Stream<T>

The items to push into the PushStream

Returns:PushStream<T>

A PushStream containing the items from the Java Stream

org.osgi.util.pushstream back to summary

private Class PushStreamProvider.MultiplexingConsumer<T>

extends Object
implements PushEventConsumer<T>
Class Inheritance
  • java.lang.Object
  • org.osgi.util.pushstream.PushStreamProvider.MultiplexingConsumer
All Implemented Interfaces
org.osgi.util.pushstream.PushEventConsumer

Field Summary

Modifier and TypeField and Description
private final CopyOnWriteArrayList<PushEventConsumer<? super T>>
private final AtomicReference<PushEvent<T>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public long
accept(PushEvent<? extends T>
The event
event
)

Implements org.osgi.util.pushstream.PushEventConsumer.accept.

Accept an event from a source.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

consumersback to summary
private final CopyOnWriteArrayList<PushEventConsumer<? super T>> consumers
terminalEventStoreback to summary
private final AtomicReference<PushEvent<T>> terminalEventStore

Constructor Detail

MultiplexingConsumerback to summary
public MultiplexingConsumer(AtomicReference<PushEvent<T>> terminalEventStore, CopyOnWriteArrayList<PushEventConsumer<? super T>> consumers)

Method Detail

acceptback to summary
public long accept(PushEvent<? extends T> event) throws Exception

Implements org.osgi.util.pushstream.PushEventConsumer.accept.

Doc from org.osgi.util.pushstream.PushEventConsumer.accept.

Accept an event from a source. Events can be delivered on multiple threads simultaneously. However, Close and Error events are the last events received, no more events must be sent after them.

Parameters
event:PushEvent<? extends T>

The event

Returns:long

less than 0 means abort, 0 means continue, more than 0 means delay ms

Annotations
@Override
Exceptions
Exception:
to indicate that an error has occurred and that no further events should be delivered to this PushEventConsumer
org.osgi.util.pushstream back to summary

pack-priv final Class PushStreamProvider.PushEventPipe<T>

extends Object
implements PushEventConsumer<T>, PushEventSource<T>
Class Inheritance
All Implemented Interfaces
org.osgi.util.pushstream.PushEventSource, org.osgi.util.pushstream.PushEventConsumer

Field Summary

Modifier and TypeField and Description
pack-priv volatile PushEventConsumer<? super T>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public long
accept(PushEvent<? extends T>
The event
event
)

Implements org.osgi.util.pushstream.PushEventConsumer.accept.

Accept an event from a source.

public AutoCloseable
open(PushEventConsumer<? super T>
the consumer (not null)
pec
)

Implements org.osgi.util.pushstream.PushEventSource.open.

Open the asynchronous channel between the source and the consumer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateback to summary
pack-priv volatile PushEventConsumer<? super T> delegate

Constructor Detail

PushEventPipeback to summary
pack-priv PushEventPipe()

Method Detail

acceptback to summary
public long accept(PushEvent<? extends T> event) throws Exception

Implements org.osgi.util.pushstream.PushEventConsumer.accept.

Doc from org.osgi.util.pushstream.PushEventConsumer.accept.

Accept an event from a source. Events can be delivered on multiple threads simultaneously. However, Close and Error events are the last events received, no more events must be sent after them.

Parameters
event:PushEvent<? extends T>

The event

Returns:long

less than 0 means abort, 0 means continue, more than 0 means delay ms

Annotations
@Override
Exceptions
Exception:
to indicate that an error has occurred and that no further events should be delivered to this PushEventConsumer
openback to summary
public AutoCloseable open(PushEventConsumer<? super T> pec) throws Exception

Implements org.osgi.util.pushstream.PushEventSource.open.

Doc from org.osgi.util.pushstream.PushEventSource.open.

Open the asynchronous channel between the source and the consumer. The call returns an AutoCloseable. This can be closed, and should close the channel, including sending a Close event if the channel was not already closed. The returned object must be able to be closed multiple times without sending more than one Close events.

Parameters
pec:PushEventConsumer<? super T>

the consumer (not null)

Returns:AutoCloseable

a AutoCloseable that can be used to close the stream

Annotations
@Override