This is an automated email from the ASF dual-hosted git repository.

jbertram pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new f7b0c5fb9f ARTEMIS-5554 Fix minor resources leaks
f7b0c5fb9f is described below

commit f7b0c5fb9f40fc915c5da6510a3c9379943045dc
Author: Domenico Francesco Bruscino <[email protected]>
AuthorDate: Thu Jun 26 11:33:15 2025 +0200

    ARTEMIS-5554 Fix minor resources leaks
    
    Close ActiveMQConnectionFactory instances created by CLI commands.
    Use a try-with-resources block for the LargeBodyReader in the method
    getLargeMessageBuffer of the CoreMessage class to avoid leaks in case
    of exceptions.
---
 .../org/apache/activemq/artemis/cli/Artemis.java   | 17 ++--
 .../activemq/artemis/cli/commands/Connect.java     |  2 +-
 .../artemis/cli/commands/check/ClusterCheck.java   |  2 +-
 .../commands/messages/BasicConnectionAbstract.java | 93 ----------------------
 .../cli/commands/messages/ConnectionAbstract.java  | 35 ++++++--
 .../artemis/cli/commands/messages/Producer.java    |  7 +-
 .../artemis/cli/commands/messages/Transfer.java    | 16 ++--
 .../artemis/cli/commands/queue/StatQueue.java      |  2 +-
 .../cli/factory/ConnectionFactoryClosable.java     | 68 ++++++++++++++++
 .../apache/activemq/artemis/util/ServerUtil.java   |  4 +-
 .../artemis/core/message/impl/CoreMessage.java     | 21 ++---
 .../activemq/artemis/utils/RealServerTestBase.java | 25 +-----
 .../soak/interrupt/JournalFlushInterruptTest.java  |  3 +-
 13 files changed, 141 insertions(+), 154 deletions(-)

diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java
index a68738eda0..b6a2e82fda 100644
--- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java
+++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java
@@ -314,16 +314,17 @@ public class Artemis implements Runnable {
 
 
    public static String getNameFromBanner() throws Exception {
-      InputStream inputStream = 
Artemis.class.getResourceAsStream("banner.txt");
-      BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream));
-      String lastLine = "";
-      while (reader.ready()) {
-         String line = reader.readLine();
-         if (!line.trim().isEmpty()) {
-            lastLine = line;
+      try (InputStream inputStream = 
Artemis.class.getResourceAsStream("banner.txt")) {
+         BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream));
+         String lastLine = "";
+         while (reader.ready()) {
+            String line = reader.readLine();
+            if (!line.trim().isEmpty()) {
+               lastLine = line;
+            }
          }
+         return lastLine.trim();
       }
