【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】
参考地址:https://www.jianshu.com/p/6f3ee90ab7d3
示例:
public static void main(String[] args) throws InterruptedException, ExecutionException {
CompletableFuture<String> cf1 = new CompletableFuture<>();
new Thread(() -> {
// 模拟执行耗时任务
System.out.println("task doing...");
try {
Thread.sleep(3000);
} catch (Exception e) {
cf1.completeExceptionally(e);
}
// 告诉completableFuture任务已经完成
cf1.complete("3");
}).start();
CompletableFuture<String> cf2 = new CompletableFuture<>();
new Thread(() -> {
// 模拟执行耗时任务
System.out.println("task doing...");
try {
Thread.sleep(1000);
} catch (Exception e) {
cf2.completeExceptionally(e);
}
// 告诉completableFuture任务已经完成
cf2.complete("1");
}).start();
System.out.println(cf1.get());
System.out.println(cf2.get());
}
在spring cloud微服务中调用分别几个其他微服务中的服务接口,放置单线程进行调用,导致接口超时的问题,应用Completablefuture 解决:
@RestController
public class TenTenementApiImpl implements ITenTenementApi{ @Autowired
private SysTenementService sysTenementService; @Autowired
private SysTenementConfigService sysTenementConfigService; @Autowired
FeignTenBrandClient brandClient; @Autowired
FeignTenDealerClient tenDealerClient; @Autowired
FeignTenMemberClient tenMemberClient; @Autowired
TenSecurityCodeCountClient tenSecurityCodeCountClient; /**
* 租户首页 统计
* 企业概况
*
* @return
*/
@Override
public AjaxResult<TenStatisticalBean> tenStatistical() { AjaxResult<TenStatisticalBean> res = new AjaxResult<>();
TenementUser tenementUser = RequestData.TENEMENT_USER.get(); //获取租户的企业参数 获取 租户生成码 展示 内码/外码
SysTenementConfig sysTenementConfig = sysTenementConfigService.findByTenementId(tenementUser.getTenementId());
if (sysTenementConfig != null){ String tenJson = JSON.toJSONString(tenementUser);
TenStatisticalBean bean = new TenStatisticalBean();
bean.setTid(tenementUser.getTenementId());
try { //商品统计,调用ms-goods服务
CompletableFuture<Integer> cf1 = new CompletableFuture<>();
new Thread(() -> { System.out.println("异步商品统计---->");
Integer goodsCount = 0;
try {
AjaxResult<Integer> goodsRes = brandClient.countGoodsByTid(tenJson);
if (goodsRes.isSuccess()){
goodsCount = goodsRes.getObj();
}
} catch (Exception e) {
goodsCount = null;
}
// 告诉completableFuture任务已经完成
cf1.complete(goodsCount);
}).start(); //经销商统计,调用ms-dealer服务
CompletableFuture<Integer> cf2 = new CompletableFuture<>();
new Thread(() -> { System.out.println("异步经销商统计---->");
Integer dealerCount = 0;
try {
AjaxResult<Integer> dealerRes = tenDealerClient.countDealerByTid(tenJson);
if (dealerRes.isSuccess()){
dealerCount = dealerRes.getObj();
}
} catch (Exception e) {
dealerCount = null;
}
// 告诉completableFuture任务已经完成
cf2.complete(dealerCount);
}).start(); //会员统计,调用ms-member服务
CompletableFuture<Integer> cf3 = new CompletableFuture<>();
new Thread(() -> { System.out.println("异步会员统计---->");
Integer memberCount = 0;
try {
AjaxResult<Integer> memberRes = tenMemberClient.countMemberByTid(tenJson);
if (memberRes.isSuccess()){
memberCount = memberRes.getObj();
}
} catch (Exception e) {
memberCount = null;
}
// 告诉completableFuture任务已经完成
cf3.complete(memberCount);
}).start(); //防伪码统计,调用ms-code服务
CompletableFuture<SecurityCodeCountBean> cf4 = new CompletableFuture<>();
new Thread(() -> { System.out.println("异步防伪码统计---->");
SecurityCodeCountBean securityCodeCount = new SecurityCodeCountBean();
try {
AjaxResult<SecurityCodeCountBean> scRes = tenSecurityCodeCountClient.countScCodeByTid(tenJson);
securityCodeCount = scRes.getObj();
if (scRes.isSuccess() && securityCodeCount != null){ }
} catch (Exception e) {
securityCodeCount = null;
}
// 告诉completableFuture任务已经完成
cf4.complete(securityCodeCount);
}).start(); bean.setGoodsCount(cf1.get());
bean.setDealerCount(cf2.get());
bean.setMemberCount(cf3.get());
SecurityCodeCountBean securityCodeCount = cf4.get();
if (securityCodeCount != null){
//设置码号生成总数
bean.setScCodeCount(securityCodeCount.getScCode());
//设置历史库存
bean.setHistoryStock(securityCodeCount.getHistoryStock());
//即时库存
bean.setImmediateStock(securityCodeCount.getImmediateStock());
//经销商库存
bean.setDealerStock(securityCodeCount.getDealerStock());
//已出售
bean.setSellCount(securityCodeCount.getSellCount());
//在途
bean.setOnWayCount(securityCodeCount.getOnWayCount());
} res.initTrue(bean);
} catch (InterruptedException e) {
throw new LunaException(e.getMessage(), LunaResultBean.ERROR_BUSINESS);
} catch (ExecutionException e) {
throw new LunaException(e.getMessage(), LunaResultBean.ERROR_BUSINESS);
}
}else {
res.initFalse("租户未配置企业参数",LunaResultBean.ERROR_BUSINESS);
}
return res;
}
}
附接收的实体:
public class TenStatisticalBean {
private String tid;//租户ID
private Integer goodsCount;//商品统计
private Integer dealerCount;//经销商统计
private Integer memberCount;//会员统计
private Long scCodeCount;//码号生成统计【此处及以下防伪码相关字段 都只展示 租户配置的产品码类型 内码量/外码量】
private Long historyStock;//历史库存 总共生成码 入 数据库的总量
private Long immediateStock;//即时库存 租户仓库 的库存数量 发货给经销商- 经销商退货给租户+
private Long onWayCount;//在途统计
private Long dealerStock;//经销商库存 经销商发货- 经销商收货+ 出售- 出售的退货+
private Long sellCount;//出售统计 所有出售的+ 退货给经销商-
public Long getOnWayCount() {
return onWayCount;
}
public void setOnWayCount(Long onWayCount) {
this.onWayCount = onWayCount;
}
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public Integer getGoodsCount() {
return goodsCount;
}
public void setGoodsCount(Integer goodsCount) {
this.goodsCount = goodsCount;
}
public Integer getDealerCount() {
return dealerCount;
}
public void setDealerCount(Integer dealerCount) {
this.dealerCount = dealerCount;
}
public Integer getMemberCount() {
return memberCount;
}
public void setMemberCount(Integer memberCount) {
this.memberCount = memberCount;
}
public Long getScCodeCount() {
return scCodeCount;
}
public void setScCodeCount(Long scCodeCount) {
this.scCodeCount = scCodeCount;
}
public Long getHistoryStock() {
return historyStock;
}
public void setHistoryStock(Long historyStock) {
this.historyStock = historyStock;
}
public Long getImmediateStock() {
return immediateStock;
}
public void setImmediateStock(Long immediateStock) {
this.immediateStock = immediateStock;
}
public Long getDealerStock() {
return dealerStock;
}
public void setDealerStock(Long dealerStock) {
this.dealerStock = dealerStock;
}
public Long getSellCount() {
return sellCount;
}
public void setSellCount(Long sellCount) {
this.sellCount = sellCount;
}
}
【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】的更多相关文章
- Spring Cloud微服务Sentinel+Apollo限流、熔断实战总结
在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实 ...
- Spring Cloud微服务限流之Sentinel+Apollo生产实践
Sentinel概述 在基于Spring Cloud构建的微服务体系中,服务之间的调用链路会随着系统的演进变得越来越长,这无疑会增加了整个系统的不可靠因素.在并发流量比较高的情况下,由于网络调用之间存 ...
- Spring Cloud微服务学习笔记
Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...
- Spring Cloud微服务系列文,服务调用框架Feign
之前博文的案例中,我们是通过RestTemplate来调用服务,而Feign框架则在此基础上做了一层封装,比如,可以通过注解等方式来绑定参数,或者以声明的方式来指定请求返回类型是JSON. 这种 ...
- 如何优化Spring Cloud微服务注册中心架构?
作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...
- Dubbo和Spring Cloud微服务架构比较
Dubbo 出生于阿里系,是阿里巴巴服务化治理的核心框架,并被广泛应用于中国各互联网公司:只需要通过 Spring 配置的方式即可完成服务化,对于应用无入侵,设计的目的还是服务于自身的业务为主. 微服 ...
- 在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用
本文为阿里云容器服务Spring Cloud应用开发系列文章的第一篇. 一.在阿里云容器服务上开发Spring Cloud微服务应用(本文) 二.部署Spring Cloud应用示例 三.服务发现 四 ...
- Dubbo 和 Spring Cloud微服务架构 比较及相关差异
你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构. 微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务, ...
- 全链路实践Spring Cloud 微服务架构
Spring Cloud 微服务架构全链路实践Spring Cloud 微服务架构全链路实践 阅读目录: 网关请求流程 Eureka 服务治理 Config 配置中心 Hystrix 监控 服务调用链 ...
随机推荐
- mac os版本Intellij IDEA 搭建spring mvc的maven工程(新手教学)
由于近期换了新公司,又换mac pro作为新电脑,打算把用了很多年的eclipse换成IDEA(IDEA比eclipse的好处我就不多说了),由于mac os和IDEA刚开始用不久,所以专门用一篇博客 ...
- 简易web-slide
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 前端网页进度Loading
loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在刷新和加载的过程中为了让用户感知到 load 的过程,我们会使用一些过渡动画来表达.最常见的比如“转圈圈”,“省略号”等等. ...
- CSU 1240 低调,低调。
原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1240 这道题已经做了很久了,加入给足够大的内存,谁都会做. 在一个数列中找一个只出现一次 ...
- Spring MVC数据绑定(一)
1.数据绑定介绍 在执行程序时,Spring MVC会根据客户端请求参数的不同,将请求消息中的信息以一定的方式转换并绑定到控制器类的方法参数中.这种将请求消息数据与后台方法参数建立连接的过程就是Spr ...
- 【严蔚敏】【数据结构(C语言版)】 求n的阶乘
阶乘函数为: 使用递归即可求得. #include <stdio.h> #include <stdlib.h> int Fact(int m){ ) ; ); //递归求阶乘 ...
- Python的环境搭建——万丈高楼平地起
Python的环境搭建,远程连接,端口映射,虚拟机 写在正文之前 python语言的开发环境还是相对比较简单的,但是也是有很多需要注意的地方,对于初次接触python或者以前很少用到虚拟环境的朋友来说 ...
- 抓包分析LVS-NAT中出现的SYN_RECV
CIP:192.168.10.193 VIP:192.168.10.152:8000 DIP:100.10.8.152:8000 RIP:100.10.8.101:8000 和 100.10.8.10 ...
- 深入理解javascript函数系列第一篇
前面的话 函数对任何一门语言来说都是核心的概念.通过函数可以封装任意多条语句,而且可以在任何地方.任何时候调用执行.在javascript里,函数即对象,程序可以随意操控它们.函数可以嵌套在其他函数中 ...
- 【模式匹配】更快的Boyer
1. 引言 前一篇中介绍了字符串KMP算法,其利用失配时已匹配的字符信息,以确定下一次匹配时模式串的起始位置.本文所要介绍的Boyer-Moore算法是一种比KMP更快的字符串匹配算法,它到底是怎么快 ...