You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ha...@apache.org on 2009/04/07 08:13:13 UTC

svn commit: r762633 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ test/java/org/apache/camel/processor/

Author: hadrian
Date: Tue Apr  7 06:13:13 2009
New Revision: 762633

URL: http://svn.apache.org/viewvc?rev=762633&view=rev
Log:
CAMEL-1509.  Fix start/stop states plus concurrency issue.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/ServiceStatus.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/ServiceStatus.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/ServiceStatus.java?rev=762633&r1=762632&r2=762633&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/ServiceStatus.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/ServiceStatus.java Tue Apr  7 06:13:13 2009
@@ -25,10 +25,10 @@
     Created, Starting, Started, Stopping, Stopped;
 
     public boolean isStartable() {
-        return this != Starting && this != Started;
+        return this == Created || this == Stopped;
     }
 
     public boolean isStoppable() {
-        return this != Stopping && this != Stopped;
+        return this == Starting || this == Started;
     }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=762633&r1=762632&r2=762633&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Tue Apr  7 06:13:13 2009
@@ -763,6 +763,20 @@
         this.errorHandlerBuilder = errorHandlerBuilder;
     }
 
+    public void start() throws Exception {
+        super.start();
+        
+        // the context is now considered started (i.e. isStarted() == true))
+        // starting routes is done after, not during context startup
+        synchronized (this) {
+            for (RouteService routeService : routeServices.values()) {
+                routeService.start();
+            }
+        }
+
+        LOG.info("Apache Camel " + getVersion() + " (CamelContext:" + getName() + ") started");
+    }
+
     // Implementation methods
     // -----------------------------------------------------------------------
 
@@ -809,17 +823,6 @@
             }
         }
         startRouteDefinitions(routeDefinitions);
