From c7886acc0009556023923f2c4a1f2e9fd3b41f79 Mon Sep 17 00:00:00 2001 From: qianye Date: Fri, 11 Jul 2025 10:04:02 +0800 Subject: [PATCH 1/4] fix --- .../impl/mqclient/MQClientAPIFactory.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java b/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java index d85dcc70a55..a04ab9f158c 100644 --- a/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java +++ b/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java @@ -17,25 +17,24 @@ package org.apache.rocketmq.client.impl.mqclient; import com.google.common.base.Strings; - import java.time.Duration; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; - import org.apache.commons.lang3.StringUtils; import org.apache.rocketmq.client.ClientConfig; import org.apache.rocketmq.client.common.NameserverAccessConfig; import org.apache.rocketmq.client.impl.ClientRemotingProcessor; import org.apache.rocketmq.common.MixAll; -import org.apache.rocketmq.common.utils.AsyncShutdownHelper; import org.apache.rocketmq.common.ObjectCreator; +import org.apache.rocketmq.common.namesrv.NameServerUpdateCallback; +import org.apache.rocketmq.common.utils.AsyncShutdownHelper; import org.apache.rocketmq.common.utils.StartAndShutdown; import org.apache.rocketmq.remoting.RPCHook; import org.apache.rocketmq.remoting.RemotingClient; import org.apache.rocketmq.remoting.netty.NettyClientConfig; -public class MQClientAPIFactory implements StartAndShutdown { +public class MQClientAPIFactory implements NameServerUpdateCallback, StartAndShutdown { private MQClientAPIExt[] clients; private final String namePrefix; @@ -133,7 +132,9 @@ protected MQClientAPIExt createAndStart(String instanceName) { remotingClientCreator ); - if (!mqClientAPIExt.updateNameServerAddressList()) { + if (StringUtils.isEmpty(nameserverAccessConfig.getNamesrvDomain())) { + mqClientAPIExt.updateNameServerAddressList(nameserverAccessConfig.getNamesrvAddr()); + } else { mqClientAPIExt.fetchNameServerAddr(); this.scheduledExecutorService.scheduleAtFixedRate( mqClientAPIExt::fetchNameServerAddr, @@ -142,7 +143,16 @@ protected MQClientAPIExt createAndStart(String instanceName) { TimeUnit.MILLISECONDS ); } + mqClientAPIExt.start(); return mqClientAPIExt; } + + @Override + public String onNameServerAddressChange(String namesrvAddress) { + for (MQClientAPIExt client : clients) { + client.onNameServerAddressChange(namesrvAddress); + } + return namesrvAddress; + } } From 2e0b7d9d828a6680192e30ecbd1b2c2dc141e4da Mon Sep 17 00:00:00 2001 From: qianye Date: Fri, 11 Jul 2025 11:08:51 +0800 Subject: [PATCH 2/4] fix --- .../client/impl/mqclient/MQClientAPITest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java diff --git a/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java b/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java new file mode 100644 index 00000000000..77f3ad469ca --- /dev/null +++ b/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java @@ -0,0 +1,129 @@ +package org.apache.rocketmq.client.impl.mqclient; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import org.apache.rocketmq.client.common.NameserverAccessConfig; +import org.apache.rocketmq.client.impl.ClientRemotingProcessor; +import org.apache.rocketmq.common.MixAll; +import org.apache.rocketmq.common.utils.ThreadUtils; +import org.apache.rocketmq.remoting.RPCHook; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class MQClientAPITest { + + private NameserverAccessConfig nameserverAccessConfig; + private final ClientRemotingProcessor clientRemotingProcessor = new DoNothingClientRemotingProcessor(null); + private final RPCHook rpcHook = null; + private ScheduledExecutorService scheduledExecutorService; + private MQClientAPIFactory mqClientAPIFactory; + + @BeforeEach + void setUp() { + scheduledExecutorService = ThreadUtils.newSingleThreadScheduledExecutor("TestScheduledExecutorService", true); + } + + @AfterEach + public void tearDown() { + scheduledExecutorService.shutdownNow(); + } + + @Test + void testInitWithNamesrvAddr() { + nameserverAccessConfig = new NameserverAccessConfig("127.0.0.1:9876", "", ""); + + mqClientAPIFactory = new MQClientAPIFactory( + nameserverAccessConfig, + "TestPrefix", + 2, + clientRemotingProcessor, + rpcHook, + scheduledExecutorService + ); + + assertEquals("127.0.0.1:9876", System.getProperty("rocketmq.namesrv.addr")); + } + + @Test + void testInitWithNamesrvDomain() { + nameserverAccessConfig = new NameserverAccessConfig("", "test-domain", ""); + + mqClientAPIFactory = new MQClientAPIFactory( + nameserverAccessConfig, + "TestPrefix", + 2, + clientRemotingProcessor, + rpcHook, + scheduledExecutorService + ); + + assertEquals("test-domain", System.getProperty("rocketmq.namesrv.domain")); + } + + @Test + void testInitThrowsExceptionWhenBothEmpty() { + nameserverAccessConfig = new NameserverAccessConfig("", "", ""); + + RuntimeException exception = assertThrows(RuntimeException.class, () -> new MQClientAPIFactory( + nameserverAccessConfig, + "TestPrefix", + 2, + clientRemotingProcessor, + rpcHook, + scheduledExecutorService + )); + + assertEquals("The configuration item NamesrvAddr is not configured", exception.getMessage()); + } + + @Test + void testStartCreatesClients() throws Exception { + nameserverAccessConfig = new NameserverAccessConfig("127.0.0.1:9876", "", ""); + + mqClientAPIFactory = new MQClientAPIFactory( + nameserverAccessConfig, + "TestPrefix", + 2, + clientRemotingProcessor, + rpcHook, + scheduledExecutorService + ); + + System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, "127.0.0.1:123"); + + mqClientAPIFactory.start(); + + // Assert + MQClientAPIExt client = mqClientAPIFactory.getClient(); + List nameServerAddressList = client.getNameServerAddressList(); + assertEquals(1, nameServerAddressList.size()); + assertEquals("127.0.0.1:9876", nameServerAddressList.get(0)); + } + + @Test + void testOnNameServerAddressChangeUpdatesAllClients() throws Exception { + nameserverAccessConfig = new NameserverAccessConfig("127.0.0.1:9876", "", ""); + + mqClientAPIFactory = new MQClientAPIFactory( + nameserverAccessConfig, + "TestPrefix", + 2, + clientRemotingProcessor, + rpcHook, + scheduledExecutorService + ); + mqClientAPIFactory.start(); + + // Act + mqClientAPIFactory.onNameServerAddressChange("new-address0;new-address1"); + + MQClientAPIExt client = mqClientAPIFactory.getClient(); + List nameServerAddressList = client.getNameServerAddressList(); + assertEquals(2, nameServerAddressList.size()); + assertEquals("new-address0", nameServerAddressList.get(0)); + } +} From c32a05529b1e0d4bfc3fd80fafbe16afc8141acc Mon Sep 17 00:00:00 2001 From: qianye Date: Fri, 11 Jul 2025 11:18:56 +0800 Subject: [PATCH 3/4] fix --- .../client/impl/mqclient/MQClientAPITest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java b/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java index 77f3ad469ca..965c5ae16b6 100644 --- a/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java +++ b/client/src/test/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPITest.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.rocketmq.client.impl.mqclient; import static org.junit.jupiter.api.Assertions.assertEquals; From 2850df00d021776beb1601db846c2685d63c8916 Mon Sep 17 00:00:00 2001 From: qianye Date: Fri, 11 Jul 2025 11:34:00 +0800 Subject: [PATCH 4/4] fix --- .../client/impl/mqclient/MQClientAPIFactory.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java b/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java index a04ab9f158c..28c1ad1930d 100644 --- a/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java +++ b/client/src/main/java/org/apache/rocketmq/client/impl/mqclient/MQClientAPIFactory.java @@ -27,14 +27,13 @@ import org.apache.rocketmq.client.impl.ClientRemotingProcessor; import org.apache.rocketmq.common.MixAll; import org.apache.rocketmq.common.ObjectCreator; -import org.apache.rocketmq.common.namesrv.NameServerUpdateCallback; import org.apache.rocketmq.common.utils.AsyncShutdownHelper; import org.apache.rocketmq.common.utils.StartAndShutdown; import org.apache.rocketmq.remoting.RPCHook; import org.apache.rocketmq.remoting.RemotingClient; import org.apache.rocketmq.remoting.netty.NettyClientConfig; -public class MQClientAPIFactory implements NameServerUpdateCallback, StartAndShutdown { +public class MQClientAPIFactory implements StartAndShutdown { private MQClientAPIExt[] clients; private final String namePrefix; @@ -148,11 +147,13 @@ protected MQClientAPIExt createAndStart(String instanceName) { return mqClientAPIExt; } - @Override - public String onNameServerAddressChange(String namesrvAddress) { + public void onNameServerAddressChange(String namesrvAddress) { for (MQClientAPIExt client : clients) { client.onNameServerAddressChange(namesrvAddress); } - return namesrvAddress; + } + + public MQClientAPIExt[] getClients() { + return clients; } }