Class SimplePushEventSourceImpl<T,U extends BlockingQueue<PushEvent<? extends T>>>
- All Implemented Interfaces:
AutoCloseable
,PushEventSource<T>
,SimplePushEventSource<T>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final List<PushEventConsumer<? super T>>
private final Object
private final Runnable
private final int
private final PromiseFactory
private final U
private final QueuePolicy<T,
U> private final PromiseFactory
private final Semaphore
private boolean
-
Constructor Summary
ConstructorsConstructorDescriptionSimplePushEventSourceImpl
(PromiseFactory promiseFactory, QueuePolicy<T, U> queuePolicy, U queue, int parallelism, Runnable onClose) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close this source.private void
private void
closeConsumer
(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 void
doSend
(PushEventConsumer<? super T> pec, PushEvent<T> event) doSendWithBackPressure
(PushEventConsumer<? super T> pec, PushEvent<T> event) void
Close this source for now, but potentially reopen it later.private void
enqueueEvent
(PushEvent<T> event) void
Close this source for now, but potentially reopen it later.private void
handleReset
(boolean resetWait) boolean
Determine whether there are anyPushEventConsumer
s for thisPushEventSource
.open
(PushEventConsumer<? super T> pec) Open the asynchronous channel between the source and the consumer.void
Asynchronously publish an event to this stream and all connectedPushEventConsumer
instances.private long
safePush
(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: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:
Exception
-
closeConsumer
-
doSend
-
doSendWithBackPressure
-
safePush
-
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 interfaceAutoCloseable
- Specified by:
close
in interfaceSimplePushEventSource<T>
-
close
-
publish
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
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
-
enqueueEvent
-
startWorker
private void startWorker() -
handleReset
private void handleReset(boolean resetWait) -
deliver
-
doCall
-
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
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
-