SpringBoot+thread pool implements high-frequency calling of http interface and multi-thread parsing json data

Scenes

Springboot+FastJson implements parsing third-party http interface json data into entity classes (time format conversion, fields containing Chinese):

Springboot+FastJson implements parsing third-party http interface json data into entity classes (time format conversion, fields containing Chinese) - CSDN Blog

The use of ExecutorService thread pool in Java (Runnable and Callable multi-thread implementation):

The use of ExecutorService thread pool in Java (Runnable and Callable multi-thread implementation)_executorservice executorservice = executors.newfix-CSDN Blog

How to create threads and thread pools in Java, recommended use of ThreadPoolExecutor and examples:

How to create threads in Java and how to create thread pools, recommended use of ThreadPoolExecutor and examples_threadpoolexecutor to create threads - CSDN Blog

On the basis of the above, you need to use scheduled tasks to frequently call the third-party http interface and parse the returned json data into a java list. You need to

To process each data in the list, a custom thread pool is needed to process each Java object separately.

Note:

Blog:
Domineering hooligan temperament_C#, architecture road, SpringBoot-CSDN blog

accomplish

1. First add the URL of the third-party interface in the configuration file

Here is the yml file

​
test:
  #测试多线程请求http接口并解析数据
  http-request-executor:
    url: http://127.0.0.1:4523/m1/2858210-0-default/testFastJson

​

2. Create a new scheduled task class

Use the @PostConstruct annotation to initialize the data required, such as obtaining the url of the interface in the configuration file and initializing the thread pool.

    @PostConstruct
    public void initData() {
        HttpRequestExecutorTestHandler.newFixedThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() / 2);
        url = Objects.requireNonNull(environment.getProperty("test.http-request-executor.url"));
    }

Reading the configuration file here needs to be introduced

    @Resource
    private Environment environment;

Then create a new scheduled task calling interface, parse the json data returned by the interface, and pass the response data field to

Classes that specifically handle data

​
    @Scheduled(fixedRateString = "1000")
    public void taskGetData() {
        String body = "";
        try {
            body = HttpRequest
                    .get("http://127.0.0.1:4523/m1/2858210-0-default/testFastJson")
                    .timeout(20000)
                    .execute()
                    .body();
            UserResDTO userResDTO = JSON.parseObject(body, UserResDTO.class);
            if (userResDTO.getCode() != null && 200!=userResDTO.getCode()) {
                //错误处理
            }else {
                JSONArray data = userResDTO.getData();
                if (StringUtils.isEmpty(data)) {
                    return;
                }
                handler.handleData(data);
            }
        } catch (Exception e) {

        }
    }

​

Complete example code of scheduled task class

​
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.test.dto.UserResDTO;
import com.ruoyi.web.handle.HttpRequestExecutorTestHandler;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.Executors;

@Component("HttpRequestExecutorTestTask")
@EnableScheduling
public class HttpRequestExecutorTestTask {

    private String url;

    @Resource
    private Environment environment;

    @Resource
    private HttpRequestExecutorTestHandler handler;

    /**
     * 初始化URL数据
     */
    @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("http://127.0.0.1:4523/m1/2858210-0-default/testFastJson")
                    .timeout(20000)
                    .execute()
                    .body();
            UserResDTO userResDTO = JSON.parseObject(body, UserResDTO.class);
            if (userResDTO.getCode() != null && 200!=userResDTO.getCode()) {
                //错误处理
            }else {
                JSONArray data = userResDTO.getData();
                if (StringUtils.isEmpty(data)) {
                    return;
                }
                handler.handleData(data);
            }
        } catch (Exception e) {

        }
    }
}

​

3. The above categories for specific business processing

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.system.domain.test.dto.UserDTO;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.ExecutorService;

@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);
    }

}

Parse the received data field into a Java list, then traverse the list, and use a thread to process each object specifically.

Attached interface sample data

{
    "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"
        }
    ]
}

4. Operation effect

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/134874503