添加重试

This commit is contained in:
shikong 2023-12-25 14:10:31 +08:00
parent b5068617bb
commit f1db3e3d1d

View File

@ -341,6 +341,8 @@ public class WvpService {
return deviceChannel; return deviceChannel;
} }
@SneakyThrows
@SuppressWarnings("UnstableApiUsage")
public DeferredResult<JsonResponse<String>> realtimeVideoUrl(String deviceCode) { public DeferredResult<JsonResponse<String>> realtimeVideoUrl(String deviceCode) {
DeferredResult<JsonResponse<String>> result = new DeferredResult<>(TimeUnit.SECONDS.toMillis(60)); DeferredResult<JsonResponse<String>> result = new DeferredResult<>(TimeUnit.SECONDS.toMillis(60));
result.onTimeout(() -> { result.onTimeout(() -> {
@ -354,7 +356,25 @@ public class WvpService {
} }
String token = login(); String token = login();
Optional<DeviceChannel> deviceChannel = getDeviceChannelByDeviceCode(token, deviceCode); Retryer<Optional<DeviceChannel>> defaultGenericRetryer = RetryerBuilder.<Optional<DeviceChannel>>newBuilder()
// 异常就重试
.retryIfException()
.retryIfRuntimeException()
// 重试间隔
.withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
// 重试次数
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.withRetryListener(new RetryListener() {
@Override
public <V> void onRetry(Attempt<V> attempt) {
if (attempt.hasException()) {
log.info("异常 {}", attempt.getExceptionCause().getMessage());
cache.remove("token");
}
}
}).build();
Optional<DeviceChannel> deviceChannel = defaultGenericRetryer.call(() -> getDeviceChannelByDeviceCode(token, deviceCode));
if (deviceChannel.isEmpty()) { if (deviceChannel.isEmpty()) {
result.setResult(JsonResponse.error(MessageFormat.format("未能获取 设备: {0} 的通道信息", deviceCode))); result.setResult(JsonResponse.error(MessageFormat.format("未能获取 设备: {0} 的通道信息", deviceCode)));
return result; return result;