怎么用java使用redlock
发布时间:2023-04-22 13:36:38 所属栏目:教程 来源:
导读:这篇文章将为大家详细讲解有关怎么用java使用redlock,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
一、在pom文件引入redis和redisson依赖:
<!-- redi
一、在pom文件引入redis和redisson依赖:
<!-- redi
|
这篇文章将为大家详细讲解有关怎么用java使用redlock,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。 一、在pom文件引入redis和redisson依赖: <!-- redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- redisson--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.3.2</version> </dependency> AquiredLockWorker接口类,,主要是用于获取锁后需要处理的逻辑: /** * Created by fangzhipeng on 2017/4/5. * 获取锁后需要处理的逻辑 */ public interface AquiredLockWorker<T> { T invokeAfterLockAquire() throws Exception; } 二、distributedLocker 获取锁管理类: /** * Created by fangzhipeng on 2017/4/5. * 获取锁管理类 */ public interface distributedLocker { /** * 获取锁 * @param resourceName 锁的名称 * @param worker 获取锁后的处理类 * @param <T> * @return 处理完具体的业务逻辑要返回的数据 * @throws UnabletoAquireLockException * @throws Exception */ <T> T lock(String resourceName, AquiredLockWorker<T> worker) throws UnabletoAquireLockException, Exception; <T> T lock(String resourceName, AquiredLockWorker<T> worker, int lockTime) throws UnabletoAquireLockException, Exception; } nabletoAquireLockException ,不能获取锁的异常类: /** * Created by fangzhipeng on 2017/4/5. * 异常类 */ public class UnabletoAquireLockException extends RuntimeException { public UnabletoAquireLockException() { } public UnabletoAquireLockException(String message) { super(message); } public UnabletoAquireLockException(String message, Throwable cause) { super(message, cause); } } 三、RedissonConnector 连接类: /** * Created by fangzhipeng on 2017/4/5. * 获取RedissonClient连接类 */ @Component public class RedissonConnector { RedissonClient redisson; @postconstruct public void init(){ redisson = Redisson.create(); } public RedissonClient getClient(){ return redisson; } } 四、RedisLocker 类,实现了distributedLocker: import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * Created by fangzhipeng on 2017/4/5. */ @Component public class RedisLocker implements distributedLocker{ private final static String LOCKER_PREFIX = "lock:"; @Autowired RedissonConnector redissonConnector; @Override public <T> T lock(String resourceName, AquiredLockWorker<T> worker) throws InterruptedException, UnabletoAquireLockException, Exception { return lock(resourceName, worker, 100); } @Override public <T> T lock(String resourceName, AquiredLockWorker<T> worker, int lockTime) throws UnabletoAquireLockException, Exception { RedissonClient redisson= redissonConnector.getClient(); RLock lock = redisson.getLock(LOCKER_PREFIX + resourceName); // Wait for 100 seconds seconds and automatically unlock it after lockTime seconds boolean success = lock.tryLock(100, lockTime, TimeUnit.SECONDS); if (success) { try { return worker.invokeAfterLockAquire(); } finally { lock.unlock(); } } throw new UnabletoAquireLockException(); } } 五、测试类: @Autowired RedisLocker distributedLocker; @RequestMapping(value = "/redlock") public String testRedlock() throws Exception{ CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch donesignal = new CountDownLatch(5); for (int i = 0; i < 5; ++i) { // create and start threads new Thread(new Worker(startSignal, donesignal)).start(); } startSignal.countDown(); // let all threads proceed donesignal.await(); System.out.println("All processors done. Shutdown connection"); return "redlock"; } class Worker implements Runnable { private final CountDownLatch startSignal; private final CountDownLatch donesignal; Worker(CountDownLatch startSignal, CountDownLatch donesignal) { this.startSignal = startSignal; this.donesignal = donesignal; } public void run() { try { startSignal.await(); distributedLocker.lock("test",new AquiredLockWorker<Object>() { @Override public Object invokeAfterLockAquire() { doTask(); return null; } }); }catch (Exception e){ } } void doTask() { System.out.println(Thread.currentThread().getName() + " start"); Random random = new Random(); int _int = random.nextInt(200); System.out.println(Thread.currentThread().getName() + " sleep " + _int + "millis"); try { Thread.sleep(_int); } catch (InterruptedException e) { e.printstacktrace(); } System.out.println(Thread.currentThread().getName() + " end"); donesignal.countDown(); } } (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
