1

i have meet a problem when i use redis in multi-threads environment. some redis config codes below.

@Bean
public RedisTemplate<String, String> redisTemplate() {
    StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory());
    redisTemplate.setDefaultSerializer(serializer());
    return redisTemplate;
}

private RedisConnectionFactory connectionFactory() {
    JedisShardInfo shardInfo = new JedisShardInfo(host, port);
    JedisConnectionFactory factory = new JedisConnectionFactory(shardInfo);
    factory.setPassword(password);
    factory.setDatabase(db);
    factory.setTimeout(timeOut);
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(20);
    factory.setPoolConfig(config);
    return factory;
}

i execute a brpoplpush operation in multi-threads. some codes below

     RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        try {
            value = redisConnection.bRPopLPush(5, RWeixinDao.WEIXIN_MSG_KEY.getBytes(), key.getBytes());
        } finally {
            redisConnection.close();
        }

and i got some exceptions

23:35:56.859 [pool-1-thread-4] ERROR c.j.n.i.s.impl.MessageProcessorImpl - process weixin message fail. org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Address already in use at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:162) at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251) at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58) at com.jiyu.nowitzki.im.service.impl.MessageProcessorImpl.process(MessageProcessorImpl.java:82) at com.jiyu.nowitzki.im.service.impl.MessageProcessorImpl.lambda$run$0(MessageProcessorImpl.java:63) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Address already in use at redis.clients.jedis.Connection.connect(Connection.java:164) at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:82) at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1641) at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:159) ... 7 common frames omitted

it says this code is wrong RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection(); does it cause by i call the close() method? anyone could tell me plz... thxs

meisei
  • 21
  • 7
  • You're probably running out of [ephemeral ports](https://en.wikipedia.org/wiki/Ephemeral_port). – mp911de Apr 09 '16 at 20:42
  • @mp911de how does it happen? i'm lack of tcp/os knowledge. can you give more details thanks. – meisei Apr 10 '16 at 12:33
  • Check this post: http://stackoverflow.com/q/2332741/2067527 – mp911de Apr 10 '16 at 13:35
  • @mp911de thanks a lot. I still can't understand why my redis application could throw the exception. does it cause by the multi-threads? – meisei Apr 10 '16 at 16:38

0 Answers0