Skip to content

Commit ed0b5d5

Browse files
haifxudockerzhang
authored andcommitted
[INLONG-7646][Audit] Fix NPE when mq configuration is not registered (#7647)
1 parent 11a086a commit ed0b5d5

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

inlong-audit/audit-store/src/main/java/org/apache/inlong/audit/service/AuditMsgConsumerServer.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.inlong.audit.service.consume.KafkaConsume;
3838
import org.apache.inlong.audit.service.consume.PulsarConsume;
3939
import org.apache.inlong.audit.service.consume.TubeConsume;
40+
import org.apache.inlong.common.constant.MQType;
4041
import org.apache.inlong.common.pojo.audit.AuditConfigRequest;
4142
import org.apache.inlong.common.pojo.audit.MQInfo;
4243
import org.slf4j.Logger;
@@ -45,7 +46,6 @@
4546
import org.springframework.beans.factory.annotation.Autowired;
4647
import org.springframework.stereotype.Service;
4748

48-
import java.io.IOException;
4949
import java.io.InputStream;
5050
import java.util.ArrayList;
5151
import java.util.List;
@@ -70,6 +70,9 @@ public class AuditMsgConsumerServer implements InitializingBean {
7070

7171
private static final String DEFAULT_CONFIG_PROPERTIES = "application.properties";
7272

73+
// interval time of getting mq config
74+
private static final int INTERVAL_MS = 5000;
75+
7376
private final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
7477

7578
private final Gson gson = new Gson();
@@ -83,24 +86,25 @@ public void afterPropertiesSet() {
8386
List<InsertData> insertServiceList = this.getInsertServiceList();
8487

8588
for (MQInfo mqInfo : mqInfoList) {
86-
if (mqConfig.isPulsar()) {
89+
if (mqConfig.isPulsar() && MQType.PULSAR.equals(mqInfo.getMqType())) {
8790
mqConfig.setPulsarServerUrl(mqInfo.getUrl());
8891
mqConsume = new PulsarConsume(insertServiceList, storeConfig, mqConfig);
8992
break;
90-
} else if (mqConfig.isTube()) {
93+
} else if (mqConfig.isTube() && MQType.TUBEMQ.equals(mqInfo.getMqType())) {
9194
mqConfig.setTubeMasterList(mqInfo.getUrl());
9295
mqConsume = new TubeConsume(insertServiceList, storeConfig, mqConfig);
9396
break;
94-
} else if (mqConfig.isKafka()) {
97+
} else if (mqConfig.isKafka() && MQType.KAFKA.equals(mqInfo.getMqType())) {
9598
mqConfig.setKafkaServerUrl(mqInfo.getUrl());
9699
mqConsume = new KafkaConsume(insertServiceList, storeConfig, mqConfig);
97100
break;
98-
} else {
99-
LOG.error("Unknown MessageQueue {}", mqConfig.getMqType());
100-
return;
101101
}
102102
}
103103

104+
if (mqConsume == null) {
105+
LOG.error("Unknown MessageQueue {}", mqConfig.getMqType());
106+
}
107+
104108
if (storeConfig.isElasticsearchStore()) {
105109
esService.startTimerRoutine();
106110
}
@@ -133,19 +137,23 @@ private List<InsertData> getInsertServiceList() {
133137

134138
private List<MQInfo> getClusterFromManager() {
135139
Properties properties = new Properties();
140+
List<MQInfo> mqConfig;
136141
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(DEFAULT_CONFIG_PROPERTIES)) {
137142
properties.load(inputStream);
138143
String managerHosts = properties.getProperty("manager.hosts");
139144
String clusterTag = properties.getProperty("proxy.cluster.tag");
140145
String[] hostList = StringUtils.split(managerHosts, ",");
141146
for (String host : hostList) {
142-
List<MQInfo> mqConfig = getMQConfig(host, clusterTag);
143-
if (ObjectUtils.isNotEmpty(mqConfig)) {
144-
LOG.info("return mqConfig");
145-
return mqConfig;
147+
while (true) {
148+
mqConfig = getMQConfig(host, clusterTag);
149+
if (ObjectUtils.isNotEmpty(mqConfig)) {
150+
return mqConfig;
151+
}
152+
LOG.info("MQ config may not be registered yet, wait for 5s and try again");
153+
Thread.sleep(INTERVAL_MS);
146154
}
147155
}
148-
} catch (IOException e) {
156+
} catch (Exception e) {
149157
throw new RuntimeException(e);
150158
}
151159
return null;

0 commit comments

Comments
 (0)