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 2011/07/14 22:29:42 UTC

svn commit: r1146885 - /activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/BaseService.scala

Author: chirino
Date: Thu Jul 14 20:29:41 2011
New Revision: 1146885

URL: http://svn.apache.org/viewvc?rev=1146885&view=rev
Log:
Simpler more robust callback handling for the BaseService class.

Modified:
    activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/BaseService.scala

Modified: activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/BaseService.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/BaseService.scala?rev=1146885&r1=1146884&r2=1146885&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/BaseService.scala (original)
+++ activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/BaseService.scala Thu Jul 14 20:29:41 2011
@@ -49,17 +49,11 @@ trait BaseService extends Service with D
     def is_failed= false
   }
 
-  trait CallbackSupport {
-    var callbacks:List[Runnable] = Nil
-    def << (r:Runnable) = if(r!=null) { callbacks ::= r }
-    def done = { callbacks.foreach(_.run); callbacks=Nil }
-  }
-
   protected class CREATED extends State { override def is_created = true  }
-  protected class STARTING extends State with CallbackSupport { override def is_starting = true  }
+  protected class STARTING extends State { override def is_starting = true  }
   protected class FAILED extends State { override def is_failed = true  }
   protected class STARTED extends State { override def is_started = true  }
-  protected class STOPPING extends State with CallbackSupport { override def is_stopping = true  }
+  protected class STOPPING extends State { override def is_stopping = true  }
   protected class STOPPED extends State { override def is_stopped = true  }
 
   final def start() = start(null)
@@ -78,14 +72,21 @@ trait BaseService extends Service with D
 
   final def start(on_completed:Runnable) = {
     def start_task:Runnable = ^{
+      def done = {
+        pending_actions.foreach(dispatch_queue.execute _)
+        pending_actions.clear()
+        if( on_completed!=null ) {
+          on_completed.run
+        }
+      }
+
       def do_start = {
         val state = new STARTING()
-        state << on_completed
         _service_state = state
         try {
           _start(^ {
             _service_state = new STARTED
-            state.done
+            done
           })
         }
         catch {
@@ -93,16 +94,10 @@ trait BaseService extends Service with D
             error(e, "Start failed due to %s", e)
             _serviceFailure = e
             _service_state = new FAILED
-            state.done
-        }
-      }
-      def done = {
-        pending_actions.foreach(dispatch_queue.execute _)
-        pending_actions.clear()
-        if( on_completed!=null ) {
-          on_completed.run
+            done
         }
       }
+
       _service_state match {
         case state:CREATED =>
           do_start
@@ -125,8 +120,9 @@ trait BaseService extends Service with D
   final def stop(on_completed:Runnable) = {
     def stop_task:Runnable = ^{
       def done = {
-        pending_actions.foreach(dispatch_queue.execute _)
+        val tmp = pending_actions.toArray
         pending_actions.clear
+        tmp.foreach(dispatch_queue.execute _)
         if( on_completed!=null ) {
           on_completed.run
         }
@@ -134,12 +130,11 @@ trait BaseService extends Service with D
       _service_state match {
         case state:STARTED =>
           val state = new STOPPING
-          state << on_completed
           _service_state = state
           try {
             _stop(^ {
               _service_state = new STOPPED
-              state.done
+              done
             })
           }
           catch {
@@ -147,7 +142,7 @@ trait BaseService extends Service with D
               error(e, "Stop failed due to: %s", e)
               _serviceFailure = e
               _service_state = new FAILED
-              state.done
+              done
           }
         case state:STOPPED =>
           done