package com.fzzy.gateway;
|
|
import com.alibaba.fastjson.JSON;
|
import com.fzzy.api.data.ApiParam;
|
import com.fzzy.api.entity.ApiConfs;
|
import com.fzzy.api.service.*;
|
import com.fzzy.api.utils.ContextUtil;
|
import com.fzzy.api.utils.RedisUtil;
|
import com.fzzy.gateway.api.GatewayRemoteManager;
|
import com.fzzy.gateway.data.WeatherWebDto;
|
import com.fzzy.gateway.entity.GatewayConf;
|
import com.fzzy.gateway.service.GatewayConfService;
|
import com.fzzy.gateway.util.GatewayHttpUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.time.DateUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 网关相关的定时器
|
*/
|
@Slf4j
|
@Component(GatewayTimerScheduled.BEAN_ID)
|
public class GatewayTimerScheduled {
|
|
public static String DEFAULT_URL = "https://v0.yiketianqi.com/api?unescape=1&version=v61&appid={appId}&appsecret={appsecret}&cityid={cityid}";
|
public static String DEFAULT_APP_ID = "49421971";
|
public static String DEFAULT_APP_SECRET = "JmJE48Fv";
|
public static String DEFAULT_CITYID = "101270101";//成都
|
|
|
public static final String BEAN_ID = "gateway.timerScheduled";
|
|
@Resource
|
private GatewayConfService confService;
|
|
@Resource
|
private GatewayRemoteManager gatewayRemoteManager;
|
|
|
/**
|
* <p>
|
* 固定时间:每间隔10分钟执行一次
|
*/
|
@Scheduled(cron = "0 0/10 * * * ? ")
|
public void scheduled() {
|
|
//网关的心跳执行
|
doHeartbeat();
|
|
}
|
|
/**
|
* 每间隔30分钟执行一次
|
*/
|
@Scheduled(cron = "0 0/30 * * * ?")
|
public void scheduled30() {
|
|
//定时获取气象信息
|
doWeatherExe();
|
}
|
|
public void doWeatherExe() {
|
|
try {
|
String url = DEFAULT_URL;
|
url = url.replace("{appId}", DEFAULT_APP_ID).replace("{appsecret}", DEFAULT_APP_SECRET).replace("{cityid}", DEFAULT_CITYID);
|
|
String result = GatewayHttpUtil.doGet(url, null);
|
|
if (null == result) {
|
log.error("当前外网获取气象信息失败……");
|
return;
|
}
|
WeatherWebDto dto = JSON.parseObject(result, WeatherWebDto.class);
|
if (StringUtils.isNotEmpty(dto.getErrcode())) {
|
log.error("当前外网获取气象信息异常:{}", dto.getErrmsg());
|
return;
|
}
|
|
WeatherWebDto.contextMap.put("default", dto);
|
|
|
log.info("===========================系统定时获获取气象信息===={}==================",dto);
|
|
} catch (Exception e) {
|
|
}
|
}
|
|
|
/**
|
* 执行网关心跳
|
*/
|
private void doHeartbeat() {
|
|
//获取缓存中的网关信息
|
List<GatewayConf> list = confService.getCacheConfList();
|
if (null == list || list.isEmpty()) {
|
log.warn("------系统为获取到网关配置信息,不执行定时心跳-----");
|
return;
|
}
|
|
for (GatewayConf conf : list) {
|
gatewayRemoteManager.getRemoteService(conf.getPushProtocol()).heartbeat(conf);
|
}
|
}
|
|
}
|