Skip to content

Commit 8ce2dac

Browse files
committed
fix(): 优化代码以及增加DateUtil测试类
2 parents 2b7e717 + bec94fe commit 8ce2dac

File tree

8 files changed

+66
-14
lines changed

8 files changed

+66
-14
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Apollo Java 2.4.0
1111
* [Add more observability in apollo config client](https://github.com/apolloconfig/apollo-java/pull/74)
1212
* [Feature Support Kubernetes ConfigMap cache for Apollo java, golang client](https://github.com/apolloconfig/apollo-java/pull/79)
1313
* [Feature support pulling configuration information from multiple AppIds](https://github.com/apolloconfig/apollo-java/pull/70)
14-
14+
* [Fix monitor arg cause npe](https://github.com/apolloconfig/apollo-java/pull/86)
1515

1616
------------------
1717
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1)

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigMonitorInitializer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ private static void initializeMetricsEventListener() {
103103

104104
private static void initializeMetricsExporter(
105105
) {
106-
if (StringUtils.isEmpty(m_configUtil.getMonitorExternalType()) || "NONE".equals(m_configUtil.
107-
getMonitorExternalType())) {
106+
if (StringUtils.isBlank(m_configUtil.getMonitorExternalType())) {
108107
return;
109108
}
110109
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientBootstrapArgsApi.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.ctrip.framework.apollo.Apollo;
2525
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
26+
import com.ctrip.framework.apollo.core.utils.StringUtils;
2627
import com.ctrip.framework.apollo.monitor.api.ApolloClientBootstrapArgsMonitorApi;
2728
import com.ctrip.framework.apollo.monitor.internal.jmx.mbean.ApolloClientJmxBootstrapArgsMBean;
2829
import com.ctrip.framework.apollo.monitor.internal.listener.AbstractApolloClientMonitorEventListener;
@@ -33,6 +34,8 @@
3334

3435
import java.time.LocalDateTime;
3536
import java.util.Map;
37+
import java.util.Optional;
38+
3639
import org.slf4j.Logger;
3740

3841
/**
@@ -76,9 +79,9 @@ public DefaultApolloClientBootstrapArgsApi(ConfigUtil configUtil) {
7679
putAttachmentValue(APP_ID, configUtil.getAppId());
7780
putAttachmentValue(ENV, configUtil.getApolloEnv());
7881
putAttachmentValue(VERSION, Apollo.VERSION);
79-
putAttachmentValue(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()));
82+
DateUtil.formatLocalDateTime(LocalDateTime.now())
83+
.ifPresent(s -> putAttachmentValue(META_FRESH, s));
8084
putAttachmentValue(CONFIG_SERVICE_URL,"");
81-
8285
}
8386

8487
@Override
@@ -92,8 +95,11 @@ public void collect0(ApolloClientMonitorEvent event) {
9295
}
9396

9497
private void putAttachmentValue(String argName, Object value) {
98+
if(StringUtils.isBlank(argName) || value == null) {
99+
return;
100+
}
95101
bootstrapArgs.put(argName, value);
96-
bootstrapArgsString.put(argName, value == null ? null : value.toString());
102+
bootstrapArgsString.put(argName, String.valueOf(value));
97103
}
98104

99105
@Override

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/listener/impl/DefaultApolloClientNamespaceApi.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Collections;
3737
import java.util.List;
3838
import java.util.Map;
39+
import java.util.Optional;
3940
import java.util.Set;
4041
import org.slf4j.Logger;
4142

@@ -183,7 +184,8 @@ public Map<String, NamespaceMetricsString> getNamespaceMetricsString() {
183184
namespaces.forEach((namespace, metrics) -> {
184185
NamespaceMetricsString namespaceMetricsString = new NamespaceMetricsString();
185186
namespaceMetricsString.setFirstLoadTimeSpendInMs(metrics.getFirstLoadTimeSpendInMs());
186-
namespaceMetricsString.setLatestUpdateTime(DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime()));
187+
DateUtil.formatLocalDateTime(metrics.getLatestUpdateTime())
188+
.ifPresent(namespaceMetricsString::setLatestUpdateTime);
187189
namespaceMetricsString.setUsageCount(metrics.getUsageCount());
188190
namespaceMetricsString.setReleaseKey(metrics.getReleaseKey());
189191
namespaceMetricsStringMap.put(namespace, namespaceMetricsString);

apollo-client/src/main/java/com/ctrip/framework/apollo/monitor/internal/tracer/ApolloClientMonitorMessageProducer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Arrays;
3333
import java.util.Collections;
3434
import java.util.List;
35+
import java.util.Optional;
3536

3637
/**
3738
* @author Rawven
@@ -124,9 +125,9 @@ private void publishConfigChangeEvent(String name) {
124125

125126
private void publishMetaServiceEvent() {
126127
ApolloClientMonitorEventPublisher.publish(
127-
ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH)
128-
.withTag(TAG_BOOTSTRAP)
129-
.putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now())));
128+
ApolloClientMonitorEventFactory.getInstance().createEvent(META_FRESH)
129+
.withTag(TAG_BOOTSTRAP)
130+
.putAttachment(META_FRESH, DateUtil.formatLocalDateTime(LocalDateTime.now()).orElse("")));
130131
}
131132

132133
private void publishConfigServiceEvent(String name) {

apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class ConfigUtil {
7474
private boolean propertyKubernetesCacheEnabled = false;
7575
private boolean clientMonitorEnabled = false;
7676
private boolean clientMonitorJmxEnabled = false;
77-
private String monitorExternalType = "NONE";
77+
private String monitorExternalType = "";
7878
private long monitorExternalExportPeriod = 10;
7979
private int monitorExceptionQueueSize = 25;
8080

@@ -556,7 +556,7 @@ private void initClientMonitorExternalType() {
556556
monitorExternalType = System.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE);
557557
if (Strings.isNullOrEmpty(monitorExternalType)) {
558558
monitorExternalType = Foundation.app()
559-
.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "NONE");
559+
.getProperty(ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE, "");
560560
}
561561
}
562562

apollo-client/src/main/java/com/ctrip/framework/apollo/util/date/DateUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.LocalDateTime;
2020
import java.time.format.DateTimeFormatter;
21+
import java.util.Optional;
2122

2223
/**
2324
* @author Rawven
@@ -32,7 +33,8 @@ public class DateUtil {
3233
* @param localDateTime the LocalDateTime to format, can be null
3334
* @return the formatted date-time string, or null if the input is null
3435
*/
35-
public static String formatLocalDateTime(LocalDateTime localDateTime) {
36-
return localDateTime != null ? localDateTime.format(MEDIUM_FORMATTER) : null;
36+
public static Optional<String> formatLocalDateTime(LocalDateTime localDateTime) {
37+
return Optional.ofNullable(localDateTime)
38+
.map(dt -> dt.format(MEDIUM_FORMATTER));
3739
}
3840
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2022 Apollo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.ctrip.framework.apollo.util.date;
18+
19+
import org.junit.Test;
20+
import static org.junit.Assert.*;
21+
import java.time.LocalDateTime;
22+
import java.util.Optional;
23+
24+
public class DateUtilTest {
25+
26+
@Test
27+
public void testFormatLocalDateTime_validDate() {
28+
LocalDateTime dateTime = LocalDateTime.of(2024, 12, 1, 10, 30, 0);
29+
30+
Optional<String> formattedDate = DateUtil.formatLocalDateTime(dateTime);
31+
32+
assertTrue(formattedDate.isPresent());
33+
assertEquals("2024-12-01 10:30:00", formattedDate.get());
34+
}
35+
36+
@Test
37+
public void testFormatLocalDateTime_nullDate() {
38+
Optional<String> result = DateUtil.formatLocalDateTime(null);
39+
40+
assertFalse(result.isPresent());
41+
}
42+
}

0 commit comments

Comments
 (0)