Commit 79408aa2 by 毛树良

<fix>:优化

1 parent 28b671db
...@@ -3,7 +3,10 @@ package com.viontech; ...@@ -3,7 +3,10 @@ package com.viontech;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@MapperScan("com.viontech.mapper") @MapperScan("com.viontech.mapper")
@EnableScheduling @EnableScheduling
...@@ -14,4 +17,11 @@ public class FanxingDockingGb1400Application { ...@@ -14,4 +17,11 @@ public class FanxingDockingGb1400Application {
SpringApplication.run(FanxingDockingGb1400Application.class, args); SpringApplication.run(FanxingDockingGb1400Application.class, args);
} }
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);
taskScheduler.setThreadNamePrefix("scheduled-task-");
return taskScheduler;
}
} }
package com.viontech.config; package com.viontech.config;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/** /**
* @author vion * @author vion
**/ **/
...@@ -39,6 +44,48 @@ public class Gb1400RestTemplate { ...@@ -39,6 +44,48 @@ public class Gb1400RestTemplate {
return result; return result;
} }
public String doGet(String userIdentify, String uri, Map params) {
String paramsStr = JSONObject.toJSONString(params);
HttpHeaders httpHeaders = setHeader(userIdentify);
HttpEntity<String> entity = new HttpEntity<>(paramsStr, httpHeaders);
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
String url = "http://" + gb1400Config.getIpPort() + uri;
if (params.containsKey("NotificationID")) {
url = url + "?NotificationID=" + params.get("NotificationID");
}
if (params.containsKey("SubscribeID")) {
if (url.indexOf("?") > 0) {
url = url + "&SubscribeID=" + params.get("SubscribeID");
} else {
url = url + "?SubscribeID=" + params.get("SubscribeID");
}
}
log.info("1400:{} data doGet,params:{}", url, paramsStr);
// ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, params);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, new HashMap<>());
String result = responseEntity.getBody();
log.info("1400:{} data doGet,result:{}", uri, result);
return result;
}
public String doDelete(String userIdentify, String uri, Map params) {
String paramsStr = JSONObject.toJSONString(params);
HttpHeaders httpHeaders = setHeader(userIdentify);
HttpEntity<String> entity = new HttpEntity<>(paramsStr, httpHeaders);
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
String url = "http://" + gb1400Config.getIpPort() + uri;
log.info("1400:{} data doDelete,params:{}", uri, paramsStr);
// restTemplate.delete(url, params);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class, new HashMap<>());
String result = responseEntity.getBody();
log.info("1400:{} data doDelete,result:{}", uri, result);
return result;
}
private HttpHeaders setHeader(String userIdentify) { private HttpHeaders setHeader(String userIdentify) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("User-Identify", userIdentify); headers.add("User-Identify", userIdentify);
......
...@@ -121,14 +121,41 @@ public class Gb1400Controller { ...@@ -121,14 +121,41 @@ public class Gb1400Controller {
} }
/** /**
* 更新订阅
* @return
*/
@RequestMapping(value = "/Subscribes" ,method = RequestMethod.PUT)
public void updateSubscribes(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedReader br = request.getReader();
String str;
StringBuilder params = new StringBuilder();
while((str = br.readLine()) != null){
params.append(str);
}
log.info("收到请求[put.Subscribes.request]:{}", params.toString());
JsonMessage vo = gb1400Service.subscribes(params.toString());
log.info("响应[put.Subscribes.request]:{}", JSONObject.toJSONString(vo));
JSONObject result = null;
response.setContentType("application/VIID+JSON;charset=utf-8");
if (SystemConstants.APP_CODE_SUCCESS == vo.getCode()) {
result = Gb1400ResponseUtil.responseStatusListObject((List<JSONObject>) vo.getData());
} else {
result = Gb1400ResponseUtil.error(1, vo.getMsg(),"/VIID/Subscribes");
}
response.getWriter().write(result.toJSONString());
}
/**
* 删除通知记录 * 删除通知记录
* @param IDList * @param IDList
* @return * @return
*/ */
@DeleteMapping("/SubscribeNotifications") @DeleteMapping("/SubscribeNotifications")
public JSONObject deleteNotificationsRecord(String IDList){ public JsonMessage deleteNotificationsRecord(String IDList){
log.info("收到请求[deleteNotificationsRecord.request]:{}", ""); log.info("收到请求[deleteNotificationsRecord.request]:{}", IDList);
return Gb1400ResponseUtil.success("/VIID/SubscribeNotifications"); JsonMessage vo = gb1400Service.deleteNotifications(IDList);
return vo;
} }
/** /**
...@@ -137,9 +164,10 @@ public class Gb1400Controller { ...@@ -137,9 +164,10 @@ public class Gb1400Controller {
* @return * @return
*/ */
@GetMapping("/SubscribeNotifications") @GetMapping("/SubscribeNotifications")
public JSONObject selectNotificationsRecord(SubscribeNotificationsObj.SubscribeNotificationObject obj){ public JsonMessage selectNotificationsRecord(SubscribeNotificationsObj.SubscribeNotificationObject obj){
log.info("收到请求[selectNotificationsRecord.request]:{}", ""); log.info("收到请求[selectNotificationsRecord.request]:{}", JSONObject.toJSONString(obj));
return Gb1400ResponseUtil.success("/VIID/SubscribeNotifications"); JsonMessage vo = gb1400Service.selectNotifications(obj);
return vo;
} }
......
...@@ -3,6 +3,7 @@ package com.viontech.runner; ...@@ -3,6 +3,7 @@ package com.viontech.runner;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.viontech.config.Gb1400Config; import com.viontech.config.Gb1400Config;
import com.viontech.constant.RedisConstants; import com.viontech.constant.RedisConstants;
import com.viontech.scheduled.Gb1400KeepAlive;
import com.viontech.service.Gb1400Service; import com.viontech.service.Gb1400Service;
import com.viontech.utils.DateUtil; import com.viontech.utils.DateUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -39,6 +40,7 @@ public class Gb1400RegisterRunner implements CommandLineRunner { ...@@ -39,6 +40,7 @@ public class Gb1400RegisterRunner implements CommandLineRunner {
public void run(String... args) { public void run(String... args) {
if("1".equals(gb1400Config.getEnable()) && !StringUtils.isEmpty(gb1400Config.getUserIdentify())){ if("1".equals(gb1400Config.getEnable()) && !StringUtils.isEmpty(gb1400Config.getUserIdentify())){
log.info("准备注册平台"); log.info("准备注册平台");
synchronized (Gb1400KeepAlive.registerLock) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -47,6 +49,7 @@ public class Gb1400RegisterRunner implements CommandLineRunner { ...@@ -47,6 +49,7 @@ public class Gb1400RegisterRunner implements CommandLineRunner {
}).start(); }).start();
} }
} }
}
public static void main(String[] args) { public static void main(String[] args) {
String body = "{\"ResponseStatusObject\":{\"RequestURL\":\"/VIID/System/Keepalive\",\"StatusCode\":0,\"StatusString\":\"保活成功\",\"Id\":\"41011000005038163699\",\"LocalTime\":\"20220422105631\"}}"; String body = "{\"ResponseStatusObject\":{\"RequestURL\":\"/VIID/System/Keepalive\",\"StatusCode\":0,\"StatusString\":\"保活成功\",\"Id\":\"41011000005038163699\",\"LocalTime\":\"20220422105631\"}}";
......
...@@ -25,13 +25,14 @@ public class Gb1400KeepAlive { ...@@ -25,13 +25,14 @@ public class Gb1400KeepAlive {
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Resource @Resource
private Gb1400Service gb1400Service; private Gb1400Service gb1400Service;
private String registerLock = new String("REGISTERLOCK"); public static String registerLock = new String("REGISTERLOCK");
@Scheduled(cron = "10 * * * * ?") @Scheduled(cron = "10 * * * * ?")
public void keepalive() { public void keepalive() {
if("0".equals(gb1400Config.getEnable())){ if("0".equals(gb1400Config.getEnable())){
return; return;
} }
log.info("开始保活,注册状态:{}", Gb1400Constants.LinkStatus);
if (!Gb1400Constants.LinkStatus) { if (!Gb1400Constants.LinkStatus) {
return; return;
} }
...@@ -64,14 +65,16 @@ public class Gb1400KeepAlive { ...@@ -64,14 +65,16 @@ public class Gb1400KeepAlive {
Gb1400Constants.LinkStatus = false; Gb1400Constants.LinkStatus = false;
log.error("keepalive.Exception",e); log.error("keepalive.Exception",e);
} }
log.info("保活结束,注册状态:{}", Gb1400Constants.LinkStatus);
} }
@Scheduled(cron = "0/30 * * * * ? ") @Scheduled(cron = "30 * * * * ?")
public void register() throws Exception{ public void register() throws Exception{
if("0".equals(gb1400Config.getEnable())){ if("0".equals(gb1400Config.getEnable())){
return; return;
} }
synchronized (registerLock) { synchronized (registerLock) {
log.info("当前注册状态:{}", Gb1400Constants.LinkStatus);
if(!Gb1400Constants.LinkStatus) { if(!Gb1400Constants.LinkStatus) {
gb1400Service.register(gb1400Config.getUserIdentify()); gb1400Service.register(gb1400Config.getUserIdentify());
} else { } else {
......
...@@ -9,4 +9,6 @@ public interface Gb1400Service { ...@@ -9,4 +9,6 @@ public interface Gb1400Service {
JsonMessage subscribes(String params); JsonMessage subscribes(String params);
JsonMessage unSubscribes(String subscribeId, String params); JsonMessage unSubscribes(String subscribeId, String params);
JsonMessage subscribeNotifications(SubscribeNotificationsObj subscribeNotificationsObj); JsonMessage subscribeNotifications(SubscribeNotificationsObj subscribeNotificationsObj);
JsonMessage selectNotifications(SubscribeNotificationsObj.SubscribeNotificationObject notificationObject);
JsonMessage deleteNotifications(String ids);
} }
...@@ -27,9 +27,7 @@ import org.springframework.util.DigestUtils; ...@@ -27,9 +27,7 @@ import org.springframework.util.DigestUtils;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Slf4j @Slf4j
...@@ -168,6 +166,30 @@ public class Gb1400ServiceImpl implements Gb1400Service { ...@@ -168,6 +166,30 @@ public class Gb1400ServiceImpl implements Gb1400Service {
return JsonMessageUtil.getErrorJsonMsg(sendResult); return JsonMessageUtil.getErrorJsonMsg(sendResult);
} }
@Override
public JsonMessage selectNotifications(SubscribeNotificationsObj.SubscribeNotificationObject notificationObject) {
Map params = new HashMap();
if (StringUtils.isNotBlank(notificationObject.getNotificationID())) {
params.put("NotificationID", notificationObject.getNotificationID());
}
if (StringUtils.isNotBlank(notificationObject.getSubscribeID())) {
params.put("SubscribeID", notificationObject.getSubscribeID());
}
String sendResult = gb1400RestTemplate.doGet(gb1400Config.getUserIdentify(), Gb1400UriConstants.SUBSCRIBENOTIFICATIONS, params);
return JsonMessageUtil.getSuccessJsonMsg(sendResult);
}
@Override
public JsonMessage deleteNotifications(String ids) {
if (StringUtils.isBlank(ids)) {
return JsonMessageUtil.getErrorJsonMsg("IDList不能为空!");
}
Map params = new HashMap();
params.put("IDList", ids);
String sendResult = gb1400RestTemplate.doDelete(gb1400Config.getUserIdentify(), Gb1400UriConstants.SUBSCRIBENOTIFICATIONS, params);
return JsonMessageUtil.getSuccessJsonMsg(sendResult);
}
private String getRedisAuth(){ private String getRedisAuth(){
Object auth = redisTemplate.opsForHash().get(RedisConstants.GB1400_REGISTER_AUTH, gb1400Config.getIpPort()); Object auth = redisTemplate.opsForHash().get(RedisConstants.GB1400_REGISTER_AUTH, gb1400Config.getIpPort());
return auth == null ? null : String.valueOf(auth); return auth == null ? null : String.valueOf(auth);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!