测试类
package com.controller.yibu;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@RequestMapping("/test")
@Slf4j
@RestController
public class TestYiBu {
@Resource
private PdfConvertImgUtils pdfConvertImgUtils;
@GetMapping("/upload")
public Object exportPDF(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Map<String, String> xdMap = new HashMap<>(16);
xdMap.put("printUrl", "printUrl");
List<String> resultList = new LinkedList<>();
String printUrl = xdMap.get("printUrl");
String orderId = "123456";
if (StringUtils.isNotBlank(printUrl)) {
resultList.add(printUrl);
pdfConvertImgUtils.asyncPdfConvertImgUploadObs(printUrl, orderId, 8L);
}
System.out.println("orderId = " + orderId);
System.out.println("orderId = " + orderId);
System.out.println("orderId = " + orderId);
System.out.println("orderId = " + orderId);
return "upload##";
}
}
异步工具类
package com.controller.yibu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@Slf4j
@Component
public class PdfConvertImgUtils {
public List<String> downloadPdfConvertImgUtils(String pdfUrl) {
List<String> list = new ArrayList<>();
try {
Thread.sleep(2000);
System.out.println("线程睡眠2秒");
} catch (InterruptedException e) {
e.printStackTrace();
}
list.add("a");
list.add("c");
list.add("d");
list.add(pdfUrl);
return list;
}
private String upload(BufferedImage image, int i) {
String result = "upload";
System.out.println("执行上传图片的业务逻辑 = " + result);
return result;
}
public void asyncPdfConvertImgUploadObs(String pdfUrl, String id, Long timeout) {
log.info("提交订单异步PDF转图片上传ID: " + id);
String ibsPrintAsync = "IBSPrintAsync" + id;
List<String> list = new ArrayList<>();
list.add(id);
CompletableFuture<List<String>> future = CompletableFuture.supplyAsync(() -> this.downloadPdfConvertImgUtils(pdfUrl), CheckFileUtils.THREAD_POOL);
future.whenComplete((result, exception) -> {
if (result != null && result.size() > 0 && exception == null) {
List<String> result2 = result;
System.out.println("##result2 = " + result2);
try {
List<String> list1 = future.get();
System.out.println("###list1= " + list1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
log.warn("提交订单异步PDF转图片上传OBS异常: " + exception.getMessage());
}
});
}
}
线程池
package com.controller.yibu;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Slf4j
public class CheckFileUtils {
public static final ThreadPoolExecutor THREAD_POOL = new ThreadPoolExecutor(500, 1000,
5, TimeUnit.SECONDS,
new LinkedBlockingQueue(500),
new ThreadPoolExecutor.CallerRunsPolicy());
}