HttpClientUtil工具类
/**
* 非表单方式提交数据,未指定编码方式
*
* @param url
* @param headers
* @return
* @throws IOException
*/
public static String post(String url, String requestBody, Map headers) throws IOException {
HttpPost post = new HttpPost(url);
if (StringUtils.isNotBlank(requestBody)) {
StringEntity entity = new StringEntity(requestBody, Consts.UTF_8);
entity.setContentType("application/json");
post.setEntity(entity);
}
return execute(post, null, headers, Consts.UTF_8);
}
请求controller代码:
new Thread(() -> ogcCustomerTerminalService.executeTerminalCustomerSync()).start();
请求service代码:
@Scheduled(cron = "0 0 1 * * ?")
public void syncTerminalCustomerData() {
lockTemplate.execute(BizLock.builder()
.bizType("sync")
.bizId("ogc")
.method("terminalCustomer")
.expire(300)
.build()
, () -> {
if (executeTerminalCustomerSync()) {
return null;
}
return null;
});
}
public boolean executeTerminalCustomerSync() {
Integer flag = ogcCustomerMapper.selectCount(new QueryWrapper());
// 不存在数据,直接发送请求
if (flag.equals(BizConstant.ZERO)) {
return syncPostOgcCustomerData();
}else {
// 存在数据,先删除掉,再请求
if (ogcCustomerMapper.delete(new QueryWrapper()) > BizConstant.ZERO) {
return syncPostOgcCustomerData();
}
}
return false;
}
/**
* 发送同步数据请求
* @return
*/
private Boolean syncPostOgcCustomerData() {
try {
String requestBody = "{\"total\":1,\"rows\":500,\"number\":\"\",\"customerName\":\" \"}";
String result = HttpClientUtil.post(terminalCustomerUrl, requestBody, null);
JSONObject jsonObject = JSONObject.parseObject(result);
Integer pages = jsonObject.getInteger("total");
String data = jsonObject.getString("rows");
JSONArray jsonArray = (JSONArray) JSONObject.parse(data);
List list = getOgcCustomerEntities(jsonArray);
if (CollectionUtils.isEmpty(list)) {
return true;
}
List listAdd = new ArrayList<>();
if (CollectionUtils.isNotEmpty(list)) {
listAdd = list.stream().distinct().collect(Collectors.toList());
}
for (int i = BizConstant.TWO; i <= pages; i++) {
requestBody = "{\"total\":\"" + i +"\",\"rows\":500,\"number\":\"\",\"customerName\":\" \"}";
result = HttpClientUtil.post(terminalCustomerUrl, requestBody, null);
jsonObject = JSONObject.parseObject(result);
data = jsonObject.getString("rows");
jsonArray = (JSONArray) JSONObject.parse(data);
list = getOgcCustomerEntities(jsonArray);
if (CollectionUtils.isNotEmpty(list)) {
listAdd = list.stream().distinct().collect(Collectors.toList());
}
}
ogcCustomerMapper.batchInsert(listAdd);
} catch (IOException e) {
e.printStackTrace();
log.info("同步terminalCustomerData失败", e);
}
return false;
}
Mapper代码:
int batchInsert(@Param("list") List list);
insert into schedule_check_value_info (tag_value_id, tag_id,tag_value,unique_code,tag_extra_value,tag_extra_value_en,status,create_time, update_time)
values
(#{item.tagValueId,jdbcType=INTEGER},
#{item.tagId,jdbcType=INTEGER},
#{item.tagValue,jdbcType=VARCHAR},
#{item.uniqueCode,jdbcType=VARCHAR},
#{item.tagExtraValue,jdbcType=VARCHAR},
#{item.tagExtraValue,jdbcType=VARCHAR},
#{item.status,jdbcType=INTEGER},
now(),
now())