skip to Main Content

I am new to redis and i am using spring data redis for implementing some task queue feautre. Since i wanna do some failover checking, is there any a way to get the number of subscriber like in command "pubsub numsub " for a specific channel. Many thanks

2

Answers


  1. if you use spring reactive-Redis with spring boot you can create a specific topic and get the all subscribers that received the published event as a return by publishing a vent ny using convertAndSend method of ReactiveRedisTemplate. but the spring doesn’t provide that in imperative way. the convertAndSend method of RedisTemplate doesn’t return anything. the return type is void.

            stringStringReactiveRedisTemplate
                    .convertAndSend(
                            "your topic",
                            "event message"
                    ).subscribe(receivedCount -> {
                        System.out.println("num of subscribers the event received: " + receivedCount);
                    });
    
    Login or Signup to reply.
  2. you can another way by using the redis connection. it doesn’t matter if you use with spring boot. you can get the connection from the redisTemplate and call the publish method to publish the event like below. then you have to provide your channel name and the message by byte[].

            Long receivedCount = redisTemplate.getConnectionFactory().getConnection().publish(
                    "your channel".getBytes(),
                    "message".getBytes()
            );
    

    now you will receive how many live subscribers that event received. (only for that mentioned topic name).
    read more https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:pubsub:publish

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search