Gb1400KeepAlive.java
2.57 KB
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
package com.viontech.scheduled;
import com.viontech.config.Gb1400Config;
import com.viontech.constant.Gb1400Constants;
import com.viontech.service.Gb1400Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@Slf4j
@Component
public class Gb1400KeepAlive {
@Autowired
private Gb1400Config gb1400Config;
@Autowired
private RestTemplate restTemplate;
@Resource
private Gb1400Service gb1400Service;
private String registerLock = new String("REGISTERLOCK");
@Scheduled(cron = "10 * * * * ?")
public void keepalive() {
if("0".equals(gb1400Config.getEnable())){
return;
}
if (!Gb1400Constants.LinkStatus) {
return;
}
//保活
String lkh = gb1400Config.getUserIdentify();
if(StringUtils.isBlank(lkh)){
return;
}
try{
HttpHeaders headers = new HttpHeaders();
headers.add("User-Identify",lkh);
headers.add("Content-Type","application/VIID+JSON;charset=utf-8");
// String currentTime = DateUtil.getCurrentTime(DateUtil.TIMESTAMP_NO_DELIMITER);
// headers.add("User-SendTime", currentTime);
// String content = "{\"DeviceID\":\""+lkh+"\"}";
String content = "{\"KeepaliveObject\":{\"DeviceID\":\""+lkh+"\"}}";
HttpEntity<String> entity = new HttpEntity<>(content, headers);
String url = "http://"+gb1400Config.getIpPort()+"/VIID/System/Keepalive";
ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, entity, String.class);
String body = postForEntity.getBody();
log.info("keepalive.result:{}", body);
}catch (Exception e){
log.error("keepalive.Exception",e);
}
}
@Scheduled(cron = "0/30 * * * * ? ")
public void register() throws Exception{
if("0".equals(gb1400Config.getEnable())){
return;
}
synchronized (registerLock) {
if(!Gb1400Constants.LinkStatus) {
gb1400Service.register(gb1400Config.getUserIdentify());
} else {
return;
}
}
}
}