我在多线程环境中使用redis时遇到了问题。 下面是一些redis配置代码。
@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;
}
我在多线程中执行brpoplpush操作。下面的一些代码
RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
try {
value = redisConnection.bRPopLPush(5, RWeixinDao.WEIXIN_MSG_KEY.getBytes(), key.getBytes());
} finally {
redisConnection.close();
}
我有一些例外
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
它说这段代码错了
RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
它是否由我调用close()方法引起?
任何人都可以告诉我PLZ ...... thxs