2016-03-18 14 views
1

RedisTemplateはPUBSUBチャネルコマンドをサポートしていません。したがって、次のような方法があります。SpringデータRedis - コマンドPUBSUBチャネルのサポート

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() 
     } 
    } 

これは推奨されるアプローチですか?

答えて

0

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

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のスタンドアロン/ Redisのセンチネルでのみ動作しますが、RedisのクラスタでJedisClusterとしてpubsubChannelsを公開するものではありませんのでご注意ください。

関連する問題