-      return lastLine.trim();
    }
 
    private static long copy(InputStream in, OutputStream out) throws Exception 
{
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Connect.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Connect.java
index 25d3da3641..7f4408ed86 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Connect.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Connect.java
@@ -29,7 +29,7 @@ public class Connect extends ConnectionAbstract {
       super.execute(context);
       try {
          CONNECTION_INFORMATION.remove();
-         createConnectionFactory();
+         createConnectionFactory().close();
          context.out.println("Connection Successful!");
 
          if (Shell.inShell()) {
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/ClusterCheck.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/ClusterCheck.java
index 498ac4adb5..2733af57e2 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/ClusterCheck.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/ClusterCheck.java
@@ -31,7 +31,7 @@ public class ClusterCheck extends ConnectionAbstract {
    public Object execute(ActionContext context) throws Exception {
       super.execute(context);
 
-      createConnectionFactory();
+      createConnectionFactory().close();
 
       try (ClusterNodeVerifier clusterVerifier = new 
ClusterNodeVerifier(brokerURL, user, password, variance).open()) {
          return clusterVerifier.verify(context);
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/BasicConnectionAbstract.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/BasicConnectionAbstract.java
deleted file mode 100644
index 7ed9b1ac62..0000000000
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/BasicConnectionAbstract.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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.activemq.artemis.cli.commands.messages;
-
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.JMSException;
-import javax.jms.JMSSecurityException;
-
-import org.apache.activemq.artemis.api.core.management.ManagementHelper;
-import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
-
-public class BasicConnectionAbstract extends ConnectionConfigurationAbtract {
-
-   protected ConnectionFactory createConnectionFactory() throws Exception {
-      recoverConnectionInformation();
-      return createConnectionFactory(brokerURL, user, password);
-   }
-
-   protected ConnectionFactory createConnectionFactory(String brokerURL,
-                                                               String user,
-                                                               String 
password) throws Exception {
-      recoverConnectionInformation();
-
-      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerURL, 
user, password);
-      try {
-         tryConnect(brokerURL, user, password, cf);
-         return cf;
-      } catch (JMSSecurityException e) {
-         // if a security exception will get the user and password through an 
input
-         if (getActionContext() != null) {
-            getActionContext().err.println("Connection failed::" + 
e.getMessage());
-         }
-         user = inputUser(user);
-         password = inputPassword(password);
-         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
-         try {
-            tryConnect(brokerURL, user, password, cf);
-         } catch (Exception e2) {
-         }
-         return cf;
-      } catch (JMSException e) {
-         // if a connection exception will ask for the URL, user and password
-         if (getActionContext() != null) {
-            getActionContext().err.println("Connection failed::" + 
e.getMessage());
-         }
-         brokerURL = inputBrokerURL(brokerURL);
-         user = inputUser(user);
-         password = inputPassword(password);
-         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
-         try {
-            tryConnect(brokerURL, user, password, cf);
-         } catch (Exception e2) {
-         }
-         return cf;
-      }
-   }
-
-   protected void tryConnect(String brokerURL,
-                          String user,
-                          String password,
-                          ConnectionFactory cf) throws JMSException {
-      Connection connection = cf.createConnection();
-      connection.close();
-      saveConnectionInfo(brokerURL, user, password);
-   }
-
-   protected void performCoreManagement(ManagementHelper.MessageAcceptor 
setup, ManagementHelper.MessageAcceptor ok, ManagementHelper.MessageAcceptor 
failed) throws Exception {
-      try (ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) 
createConnectionFactory()) {
-         ManagementHelper.doManagement(factory.getServerLocator(), user, 
password, setup, ok, failed);
-      }
-   }
-
-   protected void performCoreManagement(String uri, String user, String 
password, ManagementHelper.MessageAcceptor setup, 
ManagementHelper.MessageAcceptor ok, ManagementHelper.MessageAcceptor failed) 
throws Exception {
-      try (ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) 
createConnectionFactory(uri, user, password)) {
-         ManagementHelper.doManagement(factory.getServerLocator(), user, 
password, setup, ok, failed);
-      }
-   }
-}
\ No newline at end of file
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
index 26f1102111..b13fc6dbb8 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
@@ -16,15 +16,18 @@
  */
 package org.apache.activemq.artemis.cli.commands.messages;
 
+import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.JMSException;
 import javax.jms.JMSSecurityException;
 
+import org.apache.activemq.artemis.api.core.management.ManagementHelper;
+import org.apache.activemq.artemis.cli.factory.ConnectionFactoryClosable;
 import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
 import org.apache.qpid.jms.JmsConnectionFactory;
 import picocli.CommandLine.Option;
 
-public class ConnectionAbstract extends BasicConnectionAbstract {
+public class ConnectionAbstract extends ConnectionConfigurationAbtract {
    @Option(names = "--clientID", description = "ClientID set on the 
connection.")
    protected String clientID;
 
@@ -52,21 +55,20 @@ public class ConnectionAbstract extends 
BasicConnectionAbstract {
       this.protocol = ConnectionProtocol.fromString(protocol);
    }
 
-   @Override
-   protected ConnectionFactory createConnectionFactory() throws Exception {
+   protected ConnectionFactoryClosable createConnectionFactory() throws 
Exception {
       recoverConnectionInformation();
       return createConnectionFactory(brokerURL, user, password, clientID, 
protocol);
    }
 
-   protected ConnectionFactory createConnectionFactory(String brokerURL,
+   protected ConnectionFactoryClosable createConnectionFactory(String 
brokerURL,
                                                        String user,
                                                        String password,
                                                        String clientID,
                                                        ConnectionProtocol 
protocol) throws Exception {
       if (protocol == ConnectionProtocol.CORE) {
-         return createCoreConnectionFactory(brokerURL, user, password, 
clientID);
+         return new 
ConnectionFactoryClosable(createCoreConnectionFactory(brokerURL, user, 
password, clientID));
       } else if (protocol == ConnectionProtocol.AMQP) {
-         return createAMQPConnectionFactory(brokerURL, user, password, 
clientID);
+         return new 
ConnectionFactoryClosable(createAMQPConnectionFactory(brokerURL, user, 
password, clientID));
       } else {
          throw new IllegalStateException("protocol " + protocol + " not 
supported");
       }
@@ -177,4 +179,25 @@ public class ConnectionAbstract extends 
BasicConnectionAbstract {
          return cf;
       }
    }
+
+   protected void tryConnect(String brokerURL,
+                             String user,
+                             String password,
+                             ConnectionFactory cf) throws JMSException {
+      Connection connection = cf.createConnection();
+      connection.close();
+      saveConnectionInfo(brokerURL, user, password);
+   }
+
+   protected void performCoreManagement(ManagementHelper.MessageAcceptor 
setup, ManagementHelper.MessageAcceptor ok, ManagementHelper.MessageAcceptor 
failed) throws Exception {
+      try (ActiveMQConnectionFactory factory = createCoreConnectionFactory()) {
+         ManagementHelper.doManagement(factory.getServerLocator(), user, 
password, setup, ok, failed);
+      }
+   }
+
+   protected void performCoreManagement(String uri, String user, String 
password, ManagementHelper.MessageAcceptor setup, 
ManagementHelper.MessageAcceptor ok, ManagementHelper.MessageAcceptor failed) 
throws Exception {
+      try (ActiveMQConnectionFactory factory = 
createCoreConnectionFactory(uri, user, password, clientID)) {
+         ManagementHelper.doManagement(factory.getServerLocator(), user, 
password, setup, ok, failed);
+      }
+   }
 }
\ No newline at end of file
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Producer.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Producer.java
index 32c2eb8e49..d4bbf23bea 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Producer.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Producer.java
@@ -18,7 +18,6 @@
 package org.apache.activemq.artemis.cli.commands.messages;
 
 import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
 import javax.jms.DeliveryMode;
 import javax.jms.Destination;
 import javax.jms.Message;
@@ -28,6 +27,7 @@ import java.io.FileInputStream;
 import java.io.InputStream;
 
 import org.apache.activemq.artemis.cli.commands.ActionContext;
+import org.apache.activemq.artemis.cli.factory.ConnectionFactoryClosable;
 import org.apache.activemq.artemis.cli.factory.serialize.MessageSerializer;
 import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
@@ -149,9 +149,8 @@ public class Producer extends DestAbstract {
    public Object execute(ActionContext context) throws Exception {
       super.execute(context);
 
-      ConnectionFactory factory = createConnectionFactory();
-
-      try (Connection connection = factory.createConnection()) {
+      try (ConnectionFactoryClosable factory = createConnectionFactory();
+           Connection connection = factory.createConnection()) {
 
          // If we are reading from file, we process messages sequentially to 
guarantee ordering.  i.e. no thread creation.
          if (file != null) {
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Transfer.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Transfer.java
index 912b68e50a..ea543d9207 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Transfer.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Transfer.java
@@ -31,6 +31,7 @@ import javax.jms.Topic;
 import org.apache.activemq.artemis.api.core.Pair;
 import org.apache.activemq.artemis.cli.commands.ActionContext;
 import org.apache.activemq.artemis.cli.commands.InputAbstract;
+import org.apache.activemq.artemis.cli.factory.ConnectionFactoryClosable;
 import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
 import org.apache.qpid.jms.JmsConnectionFactory;
 import picocli.CommandLine.Command;
@@ -341,7 +342,7 @@ public class Transfer extends InputAbstract {
    }
 
    private int doTransfer(ActionContext context) throws Exception {
-      ConnectionFactory sourceConnectionFactory = 
createConnectionFactory("source", sourceProtocol, sourceURL, sourceUser, 
sourcePassword, sourceClientID);
+      ConnectionFactoryClosable sourceConnectionFactory = 
createConnectionFactory("source", sourceProtocol, sourceURL, sourceUser, 
sourcePassword, sourceClientID);
       Connection sourceConnection = sourceConnectionFactory.createConnection();
 
       Session sourceSession = 
sourceConnection.createSession(Session.SESSION_TRANSACTED);
@@ -378,7 +379,7 @@ public class Transfer extends InputAbstract {
          }
       }
 
-      ConnectionFactory targetConnectionFactory = 
createConnectionFactory("target", targetProtocol, targetURL, targetUser, 
targetPassword, null);
+      ConnectionFactoryClosable targetConnectionFactory = 
createConnectionFactory("target", targetProtocol, targetURL, targetUser, 
targetPassword, null);
       Connection targetConnection = targetConnectionFactory.createConnection();
       Session targetSession = 
targetConnection.createSession(Session.SESSION_TRANSACTED);
       Destination targetDestination = createDestination("target", 
targetSession, targetQueue, targetTopic);
@@ -437,7 +438,10 @@ public class Transfer extends InputAbstract {
       }
 
       sourceConnection.close();
+      sourceConnectionFactory.close();
+
       targetConnection.close();
+      targetConnectionFactory.close();
 
       return total;
    }
@@ -458,7 +462,7 @@ public class Transfer extends InputAbstract {
       throw new IllegalArgumentException("You need to pass either a topic or a 
queue as " + role);
    }
 
-   protected ConnectionFactory createConnectionFactory(String role,
+   protected ConnectionFactoryClosable createConnectionFactory(String role,
                                                        String protocol,
                                                        String brokerURL,
                                                        String user,
@@ -468,12 +472,12 @@ public class Transfer extends InputAbstract {
          if (isVerbose()) {
             getActionContext().out.println("Creating " + role + " CORE 
Connection towards " + brokerURL);
          }
-         return createCoreConnectionFactory(brokerURL, user, password, 
clientID);
+         return new 
ConnectionFactoryClosable(createCoreConnectionFactory(brokerURL, user, 
password, clientID));
       } else if (protocol.equals("amqp")) {
          if (isVerbose()) {
             getActionContext().out.println("Creating " + role + " AMQP 
Connection towards " + brokerURL);
          }
-         return createAMQPConnectionFactory(brokerURL, user, password, 
clientID);
+         return new 
ConnectionFactoryClosable(createAMQPConnectionFactory(brokerURL, user, 
password, clientID));
       } else {
          throw new IllegalStateException("protocol " + protocol + " not 
supported");
       }
@@ -533,6 +537,7 @@ public class Transfer extends InputAbstract {
          connection.close();
          return cf;
       } catch (JMSSecurityException e) {
+         cf.close();
          // if a security exception will get the user and password through an 
input
          getActionContext().err.println("Connection failed::" + 
e.getMessage());
          Pair<String, String> userPair = userPassword(brokerURL);
@@ -542,6 +547,7 @@ public class Transfer extends InputAbstract {
          }
          return cf;
       } catch (JMSException e) {
+         cf.close();
          // if a connection exception will ask for the URL, user and password
          getActionContext().err.println("Connection failed::" + 
e.getMessage());
          brokerURL = input("--url", "Type in the broker URL for a retry (e.g. 
tcp://localhost:61616)", brokerURL);
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java
index e9069fdd91..d422b3a369 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java
@@ -201,7 +201,7 @@ public class StatQueue extends ConnectionAbstract {
          getActionContext().out.println("filter is '" + filter + "'");
          getActionContext().out.println("maxRows='" + maxRows + "'");
       }
-      createConnectionFactory();
+      createConnectionFactory().close();
 
       if (json) {
          jsonExecution(context, filter);
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/factory/ConnectionFactoryClosable.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/factory/ConnectionFactoryClosable.java
new file mode 100644
index 0000000000..0880997b1c
--- /dev/null
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/factory/ConnectionFactoryClosable.java
@@ -0,0 +1,68 @@
+/*
+ * 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.activemq.artemis.cli.factory;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.JMSContext;
+import javax.jms.JMSException;
+
+public class ConnectionFactoryClosable implements AutoCloseable, 
ConnectionFactory {
+
+   private ConnectionFactory factory;
+
+   public ConnectionFactoryClosable(ConnectionFactory factory) {
+      this.factory = factory;
+   }
+
+   @Override
+   public void close() throws Exception {
+      if (factory instanceof AutoCloseable) {
+         ((AutoCloseable)factory).close();
+      }
+   }
+
+   @Override
+   public Connection createConnection() throws JMSException {
+      return factory.createConnection();
+   }
+
+   @Override
+   public Connection createConnection(String userName, String password) throws 
JMSException {
+      return factory.createConnection(userName, password);
+   }
+
+   @Override
+   public JMSContext createContext() {
+      return factory.createContext();
+   }
+
+   @Override
+   public JMSContext createContext(String userName, String password) {
+      return factory.createContext(userName, password);
+   }
+
+   @Override
+   public JMSContext createContext(String userName, String password, int 
sessionMode) {
+      return factory.createContext(userName, password, sessionMode);
+   }
+
+   @Override
+   public JMSContext createContext(int sessionMode) {
+      return factory.createContext(sessionMode);
+   }
+}
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java
index a77385d27f..10bd19865c 100644
--- a/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java
+++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java
@@ -148,8 +148,8 @@ public class ServerUtil {
    public static boolean waitForServerToStart(String uri, String username, 
String password, long timeout) throws InterruptedException {
       long realTimeout = System.currentTimeMillis() + timeout;
       while (System.currentTimeMillis() < realTimeout) {
-         try (ActiveMQConnectionFactory cf = 
ActiveMQJMSClient.createConnectionFactory(uri, null);
-              Connection c = cf.createConnection(username, password)) {
+         try (ActiveMQConnectionFactory cf = 
ActiveMQJMSClient.createConnectionFactory(uri, null)) {
+            cf.createConnection(username, password).close();
             System.out.println("server " + uri + " started");
          } catch (Exception e) {
             System.out.println("awaiting server " + uri + " start at ");
diff --git 
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java
 
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java
index 341245a26a..9801a1e766 100644
--- 
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java
+++ 
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/message/impl/CoreMessage.java
@@ -281,16 +281,17 @@ public class CoreMessage extends RefCountMessage 
implements ICoreMessage {
    }
 
    private ActiveMQBuffer getLargeMessageBuffer() throws ActiveMQException {
-      LargeBodyReader encoder = getLargeBodyReader();
-      encoder.open();
-      int bodySize = (int) encoder.getSize();
-      final ActiveMQBuffer buffer = new 
ChannelBufferWrapper(UnpooledByteBufAllocator.DEFAULT.heapBuffer(bodySize));
-      buffer.byteBuf().ensureWritable(bodySize);
-      final ByteBuffer nioBuffer = buffer.byteBuf().internalNioBuffer(0, 
bodySize);
-      encoder.readInto(nioBuffer);
-      buffer.writerIndex(bodySize);
-      encoder.close();
-      return buffer;
+      try (LargeBodyReader encoder = getLargeBodyReader()) {
+         encoder.open();
+         int bodySize = (int) encoder.getSize();
+         final ActiveMQBuffer buffer = new 
ChannelBufferWrapper(UnpooledByteBufAllocator.DEFAULT.heapBuffer(bodySize));
+         buffer.byteBuf().ensureWritable(bodySize);
+         final ByteBuffer nioBuffer = buffer.byteBuf().internalNioBuffer(0, 
bodySize);
+         encoder.readInto(nioBuffer);
+         buffer.writerIndex(bodySize);
+         encoder.close();
+         return buffer;
+      }
    }
 
    private ActiveMQBuffer inflate(ActiveMQBuffer buffer) throws 
DataFormatException {
diff --git 
a/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
 
b/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
index f3fdbdf17e..ce5542d074 100644
--- 
a/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
+++ 
b/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
@@ -45,9 +45,7 @@ import 
org.apache.activemq.artemis.api.core.management.ActiveMQServerControl;
 import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder;
 import org.apache.activemq.artemis.api.core.management.QueueControl;
 import org.apache.activemq.artemis.api.core.management.SimpleManagement;
-import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
 import org.apache.activemq.artemis.cli.commands.helper.HelperCreate;
-import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
 import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
 import org.apache.activemq.artemis.util.ServerUtil;
 import org.junit.jupiter.api.AfterEach;
@@ -279,27 +277,10 @@ public class RealServerTestBase extends ActiveMQTestBase {
       logger.info("Zip finished with {}", process.waitFor());
    }
 
-   public boolean waitForServerToStart(String uri, String username, String 
password, long timeout) throws InterruptedException {
-      long realTimeout = System.currentTimeMillis() + timeout;
-      while (System.currentTimeMillis() < realTimeout) {
-         try (ActiveMQConnectionFactory cf = 
ActiveMQJMSClient.createConnectionFactory(uri, null)) {
-            cf.createConnection(username, password).close();
-            System.out.println("server " + uri + " started");
-         } catch (Exception e) {
-            System.out.println("awaiting server " + uri + " start at ");
-            Thread.sleep(500);
-            continue;
-         }
-         return true;
-      }
-
-      return false;
-   }
-
    protected static void saveProperties(Properties properties, File 
propertiesFile) throws Exception {
-      OutputStream outputStream = new BufferedOutputStream(new 
FileOutputStream(propertiesFile));
-      properties.store(outputStream, "# Broker properties");
-      outputStream.close();
+      try (OutputStream outputStream = new BufferedOutputStream(new 
FileOutputStream(propertiesFile))) {
+         properties.store(outputStream, "# Broker properties");
+      }
    }
 
 
diff --git 
a/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/interrupt/JournalFlushInterruptTest.java
 
b/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/interrupt/JournalFlushInterruptTest.java
index d618e47238..2be9e38d4a 100644
--- 
a/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/interrupt/JournalFlushInterruptTest.java
+++ 
b/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/interrupt/JournalFlushInterruptTest.java
@@ -32,6 +32,7 @@ import 
org.apache.activemq.artemis.api.core.management.QueueControl;
 import org.apache.activemq.artemis.cli.commands.helper.HelperCreate;
 import org.apache.activemq.artemis.tests.soak.SoakTestBase;
 import org.apache.activemq.artemis.tests.util.CFUtil;
+import org.apache.activemq.artemis.util.ServerUtil;
 import org.apache.activemq.artemis.utils.Wait;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
@@ -97,7 +98,7 @@ public class JournalFlushInterruptTest extends SoakTestBase {
       assertTrue(serverProcess.waitFor(1, TimeUnit.MINUTES));
       serverProcess = startServer(SERVER_NAME_0, 0, 0);
 
-      waitForServerToStart("tcp://localhost:61616", "artemis", "artemis", 
5000);
+      ServerUtil.waitForServerToStart("tcp://localhost:61616", "artemis", 
"artemis", 5000);
       queueControl = getQueueControl(liveURI, nameBuilder, queueName, 
queueName, RoutingType.ANYCAST, 5000);
 
       Wait.assertEquals(messageCount, queueControl::getMessageCount);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to