activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [2/2] activemq-artemis git commit: Use try-with-resources some more
Date Fri, 29 Jul 2016 19:33:11 GMT
Use try-with-resources some more


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/c890164f
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/c890164f
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/c890164f

Branch: refs/heads/master
Commit: c890164fda0e181551951264359f33e28c73717f
Parents: 6cc0183
Author: Ville Skyttä <ville.skytta@iki.fi>
Authored: Fri Jul 29 10:39:45 2016 +0300
Committer: Clebert Suconic <clebertsuconic@apache.org>
Committed: Fri Jul 29 15:32:57 2016 -0400

----------------------------------------------------------------------
 .../jdbc/store/drivers/AbstractJDBCDriver.java  |   6 +-
 .../jms/client/ActiveMQObjectMessage.java       |   4 +-
 .../openwire/OpenWireMessageConverter.java      | 136 +++++++++----------
 .../hornetq/HornetQProtocolManagerTest.java     |  11 +-
 .../client/ConsumerWindowSizeTest.java          |   6 +-
 .../vertx/ActiveMQVertxUnitTest.java            |  23 +---
 6 files changed, 79 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c890164f/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
index 6d8be71..beac03e 100644
--- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
+++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/drivers/AbstractJDBCDriver.java
@@ -84,9 +84,9 @@ public abstract class AbstractJDBCDriver {
    public void destroy() throws Exception {
       try {
          connection.setAutoCommit(false);
-         Statement statement = connection.createStatement();
-         statement.executeUpdate("DROP TABLE " + sqlProvider.getTableName());
-         statement.close();
+         try (Statement statement = connection.createStatement()) {
+            statement.executeUpdate("DROP TABLE " + sqlProvider.getTableName());
+         }
          connection.commit();
       }
       catch (SQLException e) {

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c890164f/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQObjectMessage.java
----------------------------------------------------------------------
diff --git a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQObjectMessage.java
b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQObjectMessage.java
index f3220cb..c8b1213 100644
--- a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQObjectMessage.java
+++ b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQObjectMessage.java
@@ -135,9 +135,7 @@ public class ActiveMQObjectMessage extends ActiveMQMessage implements
ObjectMess
          return null;
       }
 
-      try {
-         ByteArrayInputStream bais = new ByteArrayInputStream(data);
-         ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bais);
+      try (ObjectInputStream ois = new ObjectInputStreamWithClassLoader(new ByteArrayInputStream(data)))
{
          Serializable object = (Serializable) ois.readObject();
          return object;
       }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c890164f/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireMessageConverter.java
----------------------------------------------------------------------
diff --git a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireMessageConverter.java
b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireMessageConverter.java
index b5689f5..8096b93 100644
--- a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireMessageConverter.java
+++ b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireMessageConverter.java
@@ -164,10 +164,8 @@ public class OpenWireMessageConverter implements MessageConverter {
                break;
             case org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE:
                if (messageCompressed) {
-                  InputStream ois = new ByteArrayInputStream(contents);
-                  ois = new InflaterInputStream(ois);
-
-                  try (org.apache.activemq.util.ByteArrayOutputStream decompressed = new
org.apache.activemq.util.ByteArrayOutputStream()) {
+                  try (InputStream ois = new InflaterInputStream(new ByteArrayInputStream(contents));
+                       org.apache.activemq.util.ByteArrayOutputStream decompressed = new
org.apache.activemq.util.ByteArrayOutputStream()) {
                      byte[] buf = new byte[1024];
                      int n = ois.read(buf);
                      while (n != -1) {
@@ -258,8 +256,7 @@ public class OpenWireMessageConverter implements MessageConverter {
             case org.apache.activemq.artemis.api.core.Message.BYTES_TYPE:
                if (messageCompressed) {
                   Inflater inflater = new Inflater();
-                  org.apache.activemq.util.ByteArrayOutputStream decompressed = new org.apache.activemq.util.ByteArrayOutputStream();
-                  try {
+                  try (org.apache.activemq.util.ByteArrayOutputStream decompressed = new
org.apache.activemq.util.ByteArrayOutputStream()) {
                      int length = ByteSequenceData.readIntBig(contents);
                      contents.offset = 0;
                      byte[] data = Arrays.copyOfRange(contents.getData(), 4, contents.getLength());
@@ -275,7 +272,6 @@ public class OpenWireMessageConverter implements MessageConverter {
                   }
                   finally {
                      inflater.end();
-                     decompressed.close();
                   }
                }
                body.writeBytes(contents.data, contents.offset, contents.length);
@@ -506,10 +502,10 @@ public class OpenWireMessageConverter implements MessageConverter {
                   if (isCompressed) {
                      out = new DeflaterOutputStream(out);
                   }
-                  DataOutputStream dataOut = new DataOutputStream(out);
-                  MarshallingSupport.writeUTF8(dataOut, text.toString());
-                  bytes = bytesOut.toByteArray();
-                  out.close();
+                  try (DataOutputStream dataOut = new DataOutputStream(out)) {
+                     MarshallingSupport.writeUTF8(dataOut, text.toString());
+                     bytes = bytesOut.toByteArray();
+                  }
                }
             }
             else if (coreType == org.apache.activemq.artemis.api.core.Message.MAP_TYPE) {
@@ -522,9 +518,9 @@ public class OpenWireMessageConverter implements MessageConverter {
                if (isCompressed) {
                   os = new DeflaterOutputStream(os);
                }
-               DataOutputStream dataOut = new DataOutputStream(os);
-               MarshallingSupport.marshalPrimitiveMap(map, dataOut);
-               dataOut.close();
+               try (DataOutputStream dataOut = new DataOutputStream(os)) {
+                  MarshallingSupport.marshalPrimitiveMap(map, dataOut);
+               }
                bytes = out.toByteArray();
             }
             else if (coreType == org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE)
{
@@ -545,61 +541,61 @@ public class OpenWireMessageConverter implements MessageConverter {
                if (isCompressed) {
                   out = new DeflaterOutputStream(bytesOut);
                }
-               DataOutputStream dataOut = new DataOutputStream(out);
-
-               boolean stop = false;
-               while (!stop && buffer.readable()) {
-                  byte primitiveType = buffer.readByte();
-                  switch (primitiveType) {
-                     case DataConstants.BOOLEAN:
-                        MarshallingSupport.marshalBoolean(dataOut, buffer.readBoolean());
-                        break;
-                     case DataConstants.BYTE:
-                        MarshallingSupport.marshalByte(dataOut, buffer.readByte());
-                        break;
-                     case DataConstants.BYTES:
-                        int len = buffer.readInt();
-                        byte[] bytesData = new byte[len];
-                        buffer.readBytes(bytesData);
-                        MarshallingSupport.marshalByteArray(dataOut, bytesData);
-                        break;
-                     case DataConstants.CHAR:
-                        char ch = (char) buffer.readShort();
-                        MarshallingSupport.marshalChar(dataOut, ch);
-                        break;
-                     case DataConstants.DOUBLE:
-                        double doubleVal = Double.longBitsToDouble(buffer.readLong());
-                        MarshallingSupport.marshalDouble(dataOut, doubleVal);
-                        break;
-                     case DataConstants.FLOAT:
-                        Float floatVal = Float.intBitsToFloat(buffer.readInt());
-                        MarshallingSupport.marshalFloat(dataOut, floatVal);
-                        break;
-                     case DataConstants.INT:
-                        MarshallingSupport.marshalInt(dataOut, buffer.readInt());
-                        break;
-                     case DataConstants.LONG:
-                        MarshallingSupport.marshalLong(dataOut, buffer.readLong());
-                        break;
-                     case DataConstants.SHORT:
-                        MarshallingSupport.marshalShort(dataOut, buffer.readShort());
-                        break;
-                     case DataConstants.STRING:
-                        String string = buffer.readNullableString();
-                        if (string == null) {
-                           MarshallingSupport.marshalNull(dataOut);
-                        }
-                        else {
-                           MarshallingSupport.marshalString(dataOut, string);
-                        }
-                        break;
-                     default:
-                        //now we stop
-                        stop = true;
-                        break;
+               try (DataOutputStream dataOut = new DataOutputStream(out)) {
+
+                  boolean stop = false;
+                  while (!stop && buffer.readable()) {
+                     byte primitiveType = buffer.readByte();
+                     switch (primitiveType) {
+                        case DataConstants.BOOLEAN:
+                           MarshallingSupport.marshalBoolean(dataOut, buffer.readBoolean());
+                           break;
+                        case DataConstants.BYTE:
+                           MarshallingSupport.marshalByte(dataOut, buffer.readByte());
+                           break;
+                        case DataConstants.BYTES:
+                           int len = buffer.readInt();
+                           byte[] bytesData = new byte[len];
+                           buffer.readBytes(bytesData);
+                           MarshallingSupport.marshalByteArray(dataOut, bytesData);
+                           break;
+                        case DataConstants.CHAR:
+                           char ch = (char) buffer.readShort();
+                           MarshallingSupport.marshalChar(dataOut, ch);
+                           break;
+                        case DataConstants.DOUBLE:
+                           double doubleVal = Double.longBitsToDouble(buffer.readLong());
+                           MarshallingSupport.marshalDouble(dataOut, doubleVal);
+                           break;
+                        case DataConstants.FLOAT:
+                           Float floatVal = Float.intBitsToFloat(buffer.readInt());
+                           MarshallingSupport.marshalFloat(dataOut, floatVal);
+                           break;
+                        case DataConstants.INT:
+                           MarshallingSupport.marshalInt(dataOut, buffer.readInt());
+                           break;
+                        case DataConstants.LONG:
+                           MarshallingSupport.marshalLong(dataOut, buffer.readLong());
+                           break;
+                        case DataConstants.SHORT:
+                           MarshallingSupport.marshalShort(dataOut, buffer.readShort());
+                           break;
+                        case DataConstants.STRING:
+                           String string = buffer.readNullableString();
+                           if (string == null) {
+                              MarshallingSupport.marshalNull(dataOut);
+                           }
+                           else {
+                              MarshallingSupport.marshalString(dataOut, string);
+                           }
+                           break;
+                        default:
+                           //now we stop
+                           stop = true;
+                           break;
+                     }
                   }
                }
-               dataOut.close();
                bytes = bytesOut.toByteArray();
             }
             else if (coreType == org.apache.activemq.artemis.api.core.Message.BYTES_TYPE)
{
@@ -608,10 +604,9 @@ public class OpenWireMessageConverter implements MessageConverter {
                buffer.readBytes(bytes);
                if (isCompressed) {
                   int length = bytes.length;
-                  org.apache.activemq.util.ByteArrayOutputStream compressed = new org.apache.activemq.util.ByteArrayOutputStream();
-                  compressed.write(new byte[4]);
                   Deflater deflater = new Deflater();
-                  try {
+                  try (org.apache.activemq.util.ByteArrayOutputStream compressed = new org.apache.activemq.util.ByteArrayOutputStream())
{
+                     compressed.write(new byte[4]);
                      deflater.setInput(bytes);
                      deflater.finish();
                      byte[] bytesBuf = new byte[1024];
@@ -625,7 +620,6 @@ public class OpenWireMessageConverter implements MessageConverter {
                   }
                   finally {
                      deflater.end();
-                     compressed.close();
                   }
                }
             }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c890164f/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolManagerTest.java
----------------------------------------------------------------------
diff --git a/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolManagerTest.java
b/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolManagerTest.java
index 0e425b8..98a4ce6 100644
--- a/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolManagerTest.java
+++ b/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/protocols/hornetq/HornetQProtocolManagerTest.java
@@ -88,12 +88,11 @@ public class HornetQProtocolManagerTest extends ActiveMQTestBase {
       manager.register(connectionFactory2, null, null, new ConcurrentHashMap<String, String>());
 
       for (XARecoveryConfig resource :manager.getResources()) {
-         ServerLocator locator = resource.createServerLocator();
-         ClientSessionFactory factory = locator.createSessionFactory();
-         ClientSession session = factory.createSession();
-         session.close();
-         factory.close();
-         locator.close();
+         try (ServerLocator locator = resource.createServerLocator();
+             ClientSessionFactory factory = locator.createSessionFactory();
+             ClientSession session = factory.createSession())         {
+            // Nothing
+         }
       }
 
    }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c890164f/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/ConsumerWindowSizeTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/ConsumerWindowSizeTest.java
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/ConsumerWindowSizeTest.java
index f1b5a1f..5009461 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/ConsumerWindowSizeTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/ConsumerWindowSizeTest.java
@@ -142,8 +142,7 @@ public class ConsumerWindowSizeTest extends ActiveMQTestBase {
    @Test
    public void testReceiveImmediateWithZeroWindow2() throws Exception {
       ActiveMQServer server = createServer(true);
-      ServerLocator locator = createInVMNonHALocator();
-      try {
+      try (ServerLocator locator = createInVMNonHALocator()) {
          server.start();
 
          locator.setConsumerWindowSize(0);
@@ -183,9 +182,6 @@ public class ConsumerWindowSizeTest extends ActiveMQTestBase {
          session1.close();
          sessionProd.close();
       }
-      finally {
-         locator.close();
-      }
    }
 
    // https://jira.jboss.org/jira/browse/HORNETQ-385

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/c890164f/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/vertx/ActiveMQVertxUnitTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/vertx/ActiveMQVertxUnitTest.java
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/vertx/ActiveMQVertxUnitTest.java
index e64bfa8..f547316 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/vertx/ActiveMQVertxUnitTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/vertx/ActiveMQVertxUnitTest.java
@@ -641,30 +641,15 @@ public class ActiveMQVertxUnitTest extends ActiveMQTestBase {
    private ClientMessage receiveFromQueue(String queueName) throws Exception {
       ClientMessage msg = null;
 
-      ServerLocator locator = null;
-      ClientSessionFactory sf = null;
-      ClientSession session = null;
+      try (ServerLocator locator = createInVMNonHALocator();
+           ClientSessionFactory sf = createSessionFactory(locator);
+           ClientSession session = sf.createSession(false, true, true)) {
 
-      try {
-         locator = createInVMNonHALocator();
-
-         sf = createSessionFactory(locator);
-
-         session = sf.createSession(false, true, true);
          ClientConsumer consumer = session.createConsumer(queueName);
          session.start();
          msg = consumer.receive(60 * 1000);
          msg.acknowledge();
-      }
-      finally {
-         if (session != null) {
-            session.commit();
-            session.close();
-         }
-         if (sf != null)
-            sf.close();
-         if (locator != null)
-            locator.close();
+         session.commit();
       }
       return msg;
    }


Mime
View raw message