1

RedisTemplate はPUBSUB CHANNELSコマンドをサポートしていません。したがって、1つの方法は次のようにすることです

   private JedisPool getJedisPool(){
        if (jedisPool == null)
        jedisPool = new JedisPool(redisConnectionFactory.getPoolConfig(), redisConnectionFactory.getHostName(), redisConnectionFactory.getPort());
        return jedisPool;
    }

    public Integer getNumChannels() {
        Integer count = 0;
        try (Jedis jedis = getJedisPool().getResource()) {
            List<String> channels = jedis.pubsubChannels("user.*");
            count = channels == null ? 0 : channels.size();
        } catch (Exception e) {
            logger.error("unable to get user count", e);
        } finally {
            //getJedisPool().close(); //No need for close or returnResource()
        }
    }

これは提案されたアプローチですか?

4

1 に答える 1

0

どこに向かっているかによります。独自のアプリケーションにのみ使用する場合は、基になるインスタンスJedisConnectionからを取得し、それを使用してコマンドを呼び出すことができます。JedisConnectionFactoryJedis

JedisConnectionFactory factory = …

// assuming you're using Redis Standalone or Redis Sentinel
RedisConnection connection = factory.getConnection();
try {
    if (connection instanceof JedisConnection) {
        Jedis jedis = ((JedisConnection) connection).getNativeConnection();
        List<String> strings = jedis.pubsubChannels("…");
    }
} finally {
    connection.close();
}

これは Redis Standalone/Redis Sentinel でのみ機能しますが、Redis Cluster では公開されJedisClusterないため、機能しないことに注意してくださいpubsubChannels

于 2016-03-19T17:26:37.873 に答える