public final class PubSubRef<T>
extends Object
implements akka.actor.NoSerializationVerificationNeeded
PubSubRef
represents a publish/subscribe topic.
Messages can be published to the topic via a stream by using the Source
returned by the publisher()
method. A single message can be published
with the publish(T)
method.
Messages can be consumed from the topic via a stream by using the Sink
returned by the subscriber()
method.
The registry of subscribers is eventually consistent, i.e. new subscribers are not immediately visible at other nodes, but typically the information will be fully replicated to all other nodes after a few seconds.
New subscribers will not see old messages that were published to the topic, i.e. it is only a live stream of messages.
Messages are not guaranteed to be delivered, i.e. they may be lost. That can for example happen if there is a transient network partition.
The subscriber has a buffer of received messages, but messages will be dropped if that buffer is full and demand for more elements have not been requested from downstream. This can happen if a subscriber is slower than the publishers of the messages. When the buffer is full the oldest messages are dropped to make room for new messages.
Constructor and Description |
---|
PubSubRef(TopicId<T> topic,
akka.actor.ActorRef mediator,
akka.actor.ActorSystem system,
int bufferSize) |
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletionStage<Boolean> |
hasAnySubscribers()
Request if this topic has any known subscribers at this point.
|
void |
publish(T message) |
akka.stream.javadsl.Sink<T,akka.NotUsed> |
publisher()
Publish messages from a stream to the topic.
|
akka.stream.javadsl.Source<T,akka.NotUsed> |
subscriber()
Consume messages from the topic via a stream.
|
TopicId<T> |
topic() |
String |
toString() |
public void publish(T message)
public akka.stream.javadsl.Sink<T,akka.NotUsed> publisher()
Source
that produces the messages to
this Sink
and then run
the stream.public akka.stream.javadsl.Source<T,akka.NotUsed> subscriber()
Source
as a response in a ServiceCall
and the elements will be streamed to the client.
Otherwise you have to connect a Sink
that consumes the messages from
this Source
and then run
the stream.public java.util.concurrent.CompletionStage<Boolean> hasAnySubscribers()
CompletionStage
is completed with the currently known information
at this node, i.e. completion is not deferred until there are subscribers.
Note that the registry of subscribers is eventually consistent, i.e. new subscribers are not immediately visible at other nodes, but typically the information will be fully replicated to all other nodes after a few seconds.
This method is especially useful when writing tests that require that a subscriber is known before sending messages to a topic.
public String toString()
toString
in class Object