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

几十万做网站平台江苏营销型网站公司

几十万做网站平台,江苏营销型网站公司,影视app搭建教程,怎样才能做一个优质的外贸网站当你需要不停调用第三方接口#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.zqtcl.cn/news/929684/

相关文章:

  • 企业营销网站建设公司哪家好网站建设团队名称
  • 法制网站建设问卷调查手机网站打开自动wap
  • 建设网站 如何给文件命名佛山网站推广市场
  • 网站客户问题解答网站建设网站规划书
  • 罗湖网站公司网络营销的种类有哪些
  • 怎么做微网站推广做一个自己的网站需要什么
  • 一个静态网站开发考虑什么最近一周新闻热点回顾
  • 北京网站设计知名乐云seo汝州建设局网站
  • 珠海左右创意园网站开发注册安全工程师报名条件和要求
  • 建设文明网站包括怎么用手机建设网站
  • 网站建设商城宁波seo深度优化平台有哪些
  • 免费企业查询网站网站建设有关的职位
  • 有哪些网站是可以做会计题目的广告网站建设设计
  • 房地产项目网站建设wordpress codecolorer
  • 网站服务器机房html5写的网站有什么好处
  • 三网合一网站源码下载宣传片拍摄手法及技巧
  • 重庆有网站公司公司做网站能抵扣进项税吗
  • 深圳南山网站开发卖东西的网站怎么建设
  • 网站开发教程全集网站内外链建设
  • 购物网站排名数商云科技
  • 哪种网站百度网盘登录入口官网
  • 做淘宝网站多少钱wordpress 七牛云存储
  • 做淘宝网站多少钱江苏省建设厅网站建筑电工证
  • 深圳网站建设 贴吧广州档案馆建设网站
  • 专注网站建设电商商城网站建设
  • 黄石专业网站建设推广一起做网店 网站打不开
  • 网站session 验证江西星子网
  • 成都高校网站建设服务公司小树建站平台
  • 宁波网站建设 慕枫科技顺德网站设计制作
  • 企业网站如何宣传wordpress 链接修改插件