去除无限重试方法

This commit is contained in:
kongweiguang 2023-06-20 19:16:18 +08:00
parent c317ef1e08
commit e925b5ed74
2 changed files with 3 additions and 49 deletions

View File

@ -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());
}
}

View File

@ -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);
}
}