ARTEMIS-1652 Improving delete logic on XmlDataImporter

Before this the test would use a string and a temporary file on the user's 
folder.
After this the test will use a temporary file with the proper 
File.createTemporaryFile method.


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

Branch: refs/heads/master
Commit: b2a71d27301f30e15f39bf425346c9b134dc39f0
Parents: 17db696
Author: Clebert Suconic <clebertsuco...@apache.org>
Authored: Fri Feb 2 11:32:58 2018 -0500
Committer: Clebert Suconic <clebertsuco...@apache.org>
Committed: Fri Feb 2 12:11:42 2018 -0500

----------------------------------------------------------------------
 .../cli/commands/tools/xml/XmlDataImporter.java | 39 ++++++++++++--------
 1 file changed, 23 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/b2a71d27/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XmlDataImporter.java
----------------------------------------------------------------------
diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XmlDataImporter.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XmlDataImporter.java
index 102cf5a..595ed55 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XmlDataImporter.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XmlDataImporter.java
@@ -26,6 +26,7 @@ import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -43,7 +44,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeSet;
-import java.util.UUID;
 
 import io.airlift.airline.Command;
 import io.airlift.airline.Option;
@@ -95,8 +95,6 @@ public final class XmlDataImporter extends ActionAbstract {
 
    final Map<String, Long> queueIDs = new HashMap<>();
 
-   String tempFileName = "";
-
    HashMap<String, String> oldPrefixTranslation = new HashMap<>();
 
    private ClientSession session;
@@ -328,13 +326,14 @@ public final class XmlDataImporter extends ActionAbstract 
{
 
       boolean endLoop = false;
 
+      File largeMessageTemporaryFile = null;
       // loop through the XML and gather up all the message's data (i.e. body, 
properties, queues, etc.)
       while (reader.hasNext()) {
          int eventType = reader.getEventType();
          switch (eventType) {
             case XMLStreamConstants.START_ELEMENT:
                if 
(XmlDataConstants.MESSAGE_BODY.equals(reader.getLocalName())) {
-                  processMessageBody(message.toCore());
+                  largeMessageTemporaryFile = 
processMessageBody(message.toCore());
                } else if 
(XmlDataConstants.PROPERTIES_CHILD.equals(reader.getLocalName())) {
                   processMessageProperties(message);
                } else if 
(XmlDataConstants.QUEUES_CHILD.equals(reader.getLocalName())) {
@@ -354,9 +353,9 @@ public final class XmlDataImporter extends ActionAbstract {
       }
 
       if (sort) {
-         messages.add(new MessageTemp(id, queues, message, tempFileName));
+         messages.add(new MessageTemp(id, queues, message, 
largeMessageTemporaryFile));
       } else {
-         sendMessage(queues, message, tempFileName);
+         sendMessage(queues, message, largeMessageTemporaryFile);
       }
    }
 
@@ -365,9 +364,9 @@ public final class XmlDataImporter extends ActionAbstract {
       long id;
       List<String> queues;
       Message message;
-      String tempFileName;
+      File tempFileName;
 
-      MessageTemp(long id, List<String> queues, Message message, String 
tempFileName) {
+      MessageTemp(long id, List<String> queues, Message message, File 
tempFileName) {
          this.message = message;
          this.queues = queues;
          this.message = message;
@@ -401,7 +400,7 @@ public final class XmlDataImporter extends ActionAbstract {
       return type;
    }
 
-   private void sendMessage(List<String> queues, Message message, String 
tempFileName) throws Exception {
+   private void sendMessage(List<String> queues, Message message, File 
tempFileName) throws Exception {
       StringBuilder logMessage = new StringBuilder();
       String destination = addressMap.get(queues.get(0));
 
@@ -448,10 +447,15 @@ public final class XmlDataImporter extends ActionAbstract 
{
          producer.send(message);
       }
 
-      if (tempFileName.length() > 0) {
-         File tempFile = new File(tempFileName);
-         if (!tempFile.delete()) {
-            ActiveMQServerLogger.LOGGER.couldNotDeleteTempFile(tempFileName);
+      if (tempFileName != null) {
+         try {
+            // this is to make sure the large message is sent before we delete 
it
+            // to avoid races
+            session.commit();
+         } catch (Throwable dontcare) {
+         }
+         if (!tempFileName.delete()) {
+            
ActiveMQServerLogger.LOGGER.couldNotDeleteTempFile(tempFileName.getAbsolutePath());
          }
       }
    }
@@ -532,7 +536,8 @@ public final class XmlDataImporter extends ActionAbstract {
       }
    }
 
-   private void processMessageBody(final ICoreMessage message) throws 
XMLStreamException, IOException {
+   private File processMessageBody(final ICoreMessage message) throws 
XMLStreamException, IOException {
+      File tempFileName = null;
       boolean isLarge = false;
 
       for (int i = 0; i < reader.getAttributeCount(); i++) {
@@ -546,11 +551,11 @@ public final class XmlDataImporter extends ActionAbstract 
{
          logger.debug("XMLStreamReader impl: " + reader);
       }
       if (isLarge) {
-         tempFileName = UUID.randomUUID().toString() + ".tmp";
+         tempFileName = File.createTempFile("largeMessage", ".tmp");
          if (logger.isDebugEnabled()) {
             logger.debug("Creating temp file " + tempFileName + " for large 
message.");
          }
-         try (OutputStream out = new FileOutputStream(tempFileName)) {
+         try (OutputStream out = new BufferedOutputStream(new 
FileOutputStream(tempFileName))) {
             getMessageBodyBytes(new MessageBodyBytesProcessor() {
                @Override
                public void processBodyBytes(byte[] bytes) throws IOException {
@@ -569,6 +574,8 @@ public final class XmlDataImporter extends ActionAbstract {
             }
          });
       }
+
+      return tempFileName;
    }
 
    /**

Reply via email to