This is an automated email from the ASF dual-hosted git repository.
jinrongtong pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new 543c11820 [ISSUE #6060] Optimize AutoSwitchRoleBase#nextPort method
(#6068)
543c11820 is described below
commit 543c11820028c0e9ffd9c6ab5895512d73149370
Author: mxsm <[email protected]>
AuthorDate: Thu Mar 2 20:01:02 2023 +0800
[ISSUE #6060] Optimize AutoSwitchRoleBase#nextPort method (#6068)
* [ISSUE #6060]Optimize AutoSwitchRoleBase#nextPort method
* polish code
* polish code style
---
.../test/autoswitchrole/AutoSwitchRoleBase.java | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git
a/test/src/test/java/org/apache/rocketmq/test/autoswitchrole/AutoSwitchRoleBase.java
b/test/src/test/java/org/apache/rocketmq/test/autoswitchrole/AutoSwitchRoleBase.java
index f9743f2ec..bc2c0dd0b 100644
---
a/test/src/test/java/org/apache/rocketmq/test/autoswitchrole/AutoSwitchRoleBase.java
+++
b/test/src/test/java/org/apache/rocketmq/test/autoswitchrole/AutoSwitchRoleBase.java
@@ -58,7 +58,7 @@ public class AutoSwitchRoleBase {
protected static List<BrokerController> brokerList;
private static SocketAddress bornHost;
private static SocketAddress storeHost;
- private static Integer number = 0;
+ private static int number = 0;
protected static void initialize() {
brokerList = new ArrayList<>();
@@ -69,28 +69,28 @@ public class AutoSwitchRoleBase {
}
}
- public static Integer nextPort() throws IOException {
+ public static int nextPort() throws IOException {
return nextPort(1001, 9999);
}
- public static Integer nextPort(Integer minPort, Integer maxPort) throws
IOException {
+ public static int nextPort(int minPort, int maxPort) throws IOException {
+
Random random = new Random();
int tempPort;
int port;
- try {
- while (true) {
+ while (true) {
+ try {
tempPort = random.nextInt(maxPort) % (maxPort - minPort + 1) +
minPort;
ServerSocket serverSocket = new ServerSocket(tempPort);
port = serverSocket.getLocalPort();
serverSocket.close();
break;
+ } catch (IOException ignored) {
+ if (number > 200) {
+ throw new IOException("This server's open ports are
temporarily full!");
+ }
+ ++number;
}
- } catch (Exception ignored) {
- if (number > 200) {
- throw new IOException("This server's open ports are
temporarily full!");
- }
- number++;
- port = nextPort(minPort, maxPort);
}
number = 0;
return port;