package com.example.demo.future;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class FutureTest {
/**
* 获取门店id
*
* @return
*/
private static Long getStoreId() {
try {
Thread.sleep(1500); //拟处理远程处理时间
} catch (InterruptedException e) {
e.printStackTrace();
}
return Math.round(Math.random() * 10000);
}
/**
* 根据门店Id获取订单数据
*
* @return
*/
private static String getData(String sign) {
try {
Thread.sleep(1500); //拟处理远程处理时间
} catch (InterruptedException e) {
e.printStackTrace();
}
return String.format("{storeId:%s,orderNo:'%s',sign:%s}",
Math.round(Math.random() * 10000), UUID.randomUUID().toString(), sign);
}
public static void main(String[] args) {
CompletableFuture<String> orderFuture = CompletableFuture
.supplyAsync(FutureTest::getStoreId) //第一步,从远程先获取门店id
.thenApply(Long::toHexString) //第二步,将门店id进行签名
.thenCompose(sign -> CompletableFuture.supplyAsync(() -> getData(sign))); //第三步,从远程获取订单数据
}
}