redission使用

 


    org.redisson
    redisson
    3.13.6

@Configuration
public class RedissionConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.database}")
    private int database;

    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress("redis://" + redisHost + ":" + port)
                .setDatabase(database);
        if (StrUtil.isNotEmpty(password)) {
            serverConfig.setPassword(password);
        }
        config.setCodec(new JsonJacksonCodec());
        return Redisson.create(config);
    }
}

使用

@Resource
private RedissonClient redissonClient;
    public void testLock() {
        RLock lock = redissonClient.getLock("key");
        try {

            boolean lockFlag = lock.tryLock(0, -1, TimeUnit.SECONDS);

            System.out.println(DateUtil.now() + "  " + lockFlag);
        } catch (Exception e) {
        } finally {
           lock.unlock();
        }
    }

redission的看门狗失效问题

/**
*  waitTime 尝试获取锁的最大等待时间,超过这个值,则认为获取锁失败
*  leaseTime 这个参数字段表示租期,只要设置了租期,那么看门狗就失效了,不会续期
*/
boolean lockFlag = lock.tryLock(0, -1, TimeUnit.SECONDS);

你可能感兴趣的:(java,java)