标签:control while 查询 批量 异常 long 订单 erro array
package com.sf.XWFS.task;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sf.XWFS.domain.OrderDO;
import com.sf.XWFS.domain.YHOrderDO;
import com.sf.XWFS.mapper.YHOrderMapper;
import com.sf.XWFS.service.OrderService;
import com.sf.XWFS.service.YHOrderService;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
/**
* Scheduled请求远航订单定时任务
* @author puxiaozhe
* @date 2019-05-07
*/
@Component
public class SchedulerTask {
private Logger logger = LoggerFactory.getLogger(SchedulerTask.class);
private OrderService orderService;
private YHOrderService yhOrderService;
/**
* 从远航数据库获取数据
*
*/
@Scheduled(fixedRate = 60000)
public void refreshOrder(){
logger.info("start -----> 开始刷新从远航获取订单数据 ");
orderService = (OrderService)ApplicationContextUtil.getBean("orderServiceImpl");
yhOrderService = (YHOrderService)ApplicationContextUtil.getBean("YHOrderServiceImpl");
//记录系统开始时间
long start = System.currentTimeMillis();
//条数
int pageSize = 50;
//偏移量
int pageStart = 0;
//最多查询次数
int maxTimes = 50;
//初始查询时间
int queryTimes = 0;
boolean isStop = false;
HashMap<String,Object> param = new HashMap<String,Object>();
try {
//查询50次,一共2500条
param.put("pageSize", pageSize);
//排序
param.put("orderBy", "id asc");
while( queryTimes ++ <= maxTimes){
param.put("pageStart", pageStart);
try{
List<Map<String,Object>> List = new ArrayList<Map<String,Object>>();
Map<String ,Object> map = new HashedMap();
List.add(map);
List<Map<String, Object>> YHOrderList =yhOrderService.queryYHOrder(List,param);
if(Collections.isEmpty(YHOrderList)){
logger.info("第"+queryTimes+"次查询到数据为空");
break;
}
logger.info("第"+queryTimes+"次查询到记录:"+YHOrderList.size());
if(YHOrderList.size()<50){
isStop = true;
}
//保存远航订单数据到本地
save(YHOrderList);
}catch(Exception ex){
isStop = true;
logger.error("", ex);
}
//已经没有需要更新的订单数据
if(isStop){
break;
}
//翻页
pageStart +=pageSize;
}
} catch (Exception ex) {
logger.error("从远航获取订单数据异常", ex);
} finally {
logger.info("end -----> 从远航获取订单数据结束,耗时:" + (System.currentTimeMillis() - start) + "ms");
}
}
/**
* 网络连接超时验证
*/
// @Scheduled(fixedRate = 60000)
// public void netWorkConnection(){
//
// logger.info("start -----> 开始检测连接服务器的状态 ");
//
// orderService = (OrderService)ApplicationContextUtil.getBean("orderServiceImpl");
// yhOrderService = (YHOrderService)ApplicationContextUtil.getBean("YHOrderServiceImpl");
//
// //记录系统开始时间
// long start = System.currentTimeMillis();
// //记录sql执行时间
// long executeTimes = 0;
// //中断的标记
// boolean isStop = false;
// try {
// while( executeTimes ++ <= 60000){
// try{
// List<Map<String,Object>> List = new ArrayList<Map<String,Object>>();
// Map<String ,Object> map = new HashedMap();
// List.add(map);
// List<Map<String, Object>> YHOrderList =yhOrderService.queryYHOrder(List,param);
// if(Collections.isEmpty(YHOrderList)){
//// logger.info("第"+queryTimes+"次查询到数据为空");
// break;
// }
//// logger.info("第"+queryTimes+"次查询到记录:"+YHOrderList.size());
// if(YHOrderList.size()<50){
// isStop = true;
// }
// }catch(Exception ex){
// isStop = true;
// logger.error("", ex);
// }
// //网络中断···
// if(isStop){
// break;
// }
// }
// } catch (Exception ex) {
// logger.error("网络连接出现异常", ex);
// } finally {
// logger.info("end -----> 检测网络连接总共耗时:" + (System.currentTimeMillis() - start) + "ms");
// }
// }
/**
* 更新到本地数据库
* @param YHOrderList
*/
// private void updateToDateBase(List<Map<String, Object>> YHOrderList){
//
// try{
// if(YHOrderList == null ||YHOrderList.size()==0){
// return;
// }
// Map<String, List<OrderDO>> map = new HashMap<String, List<OrderDO>>() ;
// List<OrderDO> list = new ArrayList<OrderDO>();
//
// YHOrderList.forEach(jsonObj ->{
// JSONObject json = (JSONObject)jsonObj;
// if(json.get("error") != null){
// return;
// }
//
// String id = json.getString("id");
// String khxm= json.getString("khxm");
// String dh = json.getString("dh");
// String shdz = json.getString("shdz");
//
// OrderDO order = new OrderDO();
// order.setOrderid(id);
// order.setdContact(khxm);
// order.setdTel(dh);
// order.setdAddress(shdz);
// list.add(order);
// });
// map.put("list",list);
// //保存到本地
// Integer rest = yhOrderService.saveYHOrder(list);
// logger.info("成功保存远航订单数据到数据库:"+rest);
// }catch(Exception ex){
// logger.error("保存远航订单数据异常",ex );
// }
// }
/**
* 保存到本地数据库
* @param orderList
*/
public void save(List<Map<String, Object>> orderList){
// 批量保存远航的订单数据
yhOrderService.batchSaveYHOrder(orderList);
}
}
标签:control while 查询 批量 异常 long 订单 erro array
原文地址:https://www.cnblogs.com/pxzbky/p/10995003.html