zainali5120
2020-09-25 286ea89f5f6cade73276f865effa5dcd2b19406d
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
package com.xcong.excoin.quartz.job;
 
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.platform.dao.PlatformCnyUsdtExchangeDao;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
 
/**
 * 美元-人民币汇率定时任务
 *
 * @author wzy
 * @date 2020-05-28
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "other-job", havingValue = "true")
public class UsdtCnyExchangePriceUpdateJob {
 
    @Resource
    private PlatformCnyUsdtExchangeDao cnyUsdtExchangeDao;
 
    @Resource
    private RedisUtils redisUtils;
 
    @Scheduled(cron = "0 */5 * * * ? ")
    public void updateUsdtCnyExchange() {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        // 模拟浏览器
        String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
        try {
            URL url = new URL("https://otc-api-hk.eiijo.cn/v1/data/config/purchase-price?coinId=2&currencyId=1&matchType=0");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(30000);
            connection.setConnectTimeout(30000);
            connection.setRequestProperty("User-agent", userAgent);
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
 
            JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
            String code = jsonObject.getString("code");
            if ("200".equals(code)) {
                JSONObject jsonData = (JSONObject) jsonObject.get("data");
                cnyUsdtExchangeDao.updateUsdt(BigDecimal.valueOf(jsonData.getDouble("price")));
                redisUtils.set("platform_cny_usdt_exchange",BigDecimal.valueOf(jsonData.getDouble("price")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}