[ Status ] optimize code

This commit is contained in:
zeaslity
2023-06-15 17:17:55 +08:00
parent 54569cc41b
commit 1aa4c23dfc
5 changed files with 36 additions and 37 deletions

View File

@@ -1,12 +1,15 @@
package io.wdd.common.utils; package io.wdd.common.utils;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@@ -14,6 +17,16 @@ import java.time.format.DateTimeFormatter;
@Configuration @Configuration
public class OctopusObjectMapperConfig { public class OctopusObjectMapperConfig {
public static ObjectMapper OctopusObjectMapper = null;
@Resource
ObjectMapper objectMapper;
@PostConstruct
public void setOctopusObjectMapper() {
OctopusObjectMapper = objectMapper;
}
public static Jackson2ObjectMapperBuilderCustomizer common() { public static Jackson2ObjectMapperBuilderCustomizer common() {
return jacksonObjectMapperBuilder -> { return jacksonObjectMapperBuilder -> {

View File

@@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.utils.TimeUtils; import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.message.OctopusMessage; import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.sender.OMessageToAgentSender; import io.wdd.rpc.message.sender.OMessageToAgentSender;
import io.wdd.rpc.status.OctopusStatusMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@@ -16,6 +15,7 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.wdd.rpc.init.AgentStatusCacheService.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST; import static io.wdd.rpc.init.AgentStatusCacheService.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST;
import static io.wdd.rpc.status.OctopusStatusMessage.ConstructAgentStatusMessage;
import static io.wdd.rpc.status.OctopusStatusMessage.METRIC_STATUS_MESSAGE_TYPE; import static io.wdd.rpc.status.OctopusStatusMessage.METRIC_STATUS_MESSAGE_TYPE;
/** /**
@@ -30,8 +30,6 @@ public class AgentMetricStatusCollectService {
public static final String METRIC_REPORT_TIME_PINCH = "metricRepeatPinch"; public static final String METRIC_REPORT_TIME_PINCH = "metricRepeatPinch";
public static final String METRIC_REPORT_TIMES_COUNT = "metricRepeatCount"; public static final String METRIC_REPORT_TIMES_COUNT = "metricRepeatCount";
@Resource
OctopusStatusMessage octopusStatusMessage;
@Resource @Resource
OMessageToAgentSender oMessageToAgentSender; OMessageToAgentSender oMessageToAgentSender;
@@ -61,12 +59,11 @@ public class AgentMetricStatusCollectService {
List<OctopusMessage> octopusStatusMessageList = ALL_HEALTHY_AGENT_TOPIC_NAME_LIST List<OctopusMessage> octopusStatusMessageList = ALL_HEALTHY_AGENT_TOPIC_NAME_LIST
.stream() .stream()
.map( .map(
agentTopicName -> octopusStatusMessage agentTopicName -> ConstructAgentStatusMessage(
.ConstructAgentStatusMessage( METRIC_STATUS_MESSAGE_TYPE,
METRIC_STATUS_MESSAGE_TYPE, agentTopicName,
agentTopicName, currentTime
currentTime )
)
) )
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@@ -1,7 +1,6 @@
package io.wdd.rpc.status; package io.wdd.rpc.status;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.rpc.message.OctopusMessage; import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.OctopusMessageType; import io.wdd.rpc.message.OctopusMessageType;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@@ -9,9 +8,10 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@@ -26,9 +26,6 @@ public class OctopusStatusMessage {
public static final String METRIC_STATUS_MESSAGE_TYPE = "METRIC"; public static final String METRIC_STATUS_MESSAGE_TYPE = "METRIC";
public static final String APP_STATUS_MESSAGE_TYPE = "APP"; public static final String APP_STATUS_MESSAGE_TYPE = "APP";
@Resource
ObjectMapper objectMapper;
/** /**
* which kind of status should be return * which kind of status should be return
* metric => short time message * metric => short time message
@@ -41,7 +38,7 @@ public class OctopusStatusMessage {
int metricRepeatPinch; int metricRepeatPinch;
public OctopusMessage ConstructAgentStatusMessage(String statusType, String agentTopicName, LocalDateTime currentTime) { public static OctopusMessage ConstructAgentStatusMessage(String statusType, String agentTopicName, LocalDateTime currentTime) {
OctopusStatusMessage statusMessage = OctopusStatusMessage OctopusStatusMessage statusMessage = OctopusStatusMessage
.builder() .builder()
@@ -50,7 +47,7 @@ public class OctopusStatusMessage {
String ops; String ops;
try { try {
ops = objectMapper.writeValueAsString(statusMessage); ops = OctopusObjectMapper.writeValueAsString(statusMessage);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -6,7 +6,6 @@ import io.wdd.rpc.message.OctopusMessageType;
import io.wdd.rpc.message.handler.async.AsyncWaitOctopusMessageResultService; import io.wdd.rpc.message.handler.async.AsyncWaitOctopusMessageResultService;
import io.wdd.rpc.message.handler.async.OctopusMessageAsyncReplayContend; import io.wdd.rpc.message.handler.async.OctopusMessageAsyncReplayContend;
import io.wdd.rpc.message.sender.OMessageToAgentSender; import io.wdd.rpc.message.sender.OMessageToAgentSender;
import io.wdd.rpc.status.OctopusStatusMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -19,6 +18,7 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.wdd.rpc.init.AgentStatusCacheService.ALL_AGENT_TOPIC_NAME_LIST; import static io.wdd.rpc.init.AgentStatusCacheService.ALL_AGENT_TOPIC_NAME_LIST;
import static io.wdd.rpc.status.OctopusStatusMessage.ConstructAgentStatusMessage;
import static io.wdd.rpc.status.OctopusStatusMessage.HEALTHY_STATUS_MESSAGE_TYPE; import static io.wdd.rpc.status.OctopusStatusMessage.HEALTHY_STATUS_MESSAGE_TYPE;
@Slf4j @Slf4j
@@ -30,9 +30,6 @@ public class AsyncStatusServiceImpl implements AsyncStatusService {
@Resource @Resource
OMessageToAgentSender oMessageToAgentSender; OMessageToAgentSender oMessageToAgentSender;
@Resource
OctopusStatusMessage octopusStatusMessage;
@Resource @Resource
AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService; AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService;
@@ -105,7 +102,7 @@ public class AsyncStatusServiceImpl implements AsyncStatusService {
List<OctopusMessage> octopusStatusMessageList = ALL_AGENT_TOPIC_NAME_LIST List<OctopusMessage> octopusStatusMessageList = ALL_AGENT_TOPIC_NAME_LIST
.stream() .stream()
.map( .map(
agentTopicName -> octopusStatusMessage.ConstructAgentStatusMessage( agentTopicName -> ConstructAgentStatusMessage(
HEALTHY_STATUS_MESSAGE_TYPE, HEALTHY_STATUS_MESSAGE_TYPE,
agentTopicName, agentTopicName,
currentTime currentTime

View File

@@ -1,19 +1,14 @@
package io.wdd.server.config; package io.wdd.server.config;
import io.wdd.common.utils.OctopusObjectMapperConfig; //@Configuration
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; //public class OctopusObjectMapper {
import org.springframework.context.annotation.Bean; //
import org.springframework.context.annotation.Configuration; // //注意该段代码并未覆盖SpringBoot自动装配的ObjectMapper对象而是加强其配置。
// // use the common config of object mapper
@Configuration // @Bean
public class OctopusObjectMapper { // public Jackson2ObjectMapperBuilderCustomizer customJackson() {
// return OctopusObjectMapperConfig.common();
//注意该段代码并未覆盖SpringBoot自动装配的ObjectMapper对象而是加强其配置。 // }
// use the common config of object mapper //
@Bean //}
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return OctopusObjectMapperConfig.common();
}
}