zoukankan      html  css  js  c++  java
  • 多线程查询的方法

    @Override
    @Scheduled(cron = "0 0 0 * * ?")// 一天请求一次
    public boolean queryAllParkInfo() {
    log.info("宜停车停车场信息查询开始:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

    int pageSize = 500;
    int pages = 15000 / pageSize;
    ExecutorService pool = Executors.newCachedThreadPool();

    //重置数据
    newAccessInfo.setTotalCount(0);
    newAccessInfo.setErrorCount(0);
    newAccessInfo.setRightCount(0);
    newAccessInfo.setRepeatCount(0);
    hmap = new HashMap<>();
    long start = System.currentTimeMillis();
    for (int i = 1; i <= pages; i++) {
    pool.execute(new ParkInfoTask(i, pageSize));
    }
    pool.shutdown();
    while (true) {
    if (pool.isTerminated()) {

    //新增连接次数
    iNewAccessCountService.updateAccessConnectTimes(newAccessInfo.getTotalCount(), 2);
    //增加连接异常信息
    iNewAccessInfoService.insertAccessInfo(newAccessInfo, 2, 2);
    long time = System.currentTimeMillis() - start;
    log.info("[" + new Date() + "]:" + "程序结束了,总耗时:" + time + " ms(毫秒)!");
    break;
    }
    }
    return true;
    }



     class ParkInfoTask implements Runnable {
    private int pageIndex;
    private int pageSize;
    String method = "getparkinfo";

    int count, error, repeat, right;

    public ParkInfoTask(int pageIndex, int pageSize) {
    getSynMessage(pageIndex, pageSize);
    }

    private synchronized void getSynMessage(int pageIndex, int pageSize) {
    this.pageIndex = pageIndex;
    this.pageSize = pageSize;

    this.count = newAccessInfo.getTotalCount();
    this.right = newAccessInfo.getRightCount();
    this.error = newAccessInfo.getErrorCount();
    this.repeat = newAccessInfo.getRepeatCount();
    }

    @Override
    public synchronized void run() {
    try {
    Thread.sleep(1000);

    // 请求参数
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add("appkey", appKey);
    map.add("method", method);
    map.add("security", MD5.MD5Encode(appSecret + method, CharsetUtils.utf));
    map.add("pageIndex", pageIndex);
    map.add("pageSize", pageSize);
    Map<String,Object> tMap = new HashMap<>();
    tMap.put("appkey", appKey);
    tMap.put("method", method);
    tMap.put("security", MD5.MD5Encode(appSecret + method, CharsetUtils.utf));
    tMap.put("pageIndex", pageIndex);
    tMap.put("pageSize", pageSize);
    String sign = SignUtils.signSZ(map, appSecret);
    map.add("sign", sign);
    log.info("请求参数:"+map.toString());
    String response = HttpUtils.findUserData(url, map, String.class);
    log.info("【" + map.get("method") + "】--响应:" + response);
    JSONObject jsonObject = JSONObject.parseObject(response);
    JSONObject data = jsonObject.getJSONObject("data");
    if (data != null && data.size() != 0) {
    JSONObject data1 = data.getJSONObject("data");
    if (data1 != null && data1.size() != 0) {
    JSONArray items = data1.getJSONArray("ParkInfos");
    newAccessInfo.setTotalCount(newAccessInfo.getTotalCount() + items.size());

    try {
    for (int i = 0; i < items.size(); i++) {

    NewSysParkInfo parkinfo = JSONObject.parseObject(JSONObject.toJSONString(items.get(i)), NewSysParkInfo.class);
    // 数据过滤
    if (parkinfo.getParkCode() == null || parkinfo.getCanton() == null || parkinfo.getAddress() == null) {
    newAccessInfo.setErrorCount(newAccessInfo.getErrorCount() + 1);
    continue;
    }
    if (hmap.containsKey(parkinfo.getParkCode())) {
    newAccessInfo.setRepeatCount(newAccessInfo.getRepeatCount() + 1);
    continue;
    } else {
    hmap.put(parkinfo.getParkCode(), 1);
    }

    NewSysParkInfo p1 = getOne(new QueryWrapper<NewSysParkInfo>().eq("park_code",parkinfo.getParkCode()));
    boolean isOk=true;
    if(p1 != null){
    //更新
    parkinfo.setUpdateTime(new Date());
    isOk = update(parkinfo,new QueryWrapper<NewSysParkInfo>().eq("park_code",parkinfo.getParkCode()));
    }else{
    parkinfo.setCreateTime(new Date());
    isOk = save(parkinfo);
    }
    if(!isOk){
    newAccessInfo.setErrorCount(newAccessInfo.getErrorCount() + 1);
    }else{
    newAccessInfo.setRightCount(newAccessInfo.getRightCount() + 1);
    }

    }

    } catch (Exception e) {
    e.printStackTrace();
    newAccessInfo.setErrorCount(newAccessInfo.getErrorCount() + 1);
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    }
  • 相关阅读:
    JVM内存逃逸
    SQL中游标的使用
    配置JAVA环境变量中CLASSPATH变量的作用
    什么是单点登录?单点登录的三种实现方式
    oracle中 connect by prior 递归算法
    test
    mac idea 常见错误记录
    mac 常用操作命令记录
    mac idea 常用快捷键记录
    运行maven install命令时出现错误(BUILD FAILURE)
  • 原文地址:https://www.cnblogs.com/pxzbky/p/14214436.html
Copyright © 2011-2022 走看看