package com.luxsan.lpms.job; import com.alibaba.fastjson2.JSONObject; import com.luxsan.lpms.mapper.PUpphOutPutDetailMapper; import com.ruoyi.framework.web.domain.AjaxResult; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.extern.log4j.Log4j2; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; /** * 使用若依的定时任务与XXLJob做的同步接口,使用restTemplate发送的请求 */ @RestController @RequestMapping("/sync") @Log4j2 @Component("syncJob") //使用若依 syncJob.getMapData public class SYNCOverAllCapacityJob { public static final String url = "http://IP:8081/项目路径"; public static final String emailUrl = "https://域名/项目路径"; // public static final String url1= "http://IP:8081/项目路径"; // public static final String url2= "https://ifasx.luxsan-ict.com/api/WorkWeChat/SendTextMessage"; @Resource private RestTemplate restTemplate; @Resource PUpphOutPutDetailMapper pUpphOutPutDetailMapper; @RequestMapping("/getData") @Transactional(rollbackFor = Exception.class) @XxlJob("mapData") //使用XXljob 就这一个就可以 JobHandler* public AjaxResult getMapData() { //若依 定时任务 List<Map<String, String>> list = pUpphOutPutDetailMapper.selectParm(); List<Map<String, Object>> resultList = new ArrayList<>(); String startTime = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));//获取当前时间 // String startTime = "2023/9/3";//获取当前时间 String date = getDate(startTime, -1);//前一天的时间 for (Map<String, String> dataMap : list) { HashMap<String, String> requestMap = new HashMap<>(); HttpHeaders headers = new HttpHeaders(); headers.add("NginxModel", dataMap.get("MODEL")); headers.add("NginxStage", dataMap.get("STAGE_GROUP")); headers.add("NginxProcess", dataMap.get("FATP")); headers.add("sign", "e74167063074b180b6dafa567b2095a8f7996327"); requestMap.put("shift", "D"); requestMap.put("model", dataMap.get("MODEL")); requestMap.put("stage", dataMap.get("STAGE_GROUP")); requestMap.put("reflow", "Y"); requestMap.put("startTime", startTime); //调用接口发送请求 List<Map<String, String>> dataList = exchange(headers, requestMap); if (null != dataList && !dataList.isEmpty()) { for (Map<String, String> data : dataList) { HashMap<String, Object> resultMap = getResultMap(data, dataMap); resultMap.put("shift", "D"); resultMap.put("startTime", startTime); resultList.add(resultMap); } } requestMap.put("shift", "N"); requestMap.put("startTime", date); //调用接口发送请求 List<Map<String, String>> dataNList = exchange(headers, requestMap); if (null != dataNList && !dataNList.isEmpty()) { for (Map<String, String> data : dataNList) { HashMap<String, Object> resultMap = getResultMap(data, dataMap); resultMap.put("shift", "N"); resultMap.put("startTime", date); resultList.add(resultMap); } } } log.info("resultList=====" + resultList.size() + "条"); if (null != resultList && resultList.size() > 0) { pUpphOutPutDetailMapper.deleteByDate(startTime); pUpphOutPutDetailMapper.deleteByDateN(date); int count = pUpphOutPutDetailMapper.insertResultList(resultList); log.info("新增" + count + "条"); return AjaxResult.success("新增" + count + "条"); }else { try { // JSONObject jsonObject = new JSONObject(); // jsonObject.put("EmpCodes", "75333337"); // jsonObject.put("Content", "云管同步定时任务新增0条,确认下是不是任务失败了"); // ResponseEntity<Map> forEntity = restTemplate.postForEntity(url2, jsonObject, Map.class); // log.info("新增0条"); JSONObject jsonObject = new JSONObject(); jsonObject.put("mailFrom", "luxsan.imes@luxsan-ict.com"); jsonObject.put("mailTo", "LEE.LIN@luxsan-ict.com"); jsonObject.put("subject", "云管数据同步定时任务预警"); jsonObject.put("content", "云管同步定时任务新增0条,确认下是不是任务失败了"); ResponseEntity<Map> forEntity = restTemplate.postForEntity(emailUrl, jsonObject, Map.class); log.info("新增0条"); }catch (Exception e) { return AjaxResult.error("调用邮箱接口失败"); } return AjaxResult.success("新增0条"); } } private HashMap<String, Object> getResultMap(Map<String, String> data, Map<String, String> dataMap) { HashMap<String, Object> resultMap = new HashMap<>(); resultMap.put("model", dataMap.get("MODEL")); resultMap.put("outPut", data.get("inputQty")); resultMap.put("stage", data.get("stage")); resultMap.put("line", data.get("line")); resultMap.put("lineWorkTime", data.get("lineWorkTime")); resultMap.put("stageGroup", data.get("SECTION")); return resultMap; } private List<Map<String, String>> exchange(HttpHeaders headers, HashMap<String, String> requestMap) { HttpEntity httpEntity = new HttpEntity<>(requestMap, headers); ResponseEntity<JSONObject> exchange = this.restTemplate.exchange(url, HttpMethod.POST, httpEntity, JSONObject.class); JSONObject body = exchange.getBody(); List<Map<String, String>> dataList = (List<Map<String, String>>) body.get("data"); return dataList; } /** * 日期+n天,遇到月末或年末自动转下月或下年 * * @param date * @param i * @return */ public String getDate(String date, int i) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); //字符串转换成Date Date d = null; try { d = sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } Calendar c = Calendar.getInstance(); //设置Calendar日期 c.setTime(d); //给指定Calendar日期加上指定天数 c.add(Calendar.DATE, i); //得到日期转成字符串形式返回 String s = sdf.format(c.getTime()); return s; } }
RestTemplate与若依定时任务,XXLJob定时任务的结合
最新推荐文章于 2024-08-17 19:38:58 发布
该代码实现了一个基于XXLJob的定时任务,该任务通过Spring的RESTTemplate调用其他服务的REST接口进行数据同步。任务中涉及了日期处理、HTTP请求头设置以及结果的处理和存储。同时,当没有新增数据时,会触发邮件通知。
摘要由CSDN通过智能技术生成