Java 多线程 Callable 带有返回值的线程

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdk.SdkConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @Date 2022/2/16
 * @Version 1.0
 */

public class ProductCallable implements Callable<JSONArray> {
    //日志
    protected Logger logger = Logger.getLogger(this.getClass());
    //创建线程池
    ExecutorService executor = Executors.newCachedThreadPool();
    @Override
    public JSONArray call() throws Exception {
        return null;
    }

    public JSONArray productThread(JSONArray array, String accessToken, String proxyUrl) throws ExecutionException, InterruptedException {
        FutureTask futureTask = new FutureTask<>(new Callable() {
            @Override
            public Object call() throws Exception {
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < array.size(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    String id = jsonObject.getString("id");
                    Map<String, String> param = new HashMap<>();
                    param.put("accessToken", accessToken);
                    param.put("action", "/products/" + id);
                    param.put("httpMethod", "get");
                    logger.info("单个商品的参数:" + param);
                    try {
                        String urlProduct = StringHepler.concatUrl(proxyUrl, SdkConstant.WishApi.PROXY_CONTEXT_PATH);
                        urlProduct = StringHepler.concatUrl(urlProduct, SdkConstant.WishApi.API_VERSION_V3);
                        String rep = HttpClientUtil.httpPostRequest(urlProduct, JSON.toJSONString(param));
                        logger.info("单个商品的响应数据:" + rep);
                        if (StringUtils.isEmpty(rep)) {
                            // return BusinessResult.fail("获取单个商品的数据为空");
                            throw new RuntimeException("获取单个商品的数据为空");
                        }
                        //判断响应状态码
                        JSONObject productInfo = JSON.parseObject(rep);
                        if (!"0".equals(productInfo.getString("code"))) {
                            // return BusinessResult.fail(productInfo.getString("message"));
                            throw new RuntimeException(productInfo.getString("message"));
                        }
                        logger.info("单个商品详细信息:" + productInfo);
                        JSONObject productInfoJSONObject = productInfo.getJSONObject("data");
                        jsonArray.add(productInfoJSONObject);
                    } catch (Exception e) {
                        logger.error("获取单个商品数据接口调取失败:" + e.getMessage());
                    }
                }
                return jsonArray;
            }
        });
        //futureTask.run();
        executor.execute(futureTask);
        return (JSONArray) futureTask.get();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值