Package com.dehnes.kotlinredis.pubsub
Class RedisPubSubService
-
- All Implemented Interfaces:
public final class RedisPubSubServiceAn at-most-once messaging service that uses "virtual channels/topics" on top of a single Redis channel.
This allows for grater scalability where many clients can share a single Redis 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 RedisPubSubService(RedisConnectionPool connectionPool, String redisChannelName, 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 RedisPubSubServiceSubscriptionsubscribe(List<String> channels, Integer messageBufferLimit, Function2<String, ByteArray, Unit> onMsg)-
-
Constructor Detail
-
RedisPubSubService
RedisPubSubService(RedisConnectionPool connectionPool, String redisChannelName, Long delayBetweenReconnectMs, Integer heartbeatIntervalMs)
- Parameters:
connectionPool- the connection pool to be used for sending PUBLISH messagesredisChannelName- the channel to be used on RedisdelayBetweenReconnectMs- 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 RedisPubSubServiceSubscription 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.
-
-
-
-