Get the event name in redis using java

718 Views Asked by At

If I listen event in redis, just like command below,

$ redis-cli --csv psubscribe '__key*__:*'
Reading messages... (press Ctrl-C to quit)

I can get things like

"pmessage","__key*__:*","__keyspace@0__:exipre","xxx"
"pmessage","__key*__:*","__keyevent@0__:expired","xxx"

So, when I use spring-data-redis to listen events in redis, how can I get the specific event name(like keyspace@0:expire), so that not only get the key value like "xxx", but also get the event like expire or expired, as I don't want to build two topic listener respectively like below:

RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//one for expire
container.addMessageListener(listenerAdapter1, new PatternTopic(//topic 1));        
//another for expired
container.addMessageListener(listenerAdapter2, new PatternTopic(//topic 2));
1

There are 1 best solutions below

0
On

You might want to have a look at KeyspaceEventMessageListener that subscribes to __keyevent@* and then just implement doHandleMessage. Message.getChannel() holds the raw information about the event type like __keyevent@0__:expired.

KeyspaceEventMessageListener eventListener = new KeyspaceEventMessageListener(container) {

    @Override
    protected void doHandleMessage(Message message) {

        String channelInfo = new String(message.getChannel()); // __keyevent@0__:expired
        // ...
    }
};