Using SpringBoot and Thread Pools for High‑Frequency HTTP Calls with Multithreaded JSON Parsing
This tutorial demonstrates how to configure a SpringBoot application to repeatedly call a third‑party HTTP API, parse the returned JSON with FastJSON, and process each data item concurrently using a fixed thread pool initialized in a scheduled task.
First, add the target API URL to the application.yml configuration file:
test:
# Test multithreaded HTTP request and data parsing
http-request-executor:
url: http://127.0.0.1:4523/m1/2858210-0-default/testFastJsonNext, create a scheduled task class. Use @PostConstruct to read the URL from the environment and to create a fixed thread pool whose size is half of the available CPU cores.
@Component("HttpRequestExecutorTestTask")
@EnableScheduling
public class HttpRequestExecutorTestTask {
private String url;
@Resource
private Environment environment;
@Resource
private HttpRequestExecutorTestHandler handler;
@PostConstruct
public void initData() {
HttpRequestExecutorTestHandler.newFixedThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() / 2);
url = Objects.requireNonNull(environment.getProperty("test.http-request-executor.url"));
}
@Scheduled(fixedRateString = "1000")
public void taskGetData() {
String body = "";
try {
body = HttpRequest.get(url)
.timeout(20000)
.execute()
.body();
UserResDTO userResDTO = JSON.parseObject(body, UserResDTO.class);
if (userResDTO.getCode() != null && 200 != userResDTO.getCode()) {
// error handling
} else {
JSONArray data = userResDTO.getData();
if (StringUtils.isEmpty(data)) {
return;
}
handler.handleData(data);
}
} catch (Exception e) {
// exception handling
}
}
}The handler class receives the data array, converts it to a List<UserDTO>, and submits each element to the thread pool for parallel processing.
@Component
public class HttpRequestExecutorTestHandler {
public static ExecutorService newFixedThreadPool;
public void handleData(JSONArray data) {
List<UserDTO> userDTOS = data.toJavaList(UserDTO.class);
for (UserDTO userDTO : userDTOS) {
newFixedThreadPool.execute(() -> mapperApiData(userDTO));
}
}
private void mapperApiData(UserDTO userDTO) {
System.out.println(userDTO);
}
}The sample JSON returned by the API looks like:
{
"code": "200",
"data": [
{"id": "63", "name": "学指约思但", "time_cur": "2009-07-23 02:14:52", "地址": "minim sint commodo nisi"},
{"id": "19", "name": "下农前清时相", "time_cur": "2013-10-16 17:32:09", "地址": "ullamco aliqua"},
{"id": "57", "name": "米见放层张圆", "time_cur": "2015-10-20 18:40:42", "地址": "dolor minim et qui"}
]
}When the application runs, the scheduled method executes every second, fetches the JSON, parses it, and processes each record concurrently, as illustrated by the screenshot of the console output.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
