@Service
public class ZrRequestService {
private static final Logger logger = LoggerFactory.getLogger(ZrRequestService.class);
@Autowired
private ZrResquestTemplate zrResquestTemplate;
public ZrDustMonitorSecondResp getDustMonitoring(String sn){
ZrDustMonitorSecondResp respList = new ZrDustMonitorSecondResp();
Map<String,Object> reqMap = new HashMap<>();
reqMap.put("SN",sn);
ZrDustMonitorSecondResp.InfoResponse infoResponse = zrResquestTemplate.postJson(reqMap, ZrDustMonitorSecondResp.InfoResponse.class);
XxlJobLogger.log("infoResponse值为:"+String.valueOf(infoResponse));
if(null == infoResponse){
return null;
}
if ("1001".equals(infoResponse.getCode())) {
respList = infoResponse.getContent();
}
return respList;
}
}
@Component
public class ZrResquestTemplate {
@Value("${zr.serverUrl}")
private String url;
/**
* post请求
* @param tClass
* @param <T>
* @return
*/
public <T> T postJson(Map<String,Object> param, Class<T> tClass) {
HttpHeaders headers = new HttpHeaders();
if(null == url){
throw new RuntimeException("url为空");
}
String newUrl = new String(url);
if (param != null && !param.isEmpty()) {
newUrl += "?";
for (Map.Entry<String,Object> entry:param.entrySet()) {
if (!newUrl.endsWith("?")){
newUrl += "&";
}
newUrl += (entry.getKey()+"="+entry.getValue());
}
}
HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(headers);
return new RestTemplate().postForObject(newUrl,entity,tClass);
}
}
@Component
@JobHandler(value = "dustMonitoringDataJobHandler")
public class DustMonitoringDataHandler extends IJobHandler {
@Autowired
private IDustMonitoringService dustMonitoringService;
@Override
public ReturnT<String> execute(String s) throws Exception {
System.out.println("dustMonitoringDataJobHandler-begin:" + new Date());
dustMonitoringService.synDustMonitoringData();
System.out.println("dustMonitoringDataJobHandler-end:" + new Date());
return ReturnT.SUCCESS;
}
}
然后在xxl-job平台进行配置,也可以使用@Scheduled(cron = “${bes.access.record.scheduled}”)注解,
cron表达式可以配置在nocas里,使用注解的话,不需要再编写IJobHandler类,只需要在需要定时执行的类上加注解就可以。
确定是,@Scheduled是单线程的,如果同一时刻有两个线程在跑,其中一个线程执行时间较长,会影响另一个线程的执行,而且出现问题不太好排查