Browse Source

async apply socket, websocket, server - 100대 해결

feature/remove-rabbit
지대한 1 year ago
parent
commit
ecf9fd89f9
  1. 151
      src/main/java/com/palnet/comn/collection/GPCollection.java
  2. 2
      src/main/java/com/palnet/comn/model/GPModel.java
  3. 2
      src/main/java/com/palnet/comn/utils/ControlCacheUtils.java
  4. 13
      src/main/java/com/palnet/server/command/SocketCommand.java
  5. 24
      src/main/java/com/palnet/server/task/server/service/TaskServerService.java

151
src/main/java/com/palnet/comn/collection/GPCollection.java

@ -1,77 +1,106 @@
package com.palnet.comn.collection;
import com.palnet.comn.model.GPHistoryModel;
import com.palnet.comn.model.GPModel;
import com.palnet.comn.utils.DateUtils;
import com.palnet.comn.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.time.Instant;
import java.util.*;
/**
* 현재 움직이는 물체/비행체에 대한 정보를 전송하기 위해 저장하는 Collection
* @author kang
*
* @author kang
*/
@Slf4j
@Component
public class GPCollection {
// private MessageProducer messageProducer;
// public GPCollection() {
// this.messageProducer = (MessageProducer) ContextUtils.getBean("messageProducer");
// }
// 받은 데이터 정보를 데이터구조에 맞게 저장 한다.
// 최초에 데이터를 수신한 경우 관제 ID , 관제시작 시간을 셋팅한다.
public void putData(List<GPModel> paramData) {
List<GPHistoryModel> historyList;
for(GPModel data : paramData) {
data.setServerRcvDt(DateUtils.getCurrentTime()); //서버에서 받은 시간 넣기
// History Coordinates Settings
GPHistoryModel historyModel = new GPHistoryModel();
historyModel.setObjectId(data.getObjectId());
historyModel.setLat(data.getLat());
historyModel.setLng(data.getLng());
if (data.getPostionHistory() != null) {
historyList = data.getPostionHistory();
} else {
historyList = new ArrayList<>();
}
historyList.add(historyModel);
data.setPostionHistory(historyList);
/* Message Queue Server 전달 */
// messageProducer.sendControlMessage(data);
try {
Socket socket = new Socket();
SocketAddress address = new InetSocketAddress("192.168.0.26", 4355);
socket.connect(address);
String gpsJson = JsonUtils.toJson(data);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(gpsJson.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static Map<String, List<GPModel>> gpMap = Collections.synchronizedMap(new HashMap<>()); // 누적 GPModel key-objectId
public static void init() {
gpMap = Collections.synchronizedMap(new HashMap<>());
}
public static void init(String objectId) {
if (objectId == null || objectId.isEmpty()) {
return;
}
gpMap.remove(objectId);
}
public static Map<String, List<GPModel>> getAll() {
if (gpMap.keySet().size() < 1) {
return null;
}
return gpMap;
}
public static List<GPModel> get(String objectId) {
if (objectId == null || objectId.isEmpty()) {
return null;
}
if (gpMap.get(objectId) == null) {
return null;
}
return gpMap.get(objectId);
}
public static void set(GPModel model) {
if (model == null || model.getObjectId() == null || model.getObjectId().isEmpty()) {
return;
}
List<GPModel> list = gpMap.get(model.getObjectId());
if (list == null) {
list = new ArrayList<>();
}
list.add(model);
gpMap.put(model.getObjectId(), list);
}
public static List<GPModel> send(String objectId) {
if (objectId == null || objectId.isEmpty()) {
return null;
}
if (!gpMap.containsKey(objectId)) {
return null;
}
List<GPModel> list = gpMap.get(objectId);
gpMap.remove(objectId);
return list;
}
public static Map<String, List<GPModel>> sendAll() {
log.info("sendAll start - GPMap size : {}", gpMap.size());
if(gpMap.keySet().size() < 1) {
return null;
}
Map<String, List<GPModel>> map = gpMap;
GPCollection.init();
log.info("sendAll end - GPMap size : {}/{}", gpMap.size(), map.size());
return map;
}
// 1분마다 데이터 삭제
@Scheduled(fixedDelay = 1000 * 60)
public void removeSchedule() {
log.info("removeSchedule start - GPMap size : {}", gpMap.size());
for (String key : gpMap.keySet()) {
List<GPModel> list = gpMap.get(key);
if (list == null || list.size() == 0) {
continue;
}
GPModel model = list.get(list.size() - 1);
// 1분 이상된 데이터 삭제
Instant compareTime = Instant.now().minusSeconds(60);
if (compareTime.isAfter(model.getRegDt())) {
log.info("remove data - 1 munite over : {}", key);
gpMap.remove(key);
}
}
log.info("removeSchedule end - GPMap size : {}", gpMap.size());
}
}

2
src/main/java/com/palnet/comn/model/GPModel.java

@ -2,6 +2,7 @@ package com.palnet.comn.model;
import lombok.Data;
import java.time.Instant;
import java.util.List;
@Data
@ -68,5 +69,6 @@ public class GPModel {
// 비정상 상황 식별 코드
private boolean controlWarnCd;
private Instant regDt;
}

2
src/main/java/com/palnet/comn/utils/ControlCacheUtils.java

@ -6,8 +6,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.naming.ldap.Control;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

13
src/main/java/com/palnet/server/command/SocketCommand.java

@ -1,5 +1,6 @@
package com.palnet.server.command;
import com.palnet.comn.collection.GPCollection;
import com.palnet.comn.model.ControlCacheModel;
import com.palnet.comn.model.GPHistoryModel;
import com.palnet.comn.model.GPModel;
@ -10,6 +11,7 @@ import com.palnet.comn.utils.JsonUtils;
import com.palnet.server.codec.SocketPayload;
import com.palnet.server.task.server.service.TaskServerService;
import com.palnet.server.task.wb.service.TaskWbService;
import com.sun.tools.javac.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
@ -20,6 +22,8 @@ import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;
public class SocketCommand {
@ -78,6 +82,7 @@ public class SocketCommand {
model.setSpeedType((String) obj.get("speedType"));
model.setElevType((String) obj.get("elevType"));
model.setDronStatus((String) obj.get("dronStatus"));
model.setRegDt(Instant.now());
if (obj.get("lat") != null) model.setLat(Double.valueOf(obj.get("lat").toString()));
if (obj.get("lon") != null) model.setLng(Double.valueOf(obj.get("lon").toString()));
@ -155,7 +160,7 @@ public class SocketCommand {
control.setRegTime(System.currentTimeMillis());
ControlCacheUtils.setControl(model.getObjectId(), control);
}
Long end = System.currentTimeMillis();
// Long end = System.currentTimeMillis();
// logger.info(">>> during time : {}::{}", model.getObjectId(),end - start);
// STEP 2. 이력 생성할 전문 전달 -> DRON의 대한 식별정보만 이력 관리
@ -172,7 +177,8 @@ public class SocketCommand {
// messageProducer.sendControlMessage(model);
try {
// taskServerService.sendData(model);
taskServerService.sendDataWebClient(model);
// taskServerService.sendDataWebClient(model);
GPCollection.set(model);
} catch (Exception e) {
logger.error("ERROR : {}\n{}", e.getMessage(), e.getStackTrace());
}
@ -414,5 +420,8 @@ public class SocketCommand {
throw new IllegalArgumentException("좌표 정보가 존재하지 않습니다.");
}
}
}
}

24
src/main/java/com/palnet/server/task/server/service/TaskServerService.java

@ -1,5 +1,6 @@
package com.palnet.server.task.server.service;
import com.palnet.comn.collection.GPCollection;
import com.palnet.comn.model.GPModel;
import com.palnet.comn.utils.JsonUtils;
import io.netty.channel.ChannelOption;
@ -7,6 +8,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@ -17,6 +19,8 @@ import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
@ -39,6 +43,7 @@ public class TaskServerService {
private final String APP_SEND_ASYNC_URI = "/api/server/receiver/async";
private final String APP_SEND_URI = "/api/server/receiver";
private final String APP_SEND_ALL_URI = "/api/server/receiver/all";
public void sendData(GPModel model) {
HttpRequest request = null;
@ -105,4 +110,23 @@ public class TaskServerService {
}
@Scheduled(fixedDelay = 1000 * 20)
public void sendDataAllWebClient() {
Map<String, List<GPModel>> all = GPCollection.sendAll();
if (all == null) {
return;
}
WebClient client = WebClient.builder()
.baseUrl(APP_HOST)
.defaultHeader("Content-Type", "application/json")
.build();
client.post()
.uri(APP_SEND_ALL_URI)
.body(Mono.just(all), Map.class)
.retrieve()
.bodyToMono(Void.class).subscribe();
}
}

Loading…
Cancel
Save