diff --git a/rxlib-x/src/main/java/org/rx/redis/RedisUtil.java b/rxlib-x/src/main/java/org/rx/redis/RedisUtil.java index 810f07be..6484adc4 100644 --- a/rxlib-x/src/main/java/org/rx/redis/RedisUtil.java +++ b/rxlib-x/src/main/java/org/rx/redis/RedisUtil.java @@ -1,8 +1,10 @@ package org.rx.redis; import com.google.common.util.concurrent.RateLimiter; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; import org.rx.core.Cache; import org.rx.core.Strings; import org.rx.core.Sys; @@ -14,6 +16,15 @@ @Slf4j public class RedisUtil { + public static Lock wrapLock(@NonNull RedissonClient redissonClient, @NonNull String lockName) { + return Sys.fallbackProxy(Lock.class, new Lazy<>(() -> redissonClient.getLock(lockName)), new Lazy<>(ReentrantLock::new), e -> { + if (Strings.hashEquals(e.getMethod().getName(), "unlock")) { + return null; + } + throw e; + }); + } + public static Lock wrapLock(RLock rLock) { return Sys.fallbackProxy(Lock.class, rLock, new Lazy<>(ReentrantLock::new), e -> { if (Strings.hashEquals(e.getMethod().getName(), "unlock")) { @@ -24,14 +35,14 @@ public static Lock wrapLock(RLock rLock) { }); } - public static Cache wrapCache(RedisCache rCache) { - return Sys.fallbackProxy(Cache.class, rCache, new Lazy<>(() -> Cache.getInstance(MemoryCache.class))); - } - public static RateLimiterAdapter wrapRateLimiter(RedisRateLimiter rRateLimiter) { return Sys.fallbackProxy(RateLimiterAdapter.class, rRateLimiter, new Lazy<>(() -> { RateLimiter limiter = RateLimiter.create(rRateLimiter.getPermitsPerSecond()); return () -> limiter.tryAcquire(); })); } + + public static Cache wrapCache(RedisCache rCache) { + return Sys.fallbackProxy(Cache.class, rCache, new Lazy<>(() -> Cache.getInstance(MemoryCache.class))); + } } diff --git a/rxlib/src/main/java/org/rx/core/Sys.java b/rxlib/src/main/java/org/rx/core/Sys.java index 44dbd047..9f41e97b 100644 --- a/rxlib/src/main/java/org/rx/core/Sys.java +++ b/rxlib/src/main/java/org/rx/core/Sys.java @@ -273,29 +273,39 @@ public static File getJarFile(Class klass) { return new File(path); } + public static T fallbackProxy(Class targetType, Lazy target, Lazy fallbackTarget) { + return fallbackProxy(targetType, target, fallbackTarget, null); + } + + public static T fallbackProxy(@NonNull Class targetType, Lazy target, Lazy fallbackTarget, BiFunc onError) { + return proxy(targetType, (m, p) -> innerFallbackProxy(m, p, target.getValue(), fallbackTarget, onError)); + } + public static T fallbackProxy(Class targetType, T target, Lazy fallbackTarget) { return fallbackProxy(targetType, target, fallbackTarget, null); } - public static T fallbackProxy(@NonNull Class targetType, @NonNull T target, @NonNull Lazy fallbackTarget, BiFunc onError) { - return proxy(targetType, (m, p) -> { + public static T fallbackProxy(@NonNull Class targetType, T target, Lazy fallbackTarget, BiFunc onError) { + return proxy(targetType, (m, p) -> innerFallbackProxy(m, p, target, fallbackTarget, onError)); + } + + static Object innerFallbackProxy(Method m, DynamicProxyBean p, @NonNull T target, @NonNull Lazy fallbackTarget, BiFunc onError) { + try { + return p.fastInvoke(target); + } catch (Throwable e) { + T fallbackTargetValue = fallbackTarget.getValue(); try { - return p.fastInvoke(target); - } catch (Throwable e) { - T value = fallbackTarget.getValue(); - try { - Object r = p.fastInvoke(value); - log.warn("fallbackProxy", e); - return r; - } catch (Throwable fe) { - FallbackException fb = new FallbackException(m, p, target, value, e, fe); - if (onError == null) { - throw fb; - } - return onError.invoke(fb); + Object r = p.fastInvoke(fallbackTargetValue); + log.warn("fallbackProxy", e); + return r; + } catch (Throwable fe) { + FallbackException fb = new FallbackException(m, p, target, fallbackTargetValue, e, fe); + if (onError == null) { + throw fb; } + return onError.apply(fb); } - }); + } } public static T targetObject(Object proxyObject) {