activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [activemq-artemis] branch master updated: ARTEMIS-1058 Jars in web tmp dir locked on Windows
Date Tue, 22 Jan 2019 22:37:18 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new ae00423  ARTEMIS-1058 Jars in web tmp dir locked on Windows
     new 261b05e  This closes #2473
ae00423 is described below

commit ae00423034b6905564f6729f681dc8e29c86422e
Author: Howard Gao <howard.gao@gmail.com>
AuthorDate: Fri Jan 18 11:36:07 2019 +0800

    ARTEMIS-1058 Jars in web tmp dir locked on Windows
    
    Because Sun's URLClassLoader never closes it's jar file handles
    Jetty doesn't cleanup is temp web dir after Artemis broker shut
    down normally on Windows.
    
    To work around this a new process is forked before broker VM
    exits to clean up the tmp dir if they are not deleted. The
    new process out lives the main VM so that those jar's handles
    are released and the temp dir can be cleaned up.
---
 .../activemq/artemis/boot/WebTmpCleaner.java       | 64 ++++++++++++++++++++++
 .../artemis/component/WebServerComponent.java      | 41 +++++++++-----
 2 files changed, 90 insertions(+), 15 deletions(-)

diff --git a/artemis-boot/src/main/java/org/apache/activemq/artemis/boot/WebTmpCleaner.java
b/artemis-boot/src/main/java/org/apache/activemq/artemis/boot/WebTmpCleaner.java
new file mode 100644
index 0000000..42ba220
--- /dev/null
+++ b/artemis-boot/src/main/java/org/apache/activemq/artemis/boot/WebTmpCleaner.java
@@ -0,0 +1,64 @@
+/*
+ * 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.boot;
+
+import java.io.File;
+
+/**
+ * This class is used to remove the jar files
+ * in temp web dir on Windows platform where
+ * handles of the jar files are never released
+ * by URLClassLoader until the whole VM exits.
+ */
+public class WebTmpCleaner {
+
+   public static void main(String[] args) throws Exception {
+
+      String[] filesToClean = args[0].split(",");
+
+      //It needs to retry a bit as we are not sure
+      //when the main VM exists.
+      boolean allCleaned = false;
+      int maxRetries = 100;
+      while (!allCleaned && maxRetries-- > 0) {
+         allCleaned = true;
+         for (String f : filesToClean) {
+            if (!f.trim().isEmpty()) {
+               File file = new File(f);
+               if (file.exists()) {
+                  deleteFile(file);
+                  allCleaned = false;
+               }
+            }
+         }
+         Thread.sleep(200);
+      }
+   }
+
+
+   public static final void deleteFile(final File file) {
+      if (file.isDirectory()) {
+         String[] files = file.list();
+         for (String path : files) {
+            File f = new File(file, path);
+            deleteFile(f);
+         }
+      }
+      file.delete();
+   }
+
+}
diff --git a/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java
b/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java
index 05a9202..7c1a6ac 100644
--- a/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java
+++ b/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java
@@ -31,7 +31,6 @@ import org.apache.activemq.artemis.dto.AppDTO;
 import org.apache.activemq.artemis.dto.ComponentDTO;
 import org.apache.activemq.artemis.dto.WebServerDTO;
 import org.apache.activemq.artemis.utils.FileUtil;
-import org.apache.activemq.artemis.utils.TimeUtils;
 import org.eclipse.jetty.server.ConnectionFactory;
 import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.HttpConfiguration;
@@ -61,10 +60,12 @@ public class WebServerComponent implements ExternalComponent {
    private String consoleUrl;
    private List<WebAppContext> webContexts;
    private ServerConnector connector;
+   private String artemisHome;
 
    @Override
    public void configure(ComponentDTO config, String artemisInstance, String artemisHome)
throws Exception {
       webServerConfig = (WebServerDTO) config;
+      this.artemisHome = artemisHome;
       uri = new URI(webServerConfig.bind);
       server = new Server();
       String scheme = uri.getScheme();
@@ -237,28 +238,38 @@ public class WebServerComponent implements ExternalComponent {
    public void internalStop() throws Exception {
       server.stop();
       if (webContexts != null) {
+
          File tmpdir = null;
+         StringBuilder strBuilder = new StringBuilder();
+         boolean found = false;
          for (WebAppContext context : webContexts) {
             tmpdir = context.getTempDirectory();
 
-            if (tmpdir != null && !context.isPersistTempDirectory()) {
+            if (tmpdir != null && tmpdir.exists() && !context.isPersistTempDirectory())
{
                //tmpdir will be removed by deleteOnExit()
-               //somehow when broker is stopped and restarted quickly
-               //this tmpdir won't get deleted sometimes
-               boolean fileDeleted = TimeUtils.waitOnBoolean(false, 5000, tmpdir::exists);
-
-               if (!fileDeleted) {
-                  //because the execution order of shutdown hooks are
-                  //not determined, so it's possible that the deleteOnExit
-                  //is executed after this hook, in that case we force a delete.
-                  FileUtil.deleteDirectory(tmpdir);
-                  logger.debug("Force to delete temporary file on shutdown: " + tmpdir.getAbsolutePath());
-                  if (tmpdir.exists()) {
-                     ActiveMQWebLogger.LOGGER.tmpFileNotDeleted(tmpdir);
-                  }
+               //However because the URLClassLoader never release/close its opened
+               //jars the jar file won't be able to get deleted on Windows platform
+               //until after the process fully terminated. To fix this here arranges
+               //a separate process to try clean up the temp dir
+               FileUtil.deleteDirectory(tmpdir);
+               if (tmpdir.exists()) {
+                  ActiveMQWebLogger.LOGGER.tmpFileNotDeleted(tmpdir);
+                  strBuilder.append(tmpdir);
+                  strBuilder.append(",");
+                  found = true;
                }
             }
          }
+
+         if (found) {
+            String bootJar = artemisHome + File.separator + "lib" + File.separator + "artemis-boot.jar";
+
+            String[] command = {"java", "-cp", bootJar, "org.apache.activemq.artemis.boot.WebTmpCleaner",
strBuilder.toString()};
+            ProcessBuilder pb = new ProcessBuilder(command);
+
+            pb.start();
+         }
+
          webContexts.clear();
       }
    }


Mime
View raw message