RedisTemplate使用键操作redis.clients.jedis.exceptions.JedisConnectionException:发生意外的流结束

时间:2017-03-23 06:57:51

标签: java caching redis spring-data-redis

当我使用redisTemplate.keys获取匹配模式键时。出现一个异常。 但是当我在我的redis服务器上运行'keys'命令时,它可以工作。

下面是堆栈跟踪。

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.
    at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:199) ~[jedis-2.9.0.jar:?]
    at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40) ~[jedis-2.9.0.jar:?]
    at redis.clients.jedis.Protocol.process(Protocol.java:151) ~[jedis-2.9.0.jar:?]
    at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[jedis-2.9.0.jar:?]
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[jedis-2.9.0.jar:?]
    at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:276) ~[jedis-2.9.0.jar:?]
    at redis.clients.jedis.BinaryJedis.keys(BinaryJedis.java:357) ~[jedis-2.9.0.jar:?]
    at org.springframework.data.redis.connection.jedis.JedisConnection.keys(JedisConnection.java:871) ~[spring-data-redis-1.8.1.RELEASE.jar:?]
    ... 49 more

这是我的java代码。

Redis配置。

public class DefaultRedisConfig {
    @Resource
    protected Environment env;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(Converter.toInt(env.getRequiredProperty("redis.maxIdle"), 18));
        jedisPoolConfig.setMaxWaitMillis(env.getProperty("redis.max.wait.time", int.class, Protocol.DEFAULT_TIMEOUT));
        jedisPoolConfig.setMaxTotal(env.getProperty("redis.max.total", int.class, 8));
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setTestOnReturn(true);
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(env.getProperty("redis.host"));
        factory.setPassword(env.getProperty("redis.password"));
        factory.setPort(Converter.toInt(env.getRequiredProperty("redis.port"), 6379));
        factory.setUsePool(true);
        factory.setPoolConfig(jedisPoolConfig());
        return factory;
    }

    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

}

测试代码。

@Resource
private RedisTemplate redisTemplate;

public void testCommandKeys() {
    Set keys = redisTemplate.keys("Ge.*");
}

0 个答案:

没有答案