servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r1170661 - in /servicemix/components/trunk/shared-libraries/servicemix-common/src: main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java test/java/org/apache/servicemix/common/AsyncBaseLifeCycleTest.java
Date Wed, 14 Sep 2011 14:51:49 GMT
Author: gertv
Date: Wed Sep 14 14:51:48 2011
New Revision: 1170661

URL: http://svn.apache.org/viewvc?rev=1170661&view=rev
Log:
SMXCOMP-903: Fix AsyncBaseLifeCycle's prepareShutdown method

Modified:
    servicemix/components/trunk/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
    servicemix/components/trunk/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/AsyncBaseLifeCycleTest.java

Modified: servicemix/components/trunk/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
URL: http://svn.apache.org/viewvc/servicemix/components/trunk/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java?rev=1170661&r1=1170660&r2=1170661&view=diff
==============================================================================
--- servicemix/components/trunk/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
(original)
+++ servicemix/components/trunk/shared-libraries/servicemix-common/src/main/java/org/apache/servicemix/common/AsyncBaseLifeCycle.java
Wed Sep 14 14:51:48 2011
@@ -696,14 +696,23 @@ public class AsyncBaseLifeCycle implemen
      */
     public void prepareShutdown(Endpoint endpoint, long timeout) throws InterruptedException
{
         Set<String> exchanges = getKnownExchanges(endpoint);
+        long start = System.currentTimeMillis();
+        long interval = timeout / 3;         // if a timeout has been set, we'll check 3
times within the timeout period
+
         synchronized (exchanges) {
-            if (!exchanges.isEmpty()) {
+            while (!exchanges.isEmpty()) {
                 for (String id : exchanges) {
                     logger.debug("Waiting for exchange {} in {}", id, endpoint);
                 }
-                exchanges.wait(timeout);
-                logger.debug(String.format("Gave up waiting for %s exchanges in %s after
%s ms",
-                        exchanges.size(), endpoint, timeout));
+                exchanges.wait(interval);
+
+                // if a timeout has been set, this would be a good time to check that
+                long delta = System.currentTimeMillis() - start;
+                if (timeout != 0 && delta >= timeout) {
+                    logger.debug(String.format("Gave up waiting for %s exchanges in %s after
%s ms",
+                                               exchanges.size(), endpoint, delta));
+                    break;
+                }
             }
         }
     }

Modified: servicemix/components/trunk/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/AsyncBaseLifeCycleTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/trunk/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/AsyncBaseLifeCycleTest.java?rev=1170661&r1=1170660&r2=1170661&view=diff
==============================================================================
--- servicemix/components/trunk/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/AsyncBaseLifeCycleTest.java
(original)
+++ servicemix/components/trunk/shared-libraries/servicemix-common/src/test/java/org/apache/servicemix/common/AsyncBaseLifeCycleTest.java
Wed Sep 14 14:51:48 2011
@@ -57,11 +57,12 @@ public class AsyncBaseLifeCycleTest exte
     public void testPrepareShutdown() throws InterruptedException {
         final Endpoint endpoint = new MockEndpoint() ;
 
-        MockMessageExchange exchange = new MockMessageExchange();
-        exchange.setExchangeId(UUID.randomUUID().toString());
+        MockMessageExchange exchange1 = createMockExchange();
+        MockMessageExchange exchange2 = createMockExchange();
 
-        // adding a known exchange
-        lifecycle.handleExchange(endpoint, exchange, true);
+        // adding 2 known exchanges
+        lifecycle.handleExchange(endpoint, exchange1, true);
+        lifecycle.handleExchange(endpoint, exchange2, true);
 
         final CountDownLatch done = new CountDownLatch(1);
 
@@ -76,14 +77,25 @@ public class AsyncBaseLifeCycleTest exte
             }
         });
 
-        assertFalse("Should be waiting for prepareShutdown to complete",
+        assertFalse("Should be waiting for prepareShutdown to complete (2 exchanges pending)",
                     done.await(1, TimeUnit.SECONDS));
 
-        lifecycle.handleExchange(endpoint, exchange, false);
+        lifecycle.handleExchange(endpoint, exchange1, false);
+
+        assertFalse("Should be waiting for prepareShutdown to complete (1 exchange pending)",
+                    done.await(1, TimeUnit.SECONDS));
+
+        lifecycle.handleExchange(endpoint, exchange2, false);
 
         assertTrue("prepareShutdown is now done", done.await(100, TimeUnit.SECONDS));
     }
 
+    private MockMessageExchange createMockExchange() {
+        MockMessageExchange exchange = new MockMessageExchange();
+        exchange.setExchangeId(UUID.randomUUID().toString());
+        return exchange;
+    }
+
     public void testPrepareShutdownWithTimeout() throws InterruptedException, ExecutionException,
TimeoutException {
         final Endpoint endpoint = new MockEndpoint() ;
 



Mime
View raw message