Class SimplePushEventSourceImpl<T,U extends java.util.concurrent.BlockingQueue<PushEvent<? extends T>>>
- java.lang.Object
-
- org.osgi.util.pushstream.SimplePushEventSourceImpl<T,U>
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,PushEventSource<T>
,SimplePushEventSource<T>
class SimplePushEventSourceImpl<T,U extends java.util.concurrent.BlockingQueue<PushEvent<? extends T>>> extends java.lang.Object implements SimplePushEventSource<T>
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
closed
private java.util.List<PushEventConsumer<? super T>>
connected
private Deferred<java.lang.Void>
connectPromise
private java.lang.Object
lock
private java.lang.Runnable
onClose
private int
parallelism
private PromiseFactory
promiseFactory
private U
queue
private QueuePolicy<T,U>
queuePolicy
private PromiseFactory
sameThread
private java.util.concurrent.Semaphore
semaphore
private boolean
waitForFinishes
-
Constructor Summary
Constructors Constructor Description SimplePushEventSourceImpl(PromiseFactory promiseFactory, QueuePolicy<T,U> queuePolicy, U queue, int parallelism, java.lang.Runnable onClose)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close this source.private void
close(PushEvent<T> event)
private void
closeConsumer(PushEventConsumer<? super T> pec, PushEvent<T> event)
private Promise<java.lang.Void>
closedConnectPromise()
Promise<java.lang.Void>
connectPromise()
This method can be used to delay event generation until an event source has connected.private Promise<java.lang.Long>
deliver(java.util.List<PushEventConsumer<? super T>> toCall, PushEvent<T> event)
private Promise<java.lang.Long>
doCall(PushEvent<T> event, PushEventConsumer<? super T> pec)
private void
doSend(PushEventConsumer<? super T> pec, PushEvent<T> event)
private Promise<java.lang.Long>
doSendWithBackPressure(PushEventConsumer<? super T> pec, PushEvent<T> event)
void
endOfStream()
Close this source for now, but potentially reopen it later.private void
enqueueEvent(PushEvent<T> event)
void
error(java.lang.Throwable t)
Close this source for now, but potentially reopen it later.private void
handleReset(boolean resetWait)
boolean
isConnected()
Determine whether there are anyPushEventConsumer
s for thisPushEventSource
.java.lang.AutoCloseable
open(PushEventConsumer<? super T> pec)
Open the asynchronous channel between the source and the consumer.void
publish(T t)
Asynchronously publish an event to this stream and all connectedPushEventConsumer
instances.private long
safePush(PushEventConsumer<? super T> pec, PushEvent<T> event)
private void
startWorker()
-
-
-
Field Detail
-
lock
private final java.lang.Object lock
-
promiseFactory
private final PromiseFactory promiseFactory
-
sameThread
private final PromiseFactory sameThread
-
queuePolicy
private final QueuePolicy<T,U extends java.util.concurrent.BlockingQueue<PushEvent<? extends T>>> queuePolicy
-
parallelism
private final int parallelism
-
semaphore
private final java.util.concurrent.Semaphore semaphore
-
connected
private final java.util.List<PushEventConsumer<? super T>> connected
-
onClose
private final java.lang.Runnable onClose
-
closed
private boolean closed
-
connectPromise
private Deferred<java.lang.Void> connectPromise
-
waitForFinishes
private boolean waitForFinishes
-
-
Constructor Detail
-
SimplePushEventSourceImpl
public SimplePushEventSourceImpl(PromiseFactory promiseFactory, QueuePolicy<T,U> queuePolicy, U queue, int parallelism, java.lang.Runnable onClose)
-
-
Method Detail
-
open
public java.lang.AutoCloseable open(PushEventConsumer<? super T> pec) throws java.lang.Exception
Description copied from interface:PushEventSource
Open the asynchronous channel between the source and the consumer. The call returns anAutoCloseable
. 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.- Specified by:
open
in interfacePushEventSource<T>
- Parameters:
pec
- the consumer (not null)- Returns:
- a
AutoCloseable
that can be used to close the stream - Throws:
java.lang.Exception
-
closeConsumer
private void closeConsumer(PushEventConsumer<? super T> pec, PushEvent<T> event)
-
doSend
private void doSend(PushEventConsumer<? super T> pec, PushEvent<T> event)
-
doSendWithBackPressure
private Promise<java.lang.Long> doSendWithBackPressure(PushEventConsumer<? super T> pec, PushEvent<T> event)
-
safePush
private long safePush(PushEventConsumer<? super T> pec, PushEvent<T> event)
-
close
public void close()
Description copied from interface:SimplePushEventSource
Close this source. Calling this method indicates that there will never be any more events published by it. Calling this method sends a close event to all connected consumers. After calling this method anyPushEventConsumer
that tries toPushEventSource.open(PushEventConsumer)
this source will immediately receive a close event, and will not see any remaining buffered events.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceSimplePushEventSource<T>
-
publish
public void publish(T t)
Description copied from interface:SimplePushEventSource
Asynchronously publish an event to this stream and all connectedPushEventConsumer
instances. When this method returns there is no guarantee that all consumers have been notified. Events published by a single thread will maintain their relative ordering, however they may be interleaved with events from other threads.- Specified by:
publish
in interfaceSimplePushEventSource<T>
-
endOfStream
public void endOfStream()
Description copied from interface:SimplePushEventSource
Close this source for now, but potentially reopen it later. Calling this method asynchronously sends a close event to all connected consumers and then disconnects them. Any events previously queued by theSimplePushEventSource.publish(Object)
method will be delivered before this close event.After calling this method any
PushEventConsumer
that wishes mayPushEventSource.open(PushEventConsumer)
this source, and will receive subsequent events.- Specified by:
endOfStream
in interfaceSimplePushEventSource<T>
-
error
public void error(java.lang.Throwable t)
Description copied from interface:SimplePushEventSource
Close this source for now, but potentially reopen it later. Calling this method asynchronously sends an error event to all connected consumers and then disconnects them. Any events previously queued by theSimplePushEventSource.publish(Object)
method will be delivered before this error event.After calling this method any
PushEventConsumer
that wishes mayPushEventSource.open(PushEventConsumer)
this source, and will receive subsequent events.- Specified by:
error
in interfaceSimplePushEventSource<T>
- Parameters:
t
- the error
-
startWorker
private void startWorker()
-
handleReset
private void handleReset(boolean resetWait)
-
deliver
private Promise<java.lang.Long> deliver(java.util.List<PushEventConsumer<? super T>> toCall, PushEvent<T> event)
-
doCall
private Promise<java.lang.Long> doCall(PushEvent<T> event, PushEventConsumer<? super T> pec)
-
isConnected
public boolean isConnected()
Description copied from interface:SimplePushEventSource
Determine whether there are anyPushEventConsumer
s for thisPushEventSource
. This can be used to skip expensive event creation logic when there are no listeners.- Specified by:
isConnected
in interfaceSimplePushEventSource<T>
- Returns:
- true if any consumers are currently connected
-
connectPromise
public Promise<java.lang.Void> connectPromise()
Description copied from interface:SimplePushEventSource
This method can be used to delay event generation until an event source has connected. The returned promise will resolve as soon as one or morePushEventConsumer
instances have opened the SimplePushEventSource.The returned promise may already be resolved if this
SimplePushEventSource
already has connected consumers. If theSimplePushEventSource
is closed before the returned Promise resolves then it will be failed with anIllegalStateException
.Note that the connected consumers are able to asynchronously close their connections to this
SimplePushEventSource
, and therefore it is possible that once the promise resolves thisSimplePushEventSource
may no longer be connected to any consumers.- Specified by:
connectPromise
in interfaceSimplePushEventSource<T>
- Returns:
- A promise representing the connection state of this EventSource
-
closedConnectPromise
private Promise<java.lang.Void> closedConnectPromise()
-
-