SpringBoot+线程池实现高频调用http接口并多线程解析json数据

场景

Springboot+FastJson实现解析第三方http接口json数据为实体类(时间格式化转换、字段包含中文):

Springboot+FastJson实现解析第三方http接口json数据为实体类(时间格式化转换、字段包含中文)-CSDN博客

Java中ExecutorService线程池的使用(Runnable和Callable多线程实现):

Java中ExecutorService线程池的使用(Runnable和Callable多线程实现)_executorservice executorservice = executors.newfix-CSDN博客

Java中创建线程的方式以及线程池创建的方式、推荐使用ThreadPoolExecutor以及示例:

Java中创建线程的方式以及线程池创建的方式、推荐使用ThreadPoolExecutor以及示例_threadpoolexecutor创建线程-CSDN博客

在上面的基础上,需要使用定时任务高频调用典第三方http接口并解析返回的json数据为java的list,需要对

list的每个数据进行处理,这里需要用到自定义线程池对每个java对象分别进行处理。

注:

博客:
霸道流氓气质_C#,架构之路,SpringBoot-CSDN博客

实现

1、首先在配置文件中添加第三方接口的url

这里是yml文件

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

​

2、新建定时任务类

使用@PostConstruct注解初始化需要的数据,比如获取配置文件中的接口的url以及初始化线程池

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

这里读取配置文件需要引入

    @Resource
    private Environment environment;

然后新建定时任务调用接口,并解析接口返回的json数据,将响应的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) {

        }
    }

​

定时任务类完整实例代码

​
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、上面具体进行业务处理的类

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 userDTOS = data.toJavaList(UserDTO.class);
        for (UserDTO userDTO:userDTOS) {
            newFixedThreadPool.execute(() -> mapperApiData(userDTO));
        }
    }

    //具体业务处理
    private void mapperApiData(UserDTO userDTO){
        System.out.println(userDTO);
    }

}

将接收到的data字段解析成java的list,然后遍历list,每个对象用一个线程具体去处理。

附接口示例数据

{
    "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、运行效果

SpringBoot+线程池实现高频调用http接口并多线程解析json数据_第1张图片

你可能感兴趣的:(架构之路,spring,boot,http,json)