我知道jredis不是线程安全的,应该说互联网上使用的是jedissentelpool,它是线程安全的,但是我使用的,发现的不是这个情况,我不知道是否使用有问题。

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)

最佳答案

是我的代码问题,在一个方法中,我在查询之前返回jredis。

关于java - JedisSentinelPool是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41774927/

10-16 19:28