单节点-高并发合并处理请求处理
1.code
package com.xll.frameworks.common.core.merge; import com.xll.frameworks.common.core.merge.base.BaseMergeRequestProcess; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.PostConstruct; import java.util.*; import java.util.concurrent.*; /** * 功能描述: <br> * <p> * 〈抽象处理合并〉 * </p> * @Author: xuliangliang * @Date: 2022/6/28 22:22 */ @Slf4j public abstract class AbstractMergeRequestHandle<I, O> { /** * 最大任务数 **/ public static int MAX_TASK_NUM = 100; @Autowired private BaseMergeRequestProcess<I, O> requestProcess; /** * 请求类,code为查询的共同特征,例如查询商品,通过不同id的来区分 * CompletableFuture将处理结果返回 */ @Data public class Request { /** * 请求id 唯一 */ private String requestId; /** * 业务唯一标识 */ private I inputDto; /** * 异步处理 */ private CompletableFuture<O> completableFuture; } private final Queue<Request> queue = new LinkedBlockingQueue<>(MAX_TASK_NUM*5); @PostConstruct public void init() { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); scheduledExecutorService.scheduleAtFixedRate(() -> { int size = queue.size(); if (size == 0) { return; } List<AbstractMergeRequestHandle<I, O>.Request> requests = new ArrayList<>(); log.info("合并处理[{}]个请求", size); for (int i = 0; i < size; i++) { //超过最大任务数,等下次执行 if (i < MAX_TASK_NUM) { requests.add(queue.poll()); } } Map<String, O> response = requestProcess.mergeProcess(requests); for (Request request : requests) { request.completableFuture.complete(response.get(request.getRequestId())); } }, 100, 10, TimeUnit.MILLISECONDS); } public O request(I input){ Request request = new Request(); request.setRequestId(UUID.randomUUID().toString().replace("-", "")); CompletableFuture<O> future = new CompletableFuture<>(); request.setCompletableFuture(future); request.setInputDto(input); queue.offer(request); try { return future.get(3, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException e) { log.error("合并获取结果异常 error:", e); } catch (TimeoutException e) { log.error("合并获取结果超时异常 error:", e); } return null; } }
/** * 功能描述: <br> * <p> * 〈合并处理对外接口〉 * </p> * @Author: xuliangliang * @Date: 2022/6/28 22:18 */ public interface BaseMergeRequestProcess<I, O> { /** * 合并处理 * @param requests * @return */ Map<String, O> mergeProcess(List<AbstractMergeRequestHandle<I, O>.Request> requests); }
@Test public void mergeTest() throws InterruptedException { CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(100); // CPU核数 final int processors = Runtime.getRuntime().availableProcessors(); for (int i = 0; i < 100; i++) { new Thread(() -> { COUNT_DOWN_LATCH.countDown(); try { COUNT_DOWN_LATCH.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } for (int j = 1; j <= 100000; j++) { int param = new Random().nextInt(100000); if (param <=0){ param++; } InputDto dto = new InputDto(); dto.setId((long) param); log.info("结果:{}", JSON.toJSONString(handle.request(dto))); } }).start(); } Thread.sleep(30000); }
美梦成真,变为事实。