vince
2023-11-17 f91db6022539e797c958c1fac16dc5e53db4f892
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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);
 
 
            log.debug("------气象URL---{}",url);
            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);
        }
    }
 
}