Class SimplePushEventSourceImpl<T,U extends BlockingQueue<PushEvent<? extends T>>>
- All Implemented Interfaces:
AutoCloseable,PushEventSource<T>,SimplePushEventSource<T>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final List<PushEventConsumer<? super T>> private final Objectprivate final Runnableprivate final intprivate final PromiseFactoryprivate final Uprivate final QueuePolicy<T, U> private final PromiseFactoryprivate final Semaphoreprivate boolean -
Constructor Summary
ConstructorsConstructorDescriptionSimplePushEventSourceImpl(PromiseFactory promiseFactory, QueuePolicy<T, U> queuePolicy, U queue, int parallelism, Runnable onClose) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close this source.private voidprivate voidcloseConsumer(PushEventConsumer<? super T> pec, PushEvent<T> event) This method can be used to delay event generation until an event source has connected.doCall(PushEvent<T> event, PushEventConsumer<? super T> pec) private voiddoSend(PushEventConsumer<? super T> pec, PushEvent<T> event) doSendWithBackPressure(PushEventConsumer<? super T> pec, PushEvent<T> event) voidClose this source for now, but potentially reopen it later.private voidenqueueEvent(PushEvent<T> event) voidClose this source for now, but potentially reopen it later.private voidhandleReset(boolean resetWait) booleanDetermine whether there are anyPushEventConsumers for thisPushEventSource.open(PushEventConsumer<? super T> pec) Open the asynchronous channel between the source and the consumer.voidAsynchronously publish an event to this stream and all connectedPushEventConsumerinstances.private longsafePush(PushEventConsumer<? super T> pec, PushEvent<T> event) private void
-
Field Details
-
lock
-
promiseFactory
-
sameThread
-
queuePolicy
-
queue
-
parallelism
private final int parallelism -
semaphore
-
connected
-
onClose
-
closed
private boolean closed -
connectPromise
-
waitForFinishes
private boolean waitForFinishes
-
-
Constructor Details
-
SimplePushEventSourceImpl
public SimplePushEventSourceImpl(PromiseFactory promiseFactory, QueuePolicy<T, U> queuePolicy, U queue, int parallelism, Runnable onClose)
-
-
Method Details
-
open
Description copied from interface:PushEventSourceOpen 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:
openin interfacePushEventSource<T>- Parameters:
pec- the consumer (not null)- Returns:
- a
AutoCloseablethat can be used to close the stream - Throws:
Exception
-
closeConsumer
-
doSend
-
doSendWithBackPressure
-
safePush
-
close
public void close()Description copied from interface:SimplePushEventSourceClose 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 anyPushEventConsumerthat tries toPushEventSource.open(PushEventConsumer)this source will immediately receive a close event, and will not see any remaining buffered events.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceSimplePushEventSource<T>
-
close
-
publish
Description copied from interface:SimplePushEventSourceAsynchronously publish an event to this stream and all connectedPushEventConsumerinstances. 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:
publishin interfaceSimplePushEventSource<T>- Parameters:
t-
-
endOfStream
public void endOfStream()Description copied from interface:SimplePushEventSourceClose 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
PushEventConsumerthat wishes mayPushEventSource.open(PushEventConsumer)this source, and will receive subsequent events.- Specified by:
endOfStreamin interfaceSimplePushEventSource<T>
-
error
Description copied from interface:SimplePushEventSourceClose 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
PushEventConsumerthat wishes mayPushEventSource.open(PushEventConsumer)this source, and will receive subsequent events.- Specified by:
errorin interfaceSimplePushEventSource<T>- Parameters:
t- the error
-
enqueueEvent
-
startWorker
private void startWorker() -
handleReset
private void handleReset(boolean resetWait) -
deliver
-
doCall
-
isConnected
public boolean isConnected()Description copied from interface:SimplePushEventSourceDetermine whether there are anyPushEventConsumers for thisPushEventSource. This can be used to skip expensive event creation logic when there are no listeners.- Specified by:
isConnectedin interfaceSimplePushEventSource<T>- Returns:
- true if any consumers are currently connected
-
connectPromise
Description copied from interface:SimplePushEventSourceThis method can be used to delay event generation until an event source has connected. The returned promise will resolve as soon as one or morePushEventConsumerinstances have opened the SimplePushEventSource.The returned promise may already be resolved if this
SimplePushEventSourcealready has connected consumers. If theSimplePushEventSourceis 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 thisSimplePushEventSourcemay no longer be connected to any consumers.- Specified by:
connectPromisein interfaceSimplePushEventSource<T>- Returns:
- A promise representing the connection state of this EventSource
-
closedConnectPromise
-