001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.objects.lock.impl; 017 018import io.jboot.objects.lock.JbootLock; 019 020import java.util.concurrent.TimeUnit; 021import java.util.concurrent.locks.Condition; 022 023/** 024 * @author michael yang (fuhai999@gmail.com) 025 * @Date: 2020/3/7 026 */ 027public class JbootRedisLock implements JbootLock { 028 029 private io.jboot.support.redis.JbootRedisLock lock ; 030 private String name; 031 032 public JbootRedisLock(String name) { 033 this.name = name; 034 this.lock = new io.jboot.support.redis.JbootRedisLock(name); 035 } 036 037 @Override 038 public void lock() { 039 lock.setTimeoutMsecs(60 * 1000); // 1 分钟 040 if (!lock.acquire()){ 041 throw new IllegalStateException("can not get lock in JbootRedisLock"); 042 } 043 } 044 045 @Override 046 public void lockInterruptibly() throws InterruptedException { 047 throw new UnsupportedOperationException(); 048 } 049 050 @Override 051 public boolean tryLock() { 052 return lock.acquire(); 053 } 054 055 @Override 056 public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { 057 lock.setTimeoutMsecs(unit.toMillis(time)); 058 return lock.acquire(); 059 } 060 061 @Override 062 public void unlock() { 063 lock.release(); 064 } 065 066 @Override 067 public Condition newCondition() { 068 throw new UnsupportedOperationException(); 069 } 070}