In Java, based on thread pool implementation, a certain number of API requests are sent per second. The ScheduledExecutorService can be used to schedule tasks, and the ThreadPoolExecutor is used to process concurrent requests. The number of requests per second, execution time, and thread pool size can be adjusted according to actual needs.
Implementation ideas
1. Create a thread pool
- use
()
To create a scheduling thread pool - And use
()
To create a thread pool for sending API requests
2. Scheduling tasks
- use
ScheduledExecutorService
to schedule tasks at a fixed rate. - By controlling the frequency of tasks, you can ensure that a specified number of requests are sent per second.
3. Define API request tasks
- Define an implementation
Runnable
Interface class - Responsible for executing specific API requests
4. Control the request rate
- Use the scheduler to submit a specified number of tasks per second to execute in the thread pool.
Introduce dependencies
<!-- Apache HttpClient --> <dependency> <groupId>.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.2</version> </dependency>
Implement code
import .; import .; import .; import .; import .; import .; import .; import ; import .*; import ; public class ApiRequestScheduler { // Define thread pool for concurrent sending API requests private final ExecutorService requestExecutor; // Define a scheduling thread pool for scheduled request tasks private final ScheduledExecutorService scheduler; // Record the number of requests sent private final AtomicInteger requestCounter; // Number of requests sent per second private final int requestsPerSecond; // Apache HttpClient instance private final CloseableHttpClient httpClient; // The target URL of the API request private final String apiUrl; // Constructor, initialize thread pool and scheduler public ApiRequestScheduler(int requestsPerSecond, String apiUrl) { = requestsPerSecond; = (requestsPerSecond); = (1); = new AtomicInteger(0); = (); = apiUrl; } // Start scheduling API request tasks public void start() { // Schedule tasks per second and execute them according to the number of requests sent per second (() -> { for (int i = 0; i < requestsPerSecond; i++) { (this::sendApiRequest); } }, 0, 1, ); } // Stop scheduling and close thread pool and HttpClient public void stop() { (); (); try { if (!(1, )) { (); } if (!(1, )) { (); } (); } catch (InterruptedException | IOException e) { (); (); try { (); } catch (IOException ioException) { (); } } } // Send API request using Apache HttpClient private void sendApiRequest() { int requestId = (); HttpUriRequestBase request = new HttpGet(apiUrl); ("Sending API request #" + requestId); try (CloseableHttpResponse response = (request)) { String responseBody = (()); ("Request #" + requestId + " completed with status: " + () + ", response: " + responseBody); } catch (IOException | ParseException e) { ("Request #" + requestId + " failed: " + ()); } } public static void main(String[] args) { // Send 5 API requests per second, the target URL is /api ApiRequestScheduler scheduler = new ApiRequestScheduler(5, "/api"); // Start the scheduler (); // Stop the scheduler after running for 10 seconds try { (10000); } catch (InterruptedException e) { ().interrupt(); } // Stop the scheduler (); } }
Realize the effect
Send a specified number of API requests per second, use Apache HttpClient to handle HTTP communications, and ensure that resources are properly managed in a multi-threaded environment.
The number of requests per second, API URL, and thread pool size can be adjusted according to actual needs.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.