Class PartitionHub
- java.lang.Object
-
- akka.stream.javadsl.PartitionHub
-
public class PartitionHub extends java.lang.ObjectAPartitionHubis a special streaming hub that is able to route streamed elements to a dynamic set of consumers. It consists of two parts, aSinkand aSource. TheSinke elements from a producer to the actually live consumers it has. The selection of consumer is done with a function. Each element can be routed to only one consumer. Once the producer has been materialized, theSinkit feeds into returns a materialized value which is the correspondingSource. ThisSourcecan be materialized an arbitrary number of times, where each of the new materializations will receive their elements from the originalSink.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePartitionHub.ConsumerInfo
-
Constructor Summary
Constructors Constructor Description PartitionHub()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Sink<T,Source<T,NotUsed>>of(java.lang.Class<T> clazz, java.util.function.BiFunction<java.lang.Integer,T,java.lang.Integer> partitioner, int startAfterNrOfConsumers)Creates aSinkwith default buffer size 256 that receives elements from its upstream producer and routes them to a dynamic set of consumers.static <T> Sink<T,Source<T,NotUsed>>of(java.lang.Class<T> clazz, java.util.function.BiFunction<java.lang.Integer,T,java.lang.Integer> partitioner, int startAfterNrOfConsumers, int bufferSize)Creates aSinkthat receives elements from its upstream producer and routes them to a dynamic set of consumers.static <T> Sink<T,Source<T,NotUsed>>ofStateful(java.lang.Class<T> clazz, java.util.function.Supplier<java.util.function.ToLongBiFunction<PartitionHub.ConsumerInfo,T>> partitioner, int startAfterNrOfConsumers)Creates aSinkwith default buffer size 256 that receives elements from its upstream producer and routes them to a dynamic set of consumers.static <T> Sink<T,Source<T,NotUsed>>ofStateful(java.lang.Class<T> clazz, java.util.function.Supplier<java.util.function.ToLongBiFunction<PartitionHub.ConsumerInfo,T>> partitioner, int startAfterNrOfConsumers, int bufferSize)Creates aSinkthat receives elements from its upstream producer and routes them to a dynamic set of consumers.
-
-
-
Method Detail
-
ofStateful
public static <T> Sink<T,Source<T,NotUsed>> ofStateful(java.lang.Class<T> clazz, java.util.function.Supplier<java.util.function.ToLongBiFunction<PartitionHub.ConsumerInfo,T>> partitioner, int startAfterNrOfConsumers, int bufferSize)
Creates aSinkthat receives elements from its upstream producer and routes them to a dynamic set of consumers. After theSinkreturned by this method is materialized, it returns aSourceas materialized value. ThisSourcecan be materialized an arbitrary number of times and each materialization will receive the elements from the originalSink.Every new materialization of the
Sinkresults in a new, independent hub, which materializes to its ownSourcefor consuming theSinkof that materialization.If the original
Sinkis failed, then the failure is immediately propagated to all of its materializedSources (possibly jumping over already buffered elements). If the originalSinkis completed, then all correspondingSources are completed. Both failure and normal completion is "remembered" and later materializations of theSourcewill see the same (failure or completion) state.Sources that are cancelled are simply removed from the dynamic set of consumers.This
statefulSinkshould be used when there is a need to keep mutable state in the partition function, e.g. for implementing round-robin or sticky session kind of routing. If state is not needed theof(java.lang.Class<T>, java.util.function.BiFunction<java.lang.Integer, T, java.lang.Integer>, int, int)can be more convenient to use.- Parameters:
partitioner- Function that decides where to route an element. It is a factory of a function to to be able to hold stateful variables that are unique for each materialization. The function takes two parameters; the first is information about active consumers, including an array of consumer identifiers and the second is the stream element. The function should return the selected consumer identifier for the given element. The function will never be called when there are no active consumers, i.e. there is always at least one element in the array of identifiers.startAfterNrOfConsumers- Elements are buffered until this number of consumers have been connected. This is only used initially when the operator is starting up, i.e. it is not honored when consumers have been removed (canceled).bufferSize- Total number of elements that can be buffered. If this buffer is full, the producer is backpressured.
-
ofStateful
public static <T> Sink<T,Source<T,NotUsed>> ofStateful(java.lang.Class<T> clazz, java.util.function.Supplier<java.util.function.ToLongBiFunction<PartitionHub.ConsumerInfo,T>> partitioner, int startAfterNrOfConsumers)
Creates aSinkwith default buffer size 256 that receives elements from its upstream producer and routes them to a dynamic set of consumers. After theSinkreturned by this method is materialized, it returns aSourceas materialized value. ThisSourcecan be materialized an arbitrary number of times and each materialization will receive the elements from the originalSink.Every new materialization of the
Sinkresults in a new, independent hub, which materializes to its ownSourcefor consuming theSinkof that materialization.If the original
Sinkis failed, then the failure is immediately propagated to all of its materializedSources (possibly jumping over already buffered elements). If the originalSinkis completed, then all correspondingSources are completed. Both failure and normal completion is "remembered" and later materializations of theSourcewill see the same (failure or completion) state.Sources that are cancelled are simply removed from the dynamic set of consumers.This
statefulSinkshould be used when there is a need to keep mutable state in the partition function, e.g. for implementing round-robin or sticky session kind of routing. If state is not needed theof(java.lang.Class<T>, java.util.function.BiFunction<java.lang.Integer, T, java.lang.Integer>, int, int)can be more convenient to use.- Parameters:
partitioner- Function that decides where to route an element. It is a factory of a function to to be able to hold stateful variables that are unique for each materialization. The function takes two parameters; the first is information about active consumers, including an array of consumer identifiers and the second is the stream element. The function should return the selected consumer identifier for the given element. The function will never be called when there are no active consumers, i.e. there is always at least one element in the array of identifiers.startAfterNrOfConsumers- Elements are buffered until this number of consumers have been connected. This is only used initially when the operator is starting up, i.e. it is not honored when consumers have been removed (canceled).
-
of
public static <T> Sink<T,Source<T,NotUsed>> of(java.lang.Class<T> clazz, java.util.function.BiFunction<java.lang.Integer,T,java.lang.Integer> partitioner, int startAfterNrOfConsumers, int bufferSize)
Creates aSinkthat receives elements from its upstream producer and routes them to a dynamic set of consumers. After theSinkreturned by this method is materialized, it returns aSourceas materialized value. ThisSourcecan be materialized an arbitrary number of times and each materialization will receive the elements from the originalSink.Every new materialization of the
Sinkresults in a new, independent hub, which materializes to its ownSourcefor consuming theSinkof that materialization.If the original
Sinkis failed, then the failure is immediately propagated to all of its materializedSources (possibly jumping over already buffered elements). If the originalSinkis completed, then all correspondingSources are completed. Both failure and normal completion is "remembered" and later materializations of theSourcewill see the same (failure or completion) state.Sources that are cancelled are simply removed from the dynamic set of consumers.This
sinkshould be used when the routing function is stateless, e.g. based on a hashed value of the elements. Otherwise theofStateful(java.lang.Class<T>, java.util.function.Supplier<java.util.function.ToLongBiFunction<akka.stream.javadsl.PartitionHub.ConsumerInfo, T>>, int, int)can be used to implement more advanced routing logic.- Parameters:
partitioner- Function that decides where to route an element. The function takes two parameters; the first is the number of active consumers and the second is the stream element. The function should return the index of the selected consumer for the given element, i.e. int greater than or equal to 0 and less than number of consumers. E.g.(size, elem) -> Math.abs(elem.hashCode() % size). It's also possible to use-1to drop the element.startAfterNrOfConsumers- Elements are buffered until this number of consumers have been connected. This is only used initially when the operator is starting up, i.e. it is not honored when consumers have been removed (canceled).bufferSize- Total number of elements that can be buffered. If this buffer is full, the producer is backpressured.
-
of
public static <T> Sink<T,Source<T,NotUsed>> of(java.lang.Class<T> clazz, java.util.function.BiFunction<java.lang.Integer,T,java.lang.Integer> partitioner, int startAfterNrOfConsumers)
Creates aSinkwith default buffer size 256 that receives elements from its upstream producer and routes them to a dynamic set of consumers. After theSinkreturned by this method is materialized, it returns aSourceas materialized value. ThisSourcecan be materialized an arbitrary number of times and each materialization will receive the elements from the originalSink.Every new materialization of the
Sinkresults in a new, independent hub, which materializes to its ownSourcefor consuming theSinkof that materialization.If the original
Sinkis failed, then the failure is immediately propagated to all of its materializedSources (possibly jumping over already buffered elements). If the originalSinkis completed, then all correspondingSources are completed. Both failure and normal completion is "remembered" and later materializations of theSourcewill see the same (failure or completion) state.Sources that are cancelled are simply removed from the dynamic set of consumers.This
sinkshould be used when the routing function is stateless, e.g. based on a hashed value of the elements. Otherwise theofStateful(java.lang.Class<T>, java.util.function.Supplier<java.util.function.ToLongBiFunction<akka.stream.javadsl.PartitionHub.ConsumerInfo, T>>, int, int)can be used to implement more advanced routing logic.- Parameters:
partitioner- Function that decides where to route an element. The function takes two parameters; the first is the number of active consumers and the second is the stream element. The function should return the index of the selected consumer for the given element, i.e. int greater than or equal to 0 and less than number of consumers. E.g.(size, elem) -> Math.abs(elem.hashCode() % size). It's also possible to use-1to drop the element.startAfterNrOfConsumers- Elements are buffered until this number of consumers have been connected. This is only used initially when the operator is starting up, i.e. it is not honored when consumers have been removed (canceled).bufferSize- Total number of elements that can be buffered. If this buffer is full, the producer is backpressured.
-
-