-
-        // lets clear the starting flag as we are now started and we really do start up these services
-        notStarting();
-
-        synchronized (this) {
-            for (RouteService routeService : routeServices.values()) {
-                routeService.start();
-            }
-        }
-
-        LOG.info("Apache Camel " + getVersion() + " (CamelContext:" + getName() + ") started");
     }
 
     protected void startRouteDefinitions(Collection<RouteDefinition> list) throws Exception {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java?rev=762633&r1=762632&r2=762633&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java Tue Apr  7 06:13:13 2009
@@ -16,10 +16,11 @@
  */
 package org.apache.camel.impl;
 
-import java.util.ArrayList;
 import java.util.Collection;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.camel.CamelException;
 import org.apache.camel.Service;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.util.ObjectHelper;
@@ -41,34 +42,58 @@
     private String version;
 
     public void start() throws Exception {
-        if (started.compareAndSet(false, true)) {
-            starting.set(true);
-            try {
-                if (childServices != null) {
-                    ServiceHelper.startServices(childServices);
+        if (!started.get()) {
+            if (starting.compareAndSet(false, true)) {
+                boolean childrenStarted = false;
+                Exception ex = null;
+                try {
+                    if (childServices != null) {
+                        ServiceHelper.startServices(childServices);
+                    }
+                    childrenStarted = true;
+                    doStart();
+                } catch (Exception e) {
+                    ex = e;
+                } finally {
+                    if (ex != null) {
+                        stop(childrenStarted);
+                        throw ex;
+                    } else {
+                        started.set(true);
+                        starting.set(false);
+                    }
                 }
-                doStart();
-            } finally {
-                notStarting();
             }
         }
     }
-
-    public void stop() throws Exception {
-        if (started.get() && stopping.compareAndSet(false, true)) {
+    
+    private void stop(boolean childrenStarted) throws Exception {
+        if (stopping.compareAndSet(false, true)) {
             try {
-                doStop();
-            } finally {
-                if (childServices != null) {
-                    ServiceHelper.stopServices(childServices);
+                try {
+                    starting.set(false);
+                    if (childrenStarted) {
+                        doStop();
+                    }
+                } finally {
+                    started.set(false);
+                    if (childServices != null) {
+                        ServiceHelper.stopServices(childServices);
+                    }
                 }
+            } finally {
                 stopped.set(true);
-                started.set(false);
                 stopping.set(false);
             }
         }
     }
 
+    public void stop() throws Exception {
+        if (started.get()) {
+            stop(true);
+        }
+    }
+
     /**
      * Returns the current status
      */
@@ -90,14 +115,6 @@
     }
     
     /**
-     * @return true if this service is finished starting process
-     */
-    public boolean isFinishedStarting() {
-        return started.get() && !starting.get();
-    }
-    
-    
-    /**
      * @return true if this service has been started
      */
     public boolean isStarted() {
@@ -119,6 +136,13 @@
     }
 
     /**
+     * @return true if this service is closed
+     */
+    public boolean isStopped() {
+        return stopped.get();
+    }
+
+    /**
      * Helper methods so the service knows if it should keep running.
      * Returns false if the service is being stopped or is stopped.
      *
@@ -128,22 +152,10 @@
         return !(stopping.get() || stopped.get());
     }
 
-    /**
-     * @return true if this service is closed
-     */
-    public boolean isStopped() {
-        return stopped.get();
-    }
-
     protected abstract void doStart() throws Exception;
 
     protected abstract void doStop() throws Exception;
 
-
-    protected void notStarting() {
-        starting.set(false);
-    }
-
     /**
      * Creates a new thread name with the given prefix
      */
@@ -156,18 +168,16 @@
     }
 
     protected void addChildService(Object childService) {
-        if (childServices == null) {
-            childServices = new ArrayList();
+        synchronized (this) {
+            if (childServices == null) {
+                childServices = new CopyOnWriteArrayList();
+            }
         }
         childServices.add(childService);
     }
 
     protected boolean removeChildService(Object childService) {
-        if (childServices != null) {
-            return childServices.remove(childService);
-        } else {
-            return false;
-        }
+        return childServices != null ? childServices.remove(childService) : false;
     }
 
     /**
@@ -183,13 +193,8 @@
             version = aPackage.getImplementationVersion();
             if (version == null) {
                 version = aPackage.getSpecificationVersion();
-                if (version == null) {
-                    version = "";
-                }
             }
-        } else {
-            version = "";
         }
-        return version;
+        return version != null ? version : "";
     }
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java?rev=762633&r1=762632&r2=762633&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java Tue Apr  7 06:13:13 2009
@@ -24,12 +24,15 @@
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.dataset.SimpleDataSet;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * A route for simple performance testing that can be used when we suspect
  * something is wrong. Inspired by end user on forum doing this as proof of concept.
  */
 public class RoutePerformanceTest extends ContextTestSupport {
+    private static final Log LOG = LogFactory.getLog(RoutePerformanceTest.class);
 
     protected SimpleDataSet dataSet = new SimpleDataSet(1000);
 
@@ -44,8 +47,8 @@
 
         long delta = System.nanoTime() - start;
 
-        System.out.println("Took: " + delta + " ns");
-        System.out.println("Took: " + delta / 1000000 + " millis");
+        LOG.debug("Took: " + delta + " ns");
+        LOG.debug("Took: " + delta / 1000000 + " millis");
     }
 
     @Override



Re: svn commit: r762633 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ test/java/org/apache/camel/processor/

Posted by Claus Ibsen <cl...@gmail.com>.
>  public class RoutePerformanceTest extends ContextTestSupport {
> +    private static final Log LOG = LogFactory.getLog(RoutePerformanceTest.class);
>
>     protected SimpleDataSet dataSet = new SimpleDataSet(1000);
>
> @@ -44,8 +47,8 @@
>
>         long delta = System.nanoTime() - start;
>
> -        System.out.println("Took: " + delta + " ns");
> -        System.out.println("Took: " + delta / 1000000 + " millis");
> +        LOG.debug("Took: " + delta + " ns");
> +        LOG.debug("Took: " + delta / 1000000 + " millis");
>     }

Can we keep this at System.out or maybe INFO logging level, or
whatever it takes to let it output by default when you run mvn
test/install?

I used it as a quick glimpse to see if it runs at the usual
perforamance level, eg < 4 sec on my laptop.
When we had that performance degrade in Camel 1.6.0 this tests would
take x10 but most of the other tests runs at usual speed.

So its q quick indicator if something is wrong if it suddenly takes
longer/much longer time.




-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration