You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2008/05/12 22:54:31 UTC

svn commit: r655639 - in /activemq/trunk: activemq-console/src/main/java/org/apache/activemq/console/ activemq-console/src/main/java/org/apache/activemq/console/command/ activemq-core/src/main/java/org/apache/activemq/broker/ activemq-core/src/main/jav...

Author: chirino
Date: Mon May 12 13:54:31 2008
New Revision: 655639

URL: http://svn.apache.org/viewvc?rev=655639&view=rev
Log:
Fix for https://issues.apache.org/activemq/browse/AMQ-1720
added the ability to get a callback when the broker is shutdown.  Also added an option to System.exit when the broker is shutdown.



Modified:
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/Main.java
    activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/StartCommand.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/BrokerFactoryBean.java

Modified: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/Main.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/Main.java?rev=655639&r1=655638&r2=655639&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/Main.java (original)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/Main.java Mon May 12 13:54:31 2008
@@ -104,6 +104,7 @@
 
         try {
             app.runTaskClass(tokens);
+            System.exit(0);
         } catch (ClassNotFoundException e) {
             System.out.println("Could not load class: " + e.getMessage());
             try {
@@ -114,8 +115,10 @@
                 }
             } catch (MalformedURLException e1) {
             }
+            System.exit(1);
         } catch (Throwable e) {
             System.out.println("Failed to execute main task. Reason: " + e);
+            System.exit(1);
         }
     }
 

Modified: activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/StartCommand.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/StartCommand.java?rev=655639&r1=655638&r2=655639&view=diff
==============================================================================
--- activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/StartCommand.java (original)
+++ activemq/trunk/activemq-console/src/main/java/org/apache/activemq/console/command/StartCommand.java Mon May 12 13:54:31 2008
@@ -22,6 +22,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.activemq.broker.BrokerFactory;
 import org.apache.activemq.broker.BrokerService;
@@ -93,11 +94,14 @@
 
             // Prevent the main thread from exiting unless it is terminated
             // elsewhere
-            waitForShutdown();
         } catch (Exception e) {
             context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
             throw new Exception(e);
         }
+        
+        // The broker start up fine.  If this unblocks it's cause they were stopped
+        // and this would occur because of an internal error (like the DB going offline)
+        waitForShutdown();
     }
 
     /**
@@ -122,14 +126,33 @@
         final boolean[] shutdown = new boolean[] {
             false
         };
+        
         Runtime.getRuntime().addShutdownHook(new Thread() {
             public void run() {
-                synchronized (shutdown) {
-                    shutdown[0] = true;
-                    shutdown.notify();
+                for (Iterator<BrokerService> i = brokers.iterator(); i.hasNext();) {
+                    try {
+                        BrokerService broker = i.next();
+                        broker.stop();
+                    } catch (Exception e) {
+                    }
                 }
             }
         });
+        
+        final AtomicInteger brokerCounter = new AtomicInteger(brokers.size());
+        for (BrokerService bs : brokers) {
+            bs.addShutdownHook(new Runnable() {
+                public void run() {
+                    // When the last broker lets us know he is closed....
+                    if( brokerCounter.decrementAndGet() == 0 ) {
+                        synchronized (shutdown) {
+                            shutdown[0] = true;
+                            shutdown.notify();
+                        }
+                    }
+                }
+            });
+        }
 
         // Wait for any shutdown event
         synchronized (shutdown) {
@@ -141,11 +164,6 @@
             }
         }
 
-        // Stop each broker
-        for (Iterator<BrokerService> i = brokers.iterator(); i.hasNext();) {
-            BrokerService broker = i.next();
-            broker.stop();
-        }
     }
 
     /**

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java?rev=655639&r1=655638&r2=655639&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/BrokerService.java Mon May 12 13:54:31 2008
@@ -170,8 +170,10 @@
     private boolean dedicatedTaskRunner;
     private boolean cacheTempDestinations=false;//useful for failover
     private int timeBeforePurgeTempDestinations = 5000;
-   
-
+    private List<Runnable> shutdownHooks= new ArrayList<Runnable>();
+    private boolean systemExitOnShutdown;
+    private int systemExitOnShutdownExitCode;    
+    
     static {
         String localHostName = "localhost";
         try {
@@ -425,6 +427,15 @@
         }
 
         try {
+            
+            if( systemExitOnShutdown ) {
+                addShutdownHook(new Runnable(){
+                    public void run() {
+                        System.exit(systemExitOnShutdownExitCode);
+                    }
+                });
+            }
+            
             processHelperProperties();
 
             BrokerRegistry.getInstance().bind(getBrokerName(), this);
@@ -510,6 +521,15 @@
         stopped.set(true);
         stoppedLatch.countDown();
         LOG.info("ActiveMQ JMS Message Broker (" + getBrokerName() + ", " + brokerId + ") stopped");
+        synchronized(shutdownHooks) {
+            for (Runnable hook : shutdownHooks) {
+                try {
+                    hook.run();
+                } catch ( Throwable e ) {
+                    stopper.onException(hook, e);
+                }
+            }
+        }
         stopper.throwFirstException();
     }
 
@@ -1912,4 +1932,25 @@
         this.regionBroker = regionBroker;
     }
 
+    
+    public void addShutdownHook(Runnable hook) {
+        synchronized(shutdownHooks) {
+            shutdownHooks.add(hook);
+        }
+    }
+    
+    public void removeShutdownHook(Runnable hook) {
+        synchronized(shutdownHooks) {
+            shutdownHooks.remove(hook);
+        }
+    }
+
+    public boolean isSystemExitOnShutdown() {
+        return systemExitOnShutdown;
+    }
+
+    public void setSystemExitOnShutdown(boolean systemExitOnShutdown) {
+        this.systemExitOnShutdown = systemExitOnShutdown;
+    }
+    
 }
\ No newline at end of file

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java?rev=655639&r1=655638&r2=655639&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/store/jdbc/JDBCPersistenceAdapter.java Mon May 12 13:54:31 2008
@@ -473,7 +473,7 @@
         try {
             brokerService.stop();
         } catch (Exception e) {
-            LOG.warn("Failed to stop broker");
+            LOG.warn("Failure occured while stopping broker");
         }
     }
 

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/BrokerFactoryBean.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/BrokerFactoryBean.java?rev=655639&r1=655638&r2=655639&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/BrokerFactoryBean.java (original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/xbean/BrokerFactoryBean.java Mon May 12 13:54:31 2008
@@ -51,6 +51,9 @@
     private boolean start;
     private ResourceXmlApplicationContext context;
     private ApplicationContext parentContext;
+    
+    private boolean systemExitOnShutdown;
+    private int systemExitOnShutdownExitCode;
 
     public BrokerFactoryBean() {
     }
@@ -101,6 +104,14 @@
         if (broker == null) {
             throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
         }
+        
+        if( systemExitOnShutdown ) {
+            broker.addShutdownHook(new Runnable(){
+                public void run() {
+                    System.exit(systemExitOnShutdownExitCode);
+                }
+            });
+        }
         if (start) {
             broker.start();
         }
@@ -135,4 +146,28 @@
         this.start = start;
     }
 
+    public boolean isSystemExitOnStop() {
+        return systemExitOnShutdown;
+    }
+
+    public void setSystemExitOnStop(boolean systemExitOnStop) {
+        this.systemExitOnShutdown = systemExitOnStop;
+    }
+
+    public boolean isSystemExitOnShutdown() {
+        return systemExitOnShutdown;
+    }
+
+    public void setSystemExitOnShutdown(boolean systemExitOnShutdown) {
+        this.systemExitOnShutdown = systemExitOnShutdown;
+    }
+
+    public int getSystemExitOnShutdownExitCode() {
+        return systemExitOnShutdownExitCode;
+    }
+
+    public void setSystemExitOnShutdownExitCode(int systemExitOnShutdownExitCode) {
+        this.systemExitOnShutdownExitCode = systemExitOnShutdownExitCode;
+    }
+
 }