JedisSentinelPool是线程安全的吗?
我在写redis连接工具类后,在使用时发现会报异常,查了一些资料jredis,jredis不是线程安全,推荐使用pool,在修改代码后发现还有有问题,pool.getResource()这个方法是线程安全的吗?
private static Logger logger = LoggerFactory.getLogger(Redis.class); private JedisSentinelPool pool; private static Redis instance = null; public static Redis getInstance(){ if (instance == null) { throw new RuntimeException("Do not initialize!!!"); } return instance; } public static void buildInstance(Map conf) { if (instance == null) { synchronized (Redis.class) { instance = new Redis(); instance.init(conf); } } } private Redis() {} private void init(Map conf){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(Integer.valueOf(String.valueOf(conf.get("redis.max_idle")))); config.setTestOnBorrow(Boolean.valueOf(String.valueOf(conf.get("redis.isTest")))); config.setMaxTotal(Integer.valueOf(String.valueOf(conf.get("redis.maxTotal")))); String node = String.valueOf(conf.get("redis.node")); int timeOut = Integer.valueOf(String.valueOf(conf.get("redis.timeout"))); String[] addressArr = String.valueOf(conf.get("redis.server")).split(","); Set<String> sentinels = new HashSet<String>(); for (String str : addressArr) { sentinels.add(str); } pool = new JedisSentinelPool(node, sentinels, config, timeOut); } private Jedis getRedisTemplate() { Jedis resource = pool.getResource(); return resource; } /** public */void setValue(byte[] key, byte[] value) { Jedis jedis = null; try { jedis = getRedisTemplate(); jedis.set(key, value); closeJedis(jedis); } catch (Exception e) { e.printStackTrace(); closeBreakJedis(jedis); } } java.lang.ClassCastException: java.lang.Long cannot be cast to [B at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:216) at redis.clients.jedis.Connection.getBulkReply(Connection.java:205) at redis.clients.jedis.Jedis.hget(Jedis.java:622) at com.mapbar.flash.common.cache.Redis.getHashValue(Redis.java:300) at com.mapbar.flash.common.cache.TerminalCache.getCityCodeArray(TerminalCache.java:35) at com.mapbar.flash.core.bolt.ElectronicFenceBolt.doExecute(ElectronicFenceBolt.java:106) at com.mapbar.flash.core.bolt.ElectronicFenceBolt.doExecute(ElectronicFenceBolt.java:1) at com.mapbar.flash.core.bolt.BaseBolt.execute(BaseBolt.java:79) at backtype.storm.topology.BasicBoltExecutor.execute(BasicBoltExecutor.java:49) at com.alibaba.jstorm.task.execute.BoltExecutors.processTupleEvent(BoltExecutors.java:183) at com.alibaba.jstorm.task.execute.BoltExecutors.onEvent(BoltExecutors.java:161) at backtype.storm.utils.DisruptorQueueImpl.consumeBatchToCursor(DisruptorQueueImpl.java:191) at backtype.storm.utils.DisruptorQueueImpl.consumeBatchWhenAvailable(DisruptorQueueImpl.java:159) at com.alibaba.jstorm.task.execute.BoltExecutors.run(BoltExecutors.java:137) at com.alibaba.jstorm.callback.AsyncLoopRunnable.run(AsyncLoopRunnable.java:95) at java.lang.Thread.run(Thread.java:745)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的问题描述,我建议你使用别人做好的轮子,Spring Data Redis
http://projects.spring.io/spring-data-redis/
getSource()是线程安全的,它是调用GenericObjectPool的borrowObject方法。底下截图的是borrowObject的部分源码
其中idleObjects是一个LinkedBlockingDeque,它的操作是阻塞的,所以是安全的