Java線程池如何實現(xiàn)精準控制每秒API請求
Java中基于線程池實現(xiàn)指定每秒發(fā)送一定數(shù)量的API請求,可以使用ScheduledExecutorService來調度任務,同時使用ThreadPoolExecutor來處理并發(fā)請求,可以根據(jù)實際需求調整每秒請求數(shù)量、執(zhí)行時間、以及線程池大小。
實現(xiàn)思路
1.創(chuàng)建線程池
- 使用
Executors.newScheduledThreadPool()來創(chuàng)建一個調度線程池 - 并使用
Executors.newFixedThreadPool()來創(chuàng)建一個用于發(fā)送API請求的線程池
2.調度任務
- 使用
ScheduledExecutorService來按固定速率調度任務。 - 通過控制任務的頻率,可以確保每秒發(fā)送指定數(shù)量的請求。
3.定義API請求任務
- 定義一個實現(xiàn)
Runnable接口的類 - 負責執(zhí)行具體的API請求
4.控制請求速率
- 使用調度器每秒提交指定數(shù)量的任務到線程池中執(zhí)行。
引入依賴
<!-- Apache HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2</version>
</dependency>實現(xiàn)代碼
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import java.io.IOException;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ApiRequestScheduler {
// 定義線程池,用于并發(fā)發(fā)送API請求
private final ExecutorService requestExecutor;
// 定義調度線程池,用于定時調度請求任務
private final ScheduledExecutorService scheduler;
// 記錄已發(fā)送的請求數(shù)量
private final AtomicInteger requestCounter;
// 每秒發(fā)送的請求數(shù)量
private final int requestsPerSecond;
// Apache HttpClient 實例
private final CloseableHttpClient httpClient;
// API 請求的目標URL
private final String apiUrl;
// 構造函數(shù),初始化線程池和調度器
public ApiRequestScheduler(int requestsPerSecond, String apiUrl) {
this.requestsPerSecond = requestsPerSecond;
this.requestExecutor = Executors.newFixedThreadPool(requestsPerSecond);
this.scheduler = Executors.newScheduledThreadPool(1);
this.requestCounter = new AtomicInteger(0);
this.httpClient = HttpClients.createDefault();
this.apiUrl = apiUrl;
}
// 開始調度API請求任務
public void start() {
// 每秒調度任務,按照每秒發(fā)送的請求數(shù)量來執(zhí)行
scheduler.scheduleAtFixedRate(() -> {
for (int i = 0; i < requestsPerSecond; i++) {
requestExecutor.submit(this::sendApiRequest);
}
}, 0, 1, TimeUnit.SECONDS);
}
// 停止調度和關閉線程池及HttpClient
public void stop() {
scheduler.shutdown();
requestExecutor.shutdown();
try {
if (!scheduler.awaitTermination(1, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
if (!requestExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
requestExecutor.shutdownNow();
}
httpClient.close();
} catch (InterruptedException | IOException e) {
scheduler.shutdownNow();
requestExecutor.shutdownNow();
try {
httpClient.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
// 使用Apache HttpClient發(fā)送API請求
private void sendApiRequest() {
int requestId = requestCounter.incrementAndGet();
HttpUriRequestBase request = new HttpGet(apiUrl);
System.out.println("Sending API request #" + requestId);
try (CloseableHttpResponse response = httpClient.execute(request)) {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Request #" + requestId + " completed with status: " + response.getCode() +
", response: " + responseBody);
} catch (IOException | ParseException e) {
System.err.println("Request #" + requestId + " failed: " + e.getMessage());
}
}
public static void main(String[] args) {
// 每秒發(fā)送5個API請求,目標URL為http://example.com/api
ApiRequestScheduler scheduler = new ApiRequestScheduler(5, "http://www.dzy.com/api");
// 啟動調度器
scheduler.start();
// 運行10秒后停止調度器
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 停止調度器
scheduler.stop();
}
}實現(xiàn)效果
每秒發(fā)送指定數(shù)量的API請求,使用Apache HttpClient處理HTTP通信,并確保在多線程環(huán)境中正確管理資源。
可以根據(jù)實際需求調整每秒請求數(shù)量、API URL、以及線程池大小。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis-plus批量去重插入ON DUPLICATE key update使用方式
這篇文章主要介紹了Mybatis-plus批量去重插入ON DUPLICATE key update使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
詳談ThreadLocal-單例模式下高并發(fā)線程安全
這篇文章主要介紹了ThreadLocal-單例模式下高并發(fā)線程安全,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Java多線程編程中synchronized關鍵字的基礎用法講解
Java的synchronized關鍵字用于修飾線程同步,用以線程資源共享的目的等,下面就帶來簡單的Java多線程編程中synchronized關鍵字的基礎用法講解2016-06-06
MyBatis學習教程(二)—如何使用MyBatis對users表執(zhí)行CRUD操作
這篇文章主要介紹了MyBatis學習教程(二)—如何使用MyBatis對users表執(zhí)行CRUD操作的相關資料,需要的朋友可以參考下2016-05-05
httpclient getPoolEntryBlocking連接池方法源碼解讀
這篇文章主要為大家介紹了httpclient getPoolEntryBlocking連接池方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

