Skip to content

upcgaolei/distributed-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Apr 28, 2017
ae18914 · Apr 28, 2017

History

29 Commits
Apr 28, 2017
Apr 28, 2017
Apr 28, 2017
Apr 28, 2017
Apr 28, 2017
Apr 28, 2017

Repository files navigation

Distributed-Lock

基于Redis和ZooKeeper实现的分布式锁

基于Redis实现的分布式锁(可重入)

@Test
    public void testRedisReentrantLock() {
        RedisReentrantLock lock = new RedisReentrantLock(jedisPool, "订单流水号");
        try {
            if(lock.tryLock(5000L, TimeUnit.MILLISECONDS)) {
                System.out.println("生成订单流水");
            } else {
                System.out.println("超时");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }

    @Test
    public void testRedisDistributedLockTemplate(){
        final RedisDistributedLockTemplate template=new RedisDistributedLockTemplate(jedisPool);
        template.execute("订单流水号", 5000, new Callback() {
            @Override
            public Object onGetLock() throws InterruptedException {
                System.out.println("生成订单流水");
                return null;
            }

            @Override
            public Object onTimeout() throws InterruptedException {
                System.out.println("超时");
                return null;
            }
        });
    }

About

基于Redis和ZK实现的分布式锁

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages