You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/12/03 16:37:48 UTC

[22/50] tomee git commit: making our best to ensure TomEEEmbeddedApplicationRunner stops the container before we delete base dir

making our best to ensure TomEEEmbeddedApplicationRunner stops the container before we delete base dir


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/8143fc0f
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/8143fc0f
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/8143fc0f

Branch: refs/heads/tomee-1.7.x
Commit: 8143fc0fda1394153f21971bdf2126bad59e2b7b
Parents: 59deb62
Author: rmannibucau <rm...@apache.org>
Authored: Tue Oct 4 15:53:32 2016 +0200
Committer: rmannibucau <rm...@apache.org>
Committed: Tue Oct 4 15:53:32 2016 +0200

----------------------------------------------------------------------
 .../org/apache/tomee/embedded/Container.java    |  2 +-
 .../TomEEEmbeddedApplicationRunner.java         | 31 +++++++++++++++++---
 2 files changed, 28 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/8143fc0f/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
index 0cd4cfa..06b33dd 100644
--- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
@@ -424,6 +424,7 @@ public class Container implements AutoCloseable {
         // create basic installation in setup to be able to handle anything the caller does between setup() and start()
         base = new File(getBaseDir());
         if (base.exists()) {
+            // TODO: get rid of Files which has its own shutdown hook which can mess up order if started/shutdown multiple times?
             Files.delete(base);
         }
 
@@ -1103,7 +1104,6 @@ public class Container implements AutoCloseable {
     }
 
     private static class TomcatWithFastSessionIDs extends InternalTomcat {
-
         @Override
         public void start() throws LifecycleException {
             // Use fast, insecure session ID generation for all tests

http://git-wip-us.apache.org/repos/asf/tomee/blob/8143fc0f/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
index 5753bc0..7f69d32 100644
--- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/TomEEEmbeddedApplicationRunner.java
@@ -53,16 +53,38 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CountDownLatch;
+import java.util.logging.Logger;
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.util.logging.Level.SEVERE;
 import static org.apache.openejb.loader.JarLocation.jarLocation;
 import static org.apache.openejb.util.Classes.ancestors;
 
 @Vetoed
 public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
+    private static final ConcurrentMap<Runnable, Runnable> SHUTDOWN_TASKS = new ConcurrentHashMap<>();
+
+    static { // to ensure we have an ordering for shutdown tasks, we typically want to avoid Files.delete() before stop()
+        Runtime.getRuntime().addShutdownHook(new Thread("TomEEEmbeddedApplicationRunner-shutdown") {
+            @Override
+            public void run() {
+                for (final Runnable task : SHUTDOWN_TASKS.keySet()) {
+                    try {
+                        task.run();
+                    } catch (final Exception e) {
+                        Logger.getLogger(TomEEEmbeddedApplicationRunner.class.getName()).log(SEVERE, e.getMessage(), e);
+                    }
+                }
+                SHUTDOWN_TASKS.clear();
+            }
+        });
+    }
+
     private volatile boolean started = false;
     private volatile Object app;
     private volatile Thread hook;
@@ -269,7 +291,7 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
             }
         }
 
-        Runtime.getRuntime().addShutdownHook(hook = new Thread() {
+        hook = new Thread() {
             @Override
             public void run() { // ensure to log errors but not fail there
                 for (final Method mtd : appFinder.findAnnotatedMethods(PreDestroy.class)) {
@@ -302,12 +324,13 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
                 postTasks.clear();
                 app = null;
                 try {
-                    Runtime.getRuntime().removeShutdownHook(this);
+                    SHUTDOWN_TASKS.remove(this);
                 } catch (final Exception e) {
                     // no-op: that's ok at that moment if not called manually
                 }
             }
-        });
+        };
+        SHUTDOWN_TASKS.put(hook, hook);
     }
 
     // if app is not set then we'll check if -Dtomee.application-composer.application is set otherwise
@@ -350,7 +373,7 @@ public class TomEEEmbeddedApplicationRunner implements AutoCloseable {
     public synchronized void close() {
         if (hook != null) {
             hook.run();
-            Runtime.getRuntime().removeShutdownHook(hook);
+            SHUTDOWN_TASKS.remove(hook);
             hook = null;
             app = null;
         }