Package com.dehnes.kotlinvalkey.pubsub
Class ValkeyPubSubService
-
- All Implemented Interfaces:
public final class ValkeyPubSubServiceAn at-most-once messaging service that uses "virtual channels/topics" on top of a single Valkey channel.
This allows for grater scalability where many clients can share a single Valkey connection; and automatic re-connect is handled once.
Messages used must fit in the heap and cannot be streamed using InputStream, because a single received message is distributed/copied among all subscribers.
Message order is preserved per publisher due to the internal buffer - not across publishers.
Messages are not copied so be sure to not modify the ByteArray in message handlers.
-
-
Constructor Summary
Constructors Constructor Description ValkeyPubSubService(ValkeyConnectionPool connectionPool, String valkeyChannelName, Long delayBetweenReconnectMs, Integer heartbeatIntervalMs)
-
Method Summary
Modifier and Type Method Description final Unitstart(Duration timeout)Starts the service, optionally waiting for a successfully subscribe final Unitstop()Stops the service and waits for the background thread to finish final ValkeyPubSubServiceSubscriptionsubscribe(List<String> channels, Integer messageBufferLimit, Function2<String, ByteArray, Unit> onMsg)-
-
Constructor Detail
-
ValkeyPubSubService
ValkeyPubSubService(ValkeyConnectionPool connectionPool, String valkeyChannelName, Long delayBetweenReconnectMs, Integer heartbeatIntervalMs)
- Parameters:
connectionPool- the connection pool to be used for sending PUBLISH messagesvalkeyChannelName- the channel to be used on ValkeydelayBetweenReconnectMs- upon connection failure, this delay causes a wait before a reconnect is initiatedheartbeatIntervalMs- if nothing is received for this long, a PING is sent; if its reply is not received within another interval, the connection is considered dead and a reconnect is initiated
-
-
Method Detail
-
start
final Unit start(Duration timeout)
Starts the service, optionally waiting for a successfully subscribe
- Parameters:
timeout- max time to wait for the subscribe to succeed
-
subscribe
final ValkeyPubSubServiceSubscription subscribe(List<String> channels, Integer messageBufferLimit, Function2<String, ByteArray, Unit> onMsg)
- Parameters:
channels- channels to subscribe tomessageBufferLimit- arriving messages are buffered and then handled async consuming from this buffer.onMsg- code handling messages - should not throw exceptions.
-
-
-
-