当前位置: 首页 > news >正文

三网合一网站源代码做网站可以不做后端吗

三网合一网站源代码,做网站可以不做后端吗,长宁区网站建设,泉州网站建设外包当你需要不停调用第三方接口#xff0c;直到返回成功或失败时#xff0c;可以使用类似的异步重试的机制。以下是一个整合了这个要求的简单示例代码#xff1a; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.…当你需要不停调用第三方接口直到返回成功或失败时可以使用类似的异步重试的机制。以下是一个整合了这个要求的简单示例代码 import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class AsyncRetryThirdPartyApi { private static final ExecutorService executorService Executors.newFixedThreadPool(5); public static void main(String[] args) {         // 模拟调用第三方接口         CompletableFutureString apiCallResult callThirdPartyApi(); // 添加重试机制最多重试3次         CompletableFutureString retryFuture apiCallResult                 .thenComposeAsync(result - {                     if (processing.equals(result)) {                         // 如果返回的状态是处理中进行重试                         return retryWithBackoff();                     } else {                         // 如果是成功或失败状态则直接返回结果                         return CompletableFuture.completedFuture(result);                     }                 }, executorService); // 等待异步任务和重试任务完成         String finalResult retryFuture.join();         System.out.println(Final Result: finalResult); // 关闭线程池         executorService.shutdown();     } private static CompletableFutureString callThirdPartyApi() {         // 模拟调用第三方接口的异步任务         return CompletableFuture.supplyAsync(() - {             // 模拟第三方接口返回的状态这里随机生成 processing, success, 或 failure             double random Math.random();             if (random 0.3) {                 return processing;             } else if (random 0.6) {                 return success;             } else {                 return failure;             }         }, executorService);     } private static CompletableFutureString retryWithBackoff() {         // 使用指数退避exponential backoff策略进行重试         int maxRetries 3;         int retryCount 0;         long initialDelayMillis 100; // 初始延迟100毫秒 CompletableFutureString retryLoop new CompletableFuture();         retryLoop.completeExceptionally(new RuntimeException(Initial exception)); // 触发初始重试 while (retryCount maxRetries) {             retryLoop retryLoop.exceptionally(throwable - {                 // 打印重试信息                 System.out.println(Retrying... Attempt (retryCount 1)); // 模拟调用第三方接口的异步任务                 return callThirdPartyApi();             }).thenComposeAsync(result - {                 if (processing.equals(result)) {                     // 如果返回的状态仍然是处理中继续重试                     return CompletableFuture.completedFuture(null);                 } else {                     // 如果是成功或失败状态则直接返回结果                     return CompletableFuture.completedFuture(result);                 }             }, executorService); retryCount;             initialDelayMillis * 2; // 每次重试延迟加倍             try {                 TimeUnit.MILLISECONDS.sleep(initialDelayMillis);             } catch (InterruptedException e) {                 e.printStackTrace();             }         } return retryLoop;     } } / 是的你可以不使用并发框架来实现重试功能但需要手动处理线程的等待和异步任务。以下是一个简单的例子使用while循环和Thread.sleep()来实现重试 public class RetryWithoutConcurrency { public static void main(String[] args) {         int maxRetries 3;         int retryCount 0;         long retryIntervalMillis 1000; // 重试间隔为1秒 String result null;         while (retryCount maxRetries) {             try {                 result callThirdPartyApi();                 if (success.equals(result) || failure.equals(result)) {                     // 如果是成功或失败状态则退出循环                     break;                 } else {                     System.out.println(Still processing... Retrying after retryIntervalMillis milliseconds.);                     Thread.sleep(retryIntervalMillis);                 }             } catch (Exception e) {                 System.out.println(Retrying... Attempt (retryCount 1));             }             retryCount;         } System.out.println(Final Result: result);     } private static String callThirdPartyApi() {         // 模拟调用第三方接口         double random Math.random();         if (random 0.3) {             return processing;         } else if (random 0.6) {             return success;         } else {             return failure;         }     } } 在这个例子中callThirdPartyApi方法模拟了调用第三方接口而主方法通过while循环进行重试。注意这里使用了Thread.sleep()来等待一定的时间间隔。请注意这种方法在某些情况下可能不够灵活因为它是同步的可能会导致整个线程阻塞。如果你的应用需要更高的并发性能和灵活性使用并发框架通常是更好的选择。
http://www.pierceye.com/news/248301/

相关文章:

  • 软件网站怎么做的python下载安装教程
  • 旅游网站开发分析报告网站建设教程搭建芽嘱湖南岚鸿信赖
  • 网站的配色方案高校网站建设意义
  • 滇中引水工程建设管理局网站网站开发怎样验收
  • ps制作网站logo阿里云网站备案拍照
  • 网站建设合同】wordpress翻书
  • 电商网站建设制作隆化县建设局网站
  • 宁波网站建设rswl网页美工设计教案
  • 贵州省住房城乡建设部网站json网站开发
  • 桥头网站仿做百度里面的站长工具怎么取消
  • 博物馆网站页面设计说明山东高端网站定制
  • python网站开发效率jsp做网站下载图片
  • 营销式网站建设免费注册个人网站官网
  • 高职高专 网站建设与维护开发一个网站平台多少钱
  • 网站后缀有哪些宜昌建设网站
  • iis做网站的流程wordpress有中文版没
  • 一般的美工可以做网站吗网站做相册
  • 扁平化网站psd招聘类网站怎么做
  • 想当淘客自己的网站怎么做服装网页设计网站
  • 网站怎么做数据接口wordpress主题知更
  • 注册网站登录企业网站建设论文模板
  • 营销型网站模板免费下载常用wordpress搭建环境
  • 浦东新区手机网站建设wordpress 视频页面
  • 做课件最好的素材网站网站背景动图怎么做
  • 做网站时已做好了ps怎么倒入深圳燃气公司地址
  • 做类似淘宝的网站要多少钱亚马逊网站建设进度计划书
  • 够完美网站建设怎么把视频弄成超链接
  • 苏州网站建设哪家更好四川省建设工程信息网官网二建注册
  • 潍坊网站关键词推广湖南餐饮网站建设
  • 珠海网站建设优化推广win2008 iis7发布网站