mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
去除无限重试方法
This commit is contained in:
parent
c317ef1e08
commit
e925b5ed74
@ -7,6 +7,9 @@ import org.dromara.hutool.core.thread.ThreadUtil;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -112,37 +115,4 @@ public class RetryUtil {
|
||||
.orElseGet(recover);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从不停止的执行方法
|
||||
*
|
||||
* @param run 执行方法
|
||||
* @param delay 间隔时间
|
||||
* @param isEx true:出现异常继续执行;false:则出现异常跳出执行。
|
||||
*/
|
||||
public static void ofNeverStop(Runnable run, Duration delay, boolean isEx) {
|
||||
while (true) {
|
||||
try {
|
||||
run.run();
|
||||
} catch (Throwable e) {
|
||||
if (!isEx) {
|
||||
break;
|
||||
}
|
||||
} finally {
|
||||
ThreadUtil.sleep(delay.toMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从不停止的执行方法,异步执行
|
||||
*
|
||||
* @param run 执行方法
|
||||
* @param delay 间隔时间
|
||||
* @param isEx true:出现异常继续执行;false:则出现异常跳出执行。
|
||||
*/
|
||||
public static void ofNeverStopAsync(Runnable run, Duration delay, boolean isEx) {
|
||||
CompletableFuture.runAsync(() -> ofNeverStop(run, delay, isEx), GlobalThreadPool.getExecutor());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -72,20 +72,4 @@ public class RetryUtilTest {
|
||||
Assertions.assertEquals("ok", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void neverStop() {
|
||||
//异步一直执行
|
||||
RetryUtil.ofNeverStopAsync(() -> {
|
||||
System.out.println("async -->");
|
||||
}, Duration.ofSeconds(1), true);
|
||||
|
||||
System.out.println(" ================ ");
|
||||
//同步一直执行
|
||||
RetryUtil.ofNeverStop(() -> {
|
||||
System.out.println(123);
|
||||
}, Duration.ofSeconds(3), true);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user