Author: ningjiang
Date: Wed Apr  8 03:31:09 2009
New Revision: 763092

URL: http://svn.apache.org/viewvc?rev=763092&view=rev
Log:
Merged revisions 762633,762650 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r762633 | hadrian | 2009-04-07 14:13:13 +0800 (Tue, 07 Apr 2009) | 2 lines
  
  CAMEL-1509.  Fix start/stop states plus concurrency issue.
........
  r762650 | ningjiang | 2009-04-07 16:27:32 +0800 (Tue, 07 Apr 2009) | 1 line
  
  CAMEL-1509 Did a quick fix for the semantic change of started.
........

Modified:
    camel/branches/camel-1.x/   (props changed)
    
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java
    
camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Apr  8 03:31:09 2009
@@ -1 +1 @@
-/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762935
+/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=763092&r1=763091&r2=763092&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 (original)
+++ 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 Wed Apr  8 03:31:09 2009
@@ -178,7 +178,7 @@
                     component = getComponentResolver().resolveComponent(name, 
this);
                     if (component != null) {
                         addComponent(name, component);
-                        if (isStarted()) {
+                        if (isStarted() || isStarting()) {
                             // If the component is looked up after the context
                             // is started,
                             // lets start it up.
@@ -603,6 +603,18 @@
         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) {
+            startRoutes(routes);
+        }
+
+        LOG.info("Apache Camel " + getVersion() + " (CamelContext:" + 
getName() + ") started");
+    }
+
     // Implementation methods
     // -----------------------------------------------------------------------
 
@@ -649,9 +661,6 @@
             }
         }
         startRouteDefinitions(routeDefinitions);
-        startRoutes(routes);
-        
-        LOG.info("Apache Camel " + getVersion() + " (CamelContext:" + 
getName() + ") started");
     }
 
     protected void startRouteDefinitions(Collection<RouteType> list) throws 
Exception {

Modified: 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java?rev=763092&r1=763091&r2=763092&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java
 (original)
+++ 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ServiceSupport.java
 Wed Apr  8 03:31:09 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.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
@@ -40,42 +41,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 {
-                starting.set(false);
             }
         }
     }
-
-    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);
             }
         }
     }
 
-    /**
-     * @return true if this service is finished starting process
-     */
-    public boolean isFinishedStarting() {
-        return started.get() && !starting.get();
+    public void stop() throws Exception {
+        if (started.get()) {
+            stop(true);
+        }
     }
-    
-    
+
     /**
      * @return true if this service has been started
      */
@@ -98,6 +115,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.
      *
@@ -107,13 +131,6 @@
         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;
@@ -130,18 +147,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;
     }
     
     protected synchronized String getVersion() {
@@ -154,14 +169,8 @@
             version = aPackage.getImplementationVersion();
             if (version == null) {
                 version = aPackage.getSpecificationVersion();
-                if (version == null) {
-                    version = "";
-                }
             }
-        } else {
-            version = "";
         }
-        
-        return version;
+        return version != null ? version : "";
     }
 }

Modified: 
camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java?rev=763092&r1=763091&r2=763092&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java
 (original)
+++ 
camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/processor/RoutePerformanceTest.java
 Wed Apr  8 03:31:09 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);
 
@@ -43,8 +46,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


Reply via email to