Commit 79408aa2 by 毛树良

<fix>:优化

1 parent 28b671db
......@@ -3,7 +3,10 @@ package com.viontech;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
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.concurrent.ThreadPoolTaskScheduler;
@MapperScan("com.viontech.mapper")
@EnableScheduling
......@@ -14,4 +17,11 @@ public class FanxingDockingGb1400Application {
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;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* @author vion
**/
......@@ -39,6 +44,48 @@ public class Gb1400RestTemplate {
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) {
HttpHeaders headers = new HttpHeaders();
headers.add("User-Identify", userIdentify);
......
......@@ -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
* @return
*/
@DeleteMapping("/SubscribeNotifications")
public JSONObject deleteNotificationsRecord(String IDList){
log.info("收到请求[deleteNotificationsRecord.request]:{}", "");
return Gb1400ResponseUtil.success("/VIID/SubscribeNotifications");
public JsonMessage deleteNotificationsRecord(String IDList){
log.info("收到请求[deleteNotificationsRecord.request]:{}", IDList);
JsonMessage vo = gb1400Service.deleteNotifications(IDList);
return vo;
}
/**
......@@ -137,9 +164,10 @@ public class Gb1400Controller {
* @return
*/
@GetMapping("/SubscribeNotifications")
public JSONObject selectNotificationsRecord(SubscribeNotificationsObj.SubscribeNotificationObject obj){
log.info("收到请求[selectNotificationsRecord.request]:{}", "");
return Gb1400ResponseUtil.success("/VIID/SubscribeNotifications");
public JsonMessage selectNotificationsRecord(SubscribeNotificationsObj.SubscribeNotificationObject obj){
log.info("收到请求[selectNotificationsRecord.request]:{}", JSONObject.toJSONString(obj));
JsonMessage vo = gb1400Service.selectNotifications(obj);
return vo;
}
......
......@@ -3,6 +3,7 @@ package com.viontech.runner;
import com.alibaba.fastjson.JSONObject;
import com.viontech.config.Gb1400Config;
import com.viontech.constant.RedisConstants;
import com.viontech.scheduled.Gb1400KeepAlive;
import com.viontech.service.Gb1400Service;
import com.viontech.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
......@@ -39,12 +40,14 @@ public class Gb1400RegisterRunner implements CommandLineRunner {
public void run(String... args) {
if("1".equals(gb1400Config.getEnable()) && !StringUtils.isEmpty(gb1400Config.getUserIdentify())){
log.info("准备注册平台");
new Thread(new Runnable() {
@Override
public void run() {
gb1400Service.register(gb1400Config.getUserIdentify());
}
}).start();
synchronized (Gb1400KeepAlive.registerLock) {
new Thread(new Runnable() {
@Override
public void run() {
gb1400Service.register(gb1400Config.getUserIdentify());
}
}).start();
}
}
}
......
......@@ -25,13 +25,14 @@ public class Gb1400KeepAlive {
private RestTemplate restTemplate;
@Resource
private Gb1400Service gb1400Service;
private String registerLock = new String("REGISTERLOCK");
public static String registerLock = new String("REGISTERLOCK");
@Scheduled(cron = "10 * * * * ?")
public void keepalive() {
if("0".equals(gb1400Config.getEnable())){
return;
}
log.info("开始保活,注册状态:{}", Gb1400Constants.LinkStatus);
if (!Gb1400Constants.LinkStatus) {
return;
}
......@@ -64,14 +65,16 @@ public class Gb1400KeepAlive {
Gb1400Constants.LinkStatus = false;
log.error("keepalive.Exception",e);
}
log.info("保活结束,注册状态:{}", Gb1400Constants.LinkStatus);
}
@Scheduled(cron = "0/30 * * * * ? ")
@Scheduled(cron = "30 * * * * ?")
public void register() throws Exception{
if("0".equals(gb1400Config.getEnable())){
return;
}
synchronized (registerLock) {
log.info("当前注册状态:{}", Gb1400Constants.LinkStatus);
if(!Gb1400Constants.LinkStatus) {
gb1400Service.register(gb1400Config.getUserIdentify());
} else {
......
......@@ -9,4 +9,6 @@ public interface Gb1400Service {
JsonMessage subscribes(String params);
JsonMessage unSubscribes(String subscribeId, String params);
JsonMessage subscribeNotifications(SubscribeNotificationsObj subscribeNotificationsObj);
JsonMessage selectNotifications(SubscribeNotificationsObj.SubscribeNotificationObject notificationObject);
JsonMessage deleteNotifications(String ids);
}
......@@ -27,9 +27,7 @@ import org.springframework.util.DigestUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
......@@ -168,6 +166,30 @@ public class Gb1400ServiceImpl implements Gb1400Service {
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(){
Object auth = redisTemplate.opsForHash().get(RedisConstants.GB1400_REGISTER_AUTH, gb1400Config.getIpPort());
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!