ALIVE 메시지 송신 기능 완료 - StatusCheckerSchedulerService : 주기적으로 상태 확인함 - Postman 등 메시지를 전송할 때 상태를 확인하고 전송하는 것으로 수정하여야 함main
parent
30c4e43447
commit
e0ea8cff19
@ -0,0 +1,79 @@
|
||||
package com.bsmlab.dfx.agent.task.status;
|
||||
|
||||
import com.bsmlab.dfx.agent.config.AgentConfigDto;
|
||||
import com.bsmlab.dfx.agent.config.AgentConfigReader;
|
||||
import com.bsmlab.dfx.agent.listener.dto.AckDto;
|
||||
import com.bsmlab.dfx.agent.listener.dto.CommandDto;
|
||||
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
|
||||
import com.bsmlab.dfx.agent.support.exception.DfxException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class StatusCheckerSchedulerService {
|
||||
private final ThreadPoolTaskScheduler statusCheckerThreadPoolTaskScheduler;
|
||||
private final AgentConfigReader agentConfigReader;
|
||||
private ScheduledFuture<?> scheduledFuture;
|
||||
|
||||
// StartupRunner 로 부터 실행됨
|
||||
public void launch() {
|
||||
log.debug("StatusCheckerSchedulerService launch");
|
||||
String cron = agentConfigReader.getAgentConfigDto().getStatusChecker().getCron();
|
||||
this.scheduledFuture = statusCheckerThreadPoolTaskScheduler.schedule(() -> run(), new CronTrigger(cron));
|
||||
}
|
||||
|
||||
// statusCheckerThreadPoolTaskScheduler 가 실행함
|
||||
public void run() {
|
||||
List<AgentConfigDto.KnownAgent> knownAgentList = this.agentConfigReader.getAgentConfigDto().getKnownAgentList();
|
||||
if(knownAgentList != null) {
|
||||
for(AgentConfigDto.KnownAgent knownAgent : knownAgentList) {
|
||||
String messageUuid = UUID.randomUUID().toString();
|
||||
CommandDto commandDto = CommandDto.builder().commandType(CommandDto.CommandType.ALIVE).messageUuid(messageUuid).build();
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<CommandDto> bodyEntity = new HttpEntity<>(commandDto, httpHeaders);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String url = "http://" + knownAgent.getHostName() + ":" + knownAgent.getListenPort() + "/command";
|
||||
log.debug("StatusChecker to {} send a message UUID {}", knownAgent.getHostName(), messageUuid);
|
||||
String response = restTemplate.postForObject(url, bodyEntity, String.class);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
AckDto ackDto = null;
|
||||
try {
|
||||
ackDto = objectMapper.readValue(response, new TypeReference<AckDto>() {});
|
||||
log.debug("StatusChecker received ack from {} ack: {}", knownAgent.getHostName(), ackDto);
|
||||
if(AckDto.ResultType.PROCESS_SUCCESS == ackDto.getResult()) {
|
||||
// known agent is alive
|
||||
this.agentConfigReader.setKnownAgentStatus(knownAgent.getHostId(), "ALIVE");
|
||||
log.debug("known agent {} {} is ALIVE", knownAgent.getHostId(), knownAgent.getHostName());
|
||||
}
|
||||
else {
|
||||
// known agent is down
|
||||
this.agentConfigReader.setKnownAgentStatus(knownAgent.getHostId(), "DOWN");
|
||||
log.debug("known agent {} {} is DOWN", knownAgent.getHostId(), knownAgent.getHostName());
|
||||
}
|
||||
} catch (JsonProcessingException e) {
|
||||
this.agentConfigReader.setKnownAgentStatus(knownAgent.getHostId(), "DOWN");
|
||||
log.debug("known agent {} {} alive message is not valid then set DOWN", knownAgent.getHostId(), knownAgent.getHostName());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue