Skip to content

Commit

Permalink
Merge branch 'master' into rxsocks
Browse files Browse the repository at this point in the history
  • Loading branch information
RockyLOMO committed Apr 23, 2024
2 parents 1eaf454 + 2050778 commit 9f741af
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.rockylomo</groupId>
<artifactId>rx</artifactId>
<version>2.20.1-SNAPSHOT</version>
<version>2.20.2-SNAPSHOT</version>
<packaging>pom</packaging>

<parent>
Expand Down
5 changes: 2 additions & 3 deletions rxlib-x/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.rockylomo</groupId>
<artifactId>rx</artifactId>
<version>2.20.1-SNAPSHOT</version>
<version>2.20.2-SNAPSHOT</version>
</parent>
<artifactId>rxlib-x</artifactId>
<packaging>jar</packaging>
Expand Down
19 changes: 15 additions & 4 deletions rxlib-x/src/main/java/org/rx/redis/RedisUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")) {
Expand All @@ -24,14 +35,14 @@ public static Lock wrapLock(RLock rLock) {
});
}

public static <TK, TV> Cache<TK, TV> wrapCache(RedisCache<TK, TV> 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 <TK, TV> Cache<TK, TV> wrapCache(RedisCache<TK, TV> rCache) {
return Sys.fallbackProxy(Cache.class, rCache, new Lazy<>(() -> Cache.getInstance(MemoryCache.class)));
}
}
8 changes: 4 additions & 4 deletions rxlib-x/src/main/java/org/rx/util/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ public static Map<String, List<Object[]>> readExcel(InputStream in, boolean is20
return data;
}

public static void writeExcel(OutputStream out, boolean is2003File, Map<String, Iterable<Object[]>> data) {
writeExcel(out, is2003File, data, null);
public static void writeExcel(OutputStream out, boolean is2003File, Iterable<Object[]> sheetRows) {
writeExcel(out, is2003File, Collections.singletonMap("sheet1", sheetRows), null);
}

@SneakyThrows
public static void writeExcel(OutputStream out, boolean is2003File, Map<String, Iterable<Object[]>> data, Function<Row, Row> onRow) {
public static void writeExcel(OutputStream out, boolean is2003File, Map<String, Iterable<Object[]>> sheets, Function<Row, Row> onRow) {
try (Workbook workbook = is2003File ? new HSSFWorkbook() : new XSSFWorkbook()) {
for (Map.Entry<String, Iterable<Object[]>> entry : data.entrySet()) {
for (Map.Entry<String, Iterable<Object[]>> entry : sheets.entrySet()) {
Sheet sheet = workbook.getSheet(entry.getKey());
if (sheet == null) {
sheet = workbook.createSheet(entry.getKey());
Expand Down
5 changes: 2 additions & 3 deletions rxlib/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.rockylomo</groupId>
<artifactId>rx</artifactId>
<version>2.20.1-SNAPSHOT</version>
<version>2.20.2-SNAPSHOT</version>
</parent>
<!-- -SNAPSHOT -->
<artifactId>rxlib</artifactId>
Expand Down
42 changes: 26 additions & 16 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,39 @@ public static File getJarFile(Class<?> klass) {
return new File(path);
}

public static <T> T fallbackProxy(Class<T> targetType, Lazy<T> target, Lazy<T> fallbackTarget) {
return fallbackProxy(targetType, target, fallbackTarget, null);
}

public static <T> T fallbackProxy(@NonNull Class<T> targetType, Lazy<T> target, Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
return proxy(targetType, (m, p) -> innerFallbackProxy(m, p, target.getValue(), fallbackTarget, onError));
}

public static <T> T fallbackProxy(Class<T> targetType, T target, Lazy<T> fallbackTarget) {
return fallbackProxy(targetType, target, fallbackTarget, null);
}

public static <T> T fallbackProxy(@NonNull Class<T> targetType, @NonNull T target, @NonNull Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
return proxy(targetType, (m, p) -> {
public static <T> T fallbackProxy(@NonNull Class<T> targetType, T target, Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> onError) {
return proxy(targetType, (m, p) -> innerFallbackProxy(m, p, target, fallbackTarget, onError));
}

static <T> Object innerFallbackProxy(Method m, DynamicProxyBean p, @NonNull T target, @NonNull Lazy<T> fallbackTarget, BiFunc<FallbackException, Object> 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> T targetObject(Object proxyObject) {
Expand Down

0 comments on commit 9f741af

Please sign in to comment.