通过xxl-job形式定时获取第三方数据

@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是单线程的,如果同一时刻有两个线程在跑,其中一个线程执行时间较长,会影响另一个线程的执行,而且出现问题不太好排查

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值