I would like to get notifications to commands (eg. 'set') performed on certain keys in Redis. For that, I subscribe to Redis keyspace notifications as follows:
` public void subscribeToKeySpaceNotifications( JedisPubSub myJedisPubSub, String... patterns ) {
new Thread(() -> {
try (final Jedis jedis = pool.getResource()) {
jedis.psubscribe( myJedisPubSub, patterns );
} catch (Exception ex) {
LOGGER.error("Failed to subscribe to redis keyspace notifications with key pattern(s) [ {} ]", String.join(",", patterns), ex);
}
}).start();
}
`
where myJedisPubSub parameter is the following:
` public class RedisKeySetListener extends JedisPubSub {
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
logger.info("onPSubscribe: pattern/{}, subscribedChannels/{}", pattern, subscribedChannels);
}
@Override
public void onPMessage(String pattern, String channel, String message) {
logger.info("onPMessage: pattern/{}, channel/{}, message/{}", pattern, channel, message);
//processMessage(message);
}
}`
I run Redis in a Kubernetes pod. After I restart Redis pod, Redis does not maintain the subscription, and I no longer get keyspace notifications.
Do I have to handle re-subscription myself, or can I make Redis "remember" the subscription and re-apply it after restart?
Comment From: itamarhaber
Hello @tsiyona
Redis doesn't "remember" the connected subscribers. Every time there is a disconnect, regardless of the underlying reason (i.e. server restart, network failure, client crash...), the client needs to resubscribe to then channels of its interest once the new connection is established.
Comment From: tsiyona
Thank you!