|
|
|
|
@ -21,6 +21,7 @@ import org.springframework.scheduling.support.CronTrigger;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
import org.springframework.web.client.ResourceAccessException;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
@ -69,132 +70,141 @@ public class PostmanSchedulerService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(AgentConfigDto.Postman postman) {
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
String senderHostId = agentConfigReader.getAgentConfigDto().getMyHostId();
|
|
|
|
|
long senderTimestamp = System.currentTimeMillis();
|
|
|
|
|
String messageUuid = UUID.randomUUID().toString();
|
|
|
|
|
// DB TO DB 전송
|
|
|
|
|
if(AgentConfigDto.TaskType.DB_READ_THEN_SEND == postman.getTaskType()) {
|
|
|
|
|
String dataSourceId = postman.getMessage().getDataSourceId();
|
|
|
|
|
String sqlId = postman.getMessage().getSqlId();
|
|
|
|
|
try {
|
|
|
|
|
List<Map<String, Object>> dataMapList = sqlExecuteService.select(dataSourceId, sqlId, null);
|
|
|
|
|
String dataString = objectMapper.writeValueAsString(dataMapList);
|
|
|
|
|
ReceiveMessageDto receiveMessageDto = ReceiveMessageDto.builder().senderHostId(senderHostId).senderTimestamp(senderTimestamp)
|
|
|
|
|
.messageUuid(messageUuid).messageType(AgentConfigDto.MessageType.TRANSFER_DB_TO_DB)
|
|
|
|
|
.recipientHostId(postman.getRecipientHostId()).recipientDropBoxId(postman.getRecipientDropBoxId())
|
|
|
|
|
.data(dataString).build();
|
|
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
HttpEntity<ReceiveMessageDto> bodyEntity = new HttpEntity<>(receiveMessageDto, httpHeaders);
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
AgentConfigDto.KnownAgent knownAgent = agentConfigReader.getKnownAgent(postman.getRecipientHostId());
|
|
|
|
|
String url = "http://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen";
|
|
|
|
|
log.debug("postman to {} send a message UUID {}", receiveMessageDto.getRecipientHostId(), receiveMessageDto.getMessageUuid());
|
|
|
|
|
String response = restTemplate.postForObject(url, bodyEntity, String.class);
|
|
|
|
|
AckDto ackDto = objectMapper.readValue(response, new TypeReference<AckDto>() {});
|
|
|
|
|
log.debug("postman received ack from {} ack: {}", receiveMessageDto.getRecipientHostId(), ackDto);
|
|
|
|
|
if(AckDto.ResultType.RECEIVE_SUCCESS != ackDto.getResult()) {
|
|
|
|
|
throw new DfxException(postman.getRecipientHostId() + "에게 전송하였으나 상대방이 수신하지 못하였습니다." + response);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
String postProcessingSqlId = postman.getMessage().getPostProcessingSqlId();
|
|
|
|
|
sqlExecuteService.update(dataSourceId, postProcessingSqlId, new HashMap<String, Object>());
|
|
|
|
|
}
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (DfxException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(AgentConfigDto.TaskType.FILE_READ_THEN_SEND == postman.getTaskType()) {
|
|
|
|
|
File rootDirectoryfile = new File(postman.getMessage().getWatchDirectory());
|
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/hh/mm/ss");
|
|
|
|
|
Date today = new Date(System.currentTimeMillis());
|
|
|
|
|
String dateString = simpleDateFormat.format(today);
|
|
|
|
|
List<File> fileReadyList = new ArrayList<>();
|
|
|
|
|
List<File> workingFileList = new ArrayList<>();
|
|
|
|
|
if(rootDirectoryfile.isDirectory() && rootDirectoryfile.listFiles().length > 0) {
|
|
|
|
|
// 대상 파일 찾기 -> 작업 디렉토리(working)로 이동 -> 작업 완료 후 완료 디렉토리(done)로 이동
|
|
|
|
|
File[] files = rootDirectoryfile.listFiles();
|
|
|
|
|
for(File file : files) {
|
|
|
|
|
if(file.isFile()) {
|
|
|
|
|
fileReadyList.add(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, Object>> data = new ArrayList<>();
|
|
|
|
|
String dataString = null;
|
|
|
|
|
String workingDirectory = rootDirectoryfile.getAbsolutePath() + "/working/" + dateString;
|
|
|
|
|
File workingDirectoryFile = new File(workingDirectory);
|
|
|
|
|
if(!workingDirectoryFile.exists()) {
|
|
|
|
|
workingDirectoryFile.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
AgentConfigDto.KnownAgent knownAgent = agentConfigReader.getKnownAgent(postman.getRecipientHostId());
|
|
|
|
|
if("ALIVE".equals(agentConfigReader.getKnownAgentStatus(knownAgent.getHostId()))) {
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
String senderHostId = agentConfigReader.getAgentConfigDto().getMyHostId();
|
|
|
|
|
long senderTimestamp = System.currentTimeMillis();
|
|
|
|
|
String messageUuid = UUID.randomUUID().toString();
|
|
|
|
|
// DB TO DB 전송
|
|
|
|
|
if(AgentConfigDto.TaskType.DB_READ_THEN_SEND == postman.getTaskType()) {
|
|
|
|
|
String dataSourceId = postman.getMessage().getDataSourceId();
|
|
|
|
|
String sqlId = postman.getMessage().getSqlId();
|
|
|
|
|
String response = null;
|
|
|
|
|
try {
|
|
|
|
|
// 작업 디렉토리(working)로 이동
|
|
|
|
|
for(File file : fileReadyList) {
|
|
|
|
|
Files.move(file.toPath(), workingDirectoryFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
|
workingFileList.add(new File(workingDirectoryFile.getAbsoluteFile() + "/" + file.getName()));
|
|
|
|
|
}
|
|
|
|
|
// ReceiveMessageDto - messageJson 만들기
|
|
|
|
|
for(File file : workingFileList) {
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
Map<String, Object> paramMap = new HashMap<>();
|
|
|
|
|
map.put("file-name", file.getName());
|
|
|
|
|
if(postman.getMessage() != null && StringUtils.isNotEmpty(postman.getMessage().getMetaDropBoxId())
|
|
|
|
|
&& StringUtils.isNotEmpty(postman.getMessage().getMetaDataSqlId()) && StringUtils.isNotEmpty(postman.getMessage().getMetaDataDataSourceId())) {
|
|
|
|
|
map.put("meta-drop-box-id", postman.getMessage().getMetaDropBoxId());
|
|
|
|
|
paramMap.put("fileName", file.getName());
|
|
|
|
|
List<Map<String, Object>> messageDataMapList = sqlExecuteService.select(postman.getMessage().getDataSourceId(), postman.getMessage().getMetaDataSqlId(), paramMap);
|
|
|
|
|
if(messageDataMapList != null && messageDataMapList.get(0) != null) {
|
|
|
|
|
map.put("meta-data", messageDataMapList.get(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
data.add(map);
|
|
|
|
|
}
|
|
|
|
|
dataString = objectMapper.writeValueAsString(data);
|
|
|
|
|
List<Map<String, Object>> dataMapList = sqlExecuteService.select(dataSourceId, sqlId, null);
|
|
|
|
|
String dataString = objectMapper.writeValueAsString(dataMapList);
|
|
|
|
|
ReceiveMessageDto receiveMessageDto = ReceiveMessageDto.builder().senderHostId(senderHostId).senderTimestamp(senderTimestamp)
|
|
|
|
|
.messageUuid(messageUuid).messageType(AgentConfigDto.MessageType.TRANSFER_DB_TO_DB)
|
|
|
|
|
.recipientHostId(postman.getRecipientHostId()).recipientDropBoxId(postman.getRecipientDropBoxId())
|
|
|
|
|
.data(dataString).build();
|
|
|
|
|
String messageString = objectMapper.writeValueAsString(receiveMessageDto);
|
|
|
|
|
// http 준비
|
|
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
|
|
// 첫 번째 멀티파트는 message json
|
|
|
|
|
body.add("json", new HttpEntity<>(messageString, httpHeaders));
|
|
|
|
|
// 두 번째 이후 멀티파트는 파일
|
|
|
|
|
for(File file : workingFileList) {
|
|
|
|
|
body.add(file.getName(), new FileSystemResource(file));
|
|
|
|
|
}
|
|
|
|
|
// 전송
|
|
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
HttpEntity<ReceiveMessageDto> bodyEntity = new HttpEntity<>(receiveMessageDto, httpHeaders);
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
AgentConfigDto.KnownAgent knownAgent = agentConfigReader.getKnownAgent(postman.getRecipientHostId());
|
|
|
|
|
String url = "https://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen";
|
|
|
|
|
String response = restTemplate.postForObject(url, body, String.class);
|
|
|
|
|
String url = "http://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen";
|
|
|
|
|
log.debug("postman to {} send a message UUID {}", receiveMessageDto.getRecipientHostId(), receiveMessageDto.getMessageUuid());
|
|
|
|
|
response = restTemplate.postForObject(url, bodyEntity, String.class);
|
|
|
|
|
AckDto ackDto = objectMapper.readValue(response, new TypeReference<AckDto>() {});
|
|
|
|
|
log.debug("postman received ack from {} ack: {}", receiveMessageDto.getRecipientHostId(), ackDto);
|
|
|
|
|
if(AckDto.ResultType.RECEIVE_SUCCESS != ackDto.getResult()) {
|
|
|
|
|
throw new DfxException(postman.getRecipientHostId() + "에게 전송하였으나 상대방이 수신하지 못하였습니다." + response);
|
|
|
|
|
log.warn("대상 agent {}[{}]에게 전송하였으나 상대방이 수신하지 못하였습니다. 응답: {} 응답메시지: {}"
|
|
|
|
|
, knownAgent.getHostId(), knownAgent.getHostName(), ackDto.getResult(), ackDto.getResultText());
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// 작업 완료(done) 디렉토리로 이동
|
|
|
|
|
String doneDirectory = rootDirectoryfile + "/done/" + dateString;
|
|
|
|
|
File doneDirectoryFile = new File(doneDirectory);
|
|
|
|
|
if(!doneDirectoryFile.exists()) {
|
|
|
|
|
doneDirectoryFile.mkdirs();
|
|
|
|
|
String postProcessingSqlId = postman.getMessage().getPostProcessingSqlId();
|
|
|
|
|
sqlExecuteService.update(dataSourceId, postProcessingSqlId, new HashMap<String, Object>());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (JsonProcessingException e) {
|
|
|
|
|
log.warn("대상 agent {}[{}]에게 전송하였으나 응답메시지가 비정상입니다. 응답메시지: {}", knownAgent.getHostId(), knownAgent.getHostName(), response, e);
|
|
|
|
|
}
|
|
|
|
|
catch (ResourceAccessException e) {
|
|
|
|
|
log.warn("대상 agent {}[{}]에게 전송하였으나 응답하지 않습니다. exception: {}", knownAgent.getHostId(), knownAgent.getHostName(), e.getLocalizedMessage());
|
|
|
|
|
agentConfigReader.setKnownAgentStatus(knownAgent.getHostId(), "DOWN");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(AgentConfigDto.TaskType.FILE_READ_THEN_SEND == postman.getTaskType()) {
|
|
|
|
|
File rootDirectoryfile = new File(postman.getMessage().getWatchDirectory());
|
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/hh/mm/ss");
|
|
|
|
|
Date today = new Date(System.currentTimeMillis());
|
|
|
|
|
String dateString = simpleDateFormat.format(today);
|
|
|
|
|
List<File> fileReadyList = new ArrayList<>();
|
|
|
|
|
List<File> workingFileList = new ArrayList<>();
|
|
|
|
|
if(rootDirectoryfile.isDirectory() && rootDirectoryfile.listFiles().length > 0) {
|
|
|
|
|
// 대상 파일 찾기 -> 작업 디렉토리(working)로 이동 -> 작업 완료 후 완료 디렉토리(done)로 이동
|
|
|
|
|
File[] files = rootDirectoryfile.listFiles();
|
|
|
|
|
for(File file : files) {
|
|
|
|
|
if(file.isFile()) {
|
|
|
|
|
fileReadyList.add(file);
|
|
|
|
|
}
|
|
|
|
|
for(File file : workingFileList) {
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, Object>> data = new ArrayList<>();
|
|
|
|
|
String dataString = null;
|
|
|
|
|
String workingDirectory = rootDirectoryfile.getAbsolutePath() + "/working/" + dateString;
|
|
|
|
|
File workingDirectoryFile = new File(workingDirectory);
|
|
|
|
|
if(!workingDirectoryFile.exists()) {
|
|
|
|
|
workingDirectoryFile.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// 작업 디렉토리(working)로 이동
|
|
|
|
|
for(File file : fileReadyList) {
|
|
|
|
|
Files.move(file.toPath(), workingDirectoryFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
|
workingFileList.add(new File(workingDirectoryFile.getAbsoluteFile() + "/" + file.getName()));
|
|
|
|
|
}
|
|
|
|
|
// ReceiveMessageDto - messageJson 만들기
|
|
|
|
|
for(File file : workingFileList) {
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
Map<String, Object> paramMap = new HashMap<>();
|
|
|
|
|
map.put("file-name", file.getName());
|
|
|
|
|
if(postman.getMessage() != null && StringUtils.isNotEmpty(postman.getMessage().getMetaDropBoxId())
|
|
|
|
|
&& StringUtils.isNotEmpty(postman.getMessage().getMetaDataSqlId()) && StringUtils.isNotEmpty(postman.getMessage().getMetaDataDataSourceId())) {
|
|
|
|
|
map.put("meta-drop-box-id", postman.getMessage().getMetaDropBoxId());
|
|
|
|
|
paramMap.put("fileName", file.getName());
|
|
|
|
|
List<Map<String, Object>> messageDataMapList = sqlExecuteService.select(postman.getMessage().getDataSourceId(), postman.getMessage().getMetaDataSqlId(), paramMap);
|
|
|
|
|
if(messageDataMapList != null && messageDataMapList.get(0) != null) {
|
|
|
|
|
map.put("meta-data", messageDataMapList.get(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
data.add(map);
|
|
|
|
|
}
|
|
|
|
|
dataString = objectMapper.writeValueAsString(data);
|
|
|
|
|
ReceiveMessageDto receiveMessageDto = ReceiveMessageDto.builder().senderHostId(senderHostId).senderTimestamp(senderTimestamp)
|
|
|
|
|
.messageUuid(messageUuid).messageType(AgentConfigDto.MessageType.TRANSFER_DB_TO_DB)
|
|
|
|
|
.recipientHostId(postman.getRecipientHostId()).recipientDropBoxId(postman.getRecipientDropBoxId())
|
|
|
|
|
.data(dataString).build();
|
|
|
|
|
String messageString = objectMapper.writeValueAsString(receiveMessageDto);
|
|
|
|
|
// http 준비
|
|
|
|
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
|
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
|
|
// 첫 번째 멀티파트는 message json
|
|
|
|
|
body.add("json", new HttpEntity<>(messageString, httpHeaders));
|
|
|
|
|
// 두 번째 이후 멀티파트는 파일
|
|
|
|
|
for(File file : workingFileList) {
|
|
|
|
|
body.add(file.getName(), new FileSystemResource(file));
|
|
|
|
|
}
|
|
|
|
|
// 전송
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
String url = "https://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/listen";
|
|
|
|
|
String response = restTemplate.postForObject(url, body, String.class);
|
|
|
|
|
AckDto ackDto = objectMapper.readValue(response, new TypeReference<AckDto>() {});
|
|
|
|
|
if(AckDto.ResultType.RECEIVE_SUCCESS != ackDto.getResult()) {
|
|
|
|
|
throw new DfxException(postman.getRecipientHostId() + "에게 전송하였으나 상대방이 수신하지 못하였습니다." + response);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// 작업 완료(done) 디렉토리로 이동
|
|
|
|
|
String doneDirectory = rootDirectoryfile + "/done/" + dateString;
|
|
|
|
|
File doneDirectoryFile = new File(doneDirectory);
|
|
|
|
|
if(!doneDirectoryFile.exists()) {
|
|
|
|
|
doneDirectoryFile.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
for(File file : workingFileList) {
|
|
|
|
|
Files.move(file.toPath(), workingDirectoryFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (DfxException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (DfxException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
log.warn("대상 agent {}[{}] 실행 상태가 정상(ALIVE)이 아니므로 전송을 중지합니다. postman ID: {}", knownAgent.getHostId(), knownAgent.getHostName(), postman.getPostmanId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|