You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2012/01/18 23:06:25 UTC

svn commit: r1233078 - in /cxf/branches/2.4.x-fixes: ./ rt/core/src/main/java/org/apache/cxf/clustering/RetryStrategy.java systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverTest.java

Author: sergeyb
Date: Wed Jan 18 22:06:25 2012
New Revision: 1233078

URL: http://svn.apache.org/viewvc?rev=1233078&view=rev
Log:
Merged revisions 1233076 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes

................
  r1233076 | sergeyb | 2012-01-18 21:54:29 +0000 (Wed, 18 Jan 2012) | 9 lines
  
  Merged revisions 1233075 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/trunk
  
  ........
    r1233075 | sergeyb | 2012-01-18 21:49:31 +0000 (Wed, 18 Jan 2012) | 1 line
    
    [CXF-3596] Minor fix to RetryStrategy
  ........
................

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/clustering/RetryStrategy.java
    cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Wed Jan 18 22:06:25 2012
@@ -0,0 +1,2 @@
+/cxf/branches/2.5.x-fixes:1233076
+/cxf/trunk:1233075

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/clustering/RetryStrategy.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/clustering/RetryStrategy.java?rev=1233078&r1=1233077&r2=1233078&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/clustering/RetryStrategy.java (original)
+++ cxf/branches/2.4.x-fixes/rt/core/src/main/java/org/apache/cxf/clustering/RetryStrategy.java Wed Jan 18 22:06:25 2012
@@ -43,6 +43,10 @@ public class RetryStrategy extends Seque
     public List<Endpoint> getAlternateEndpoints(Exchange exchange) {
         return getEndpoints(exchange, stillTheSameAddress());
     }
+    
+    protected <T> T getNextAlternate(List<T> alternates) {
+        return stillTheSameAddress() ? alternates.get(0) : alternates.remove(0);
+    }
 
     protected boolean stillTheSameAddress() {
         if (maxNumberOfRetries == 0) {
@@ -50,15 +54,12 @@ public class RetryStrategy extends Seque
         }
         // let the target selector move to the next address
         // and then stay on the same address for maxNumberOfRetries
-        synchronized (this) {
-            if (++counter <= maxNumberOfRetries) {
-                return true;    
-            } else {
-                counter = 0;
-                return false;
-            }
+        if (++counter <= maxNumberOfRetries) {
+            return true;    
+        } else {
+            counter = 0;
+            return false;
         }
-        
     }
     
 

Modified: cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverTest.java?rev=1233078&r1=1233077&r2=1233078&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverTest.java (original)
+++ cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/FailoverTest.java Wed Jan 18 22:06:25 2012
@@ -20,12 +20,15 @@
 package org.apache.cxf.systest.jaxrs.failover;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.ws.rs.core.Response;
 
 import org.apache.cxf.clustering.FailoverTargetSelector;
 import org.apache.cxf.clustering.RandomStrategy;
+import org.apache.cxf.clustering.RetryStrategy;
 import org.apache.cxf.clustering.SequentialStrategy;
 import org.apache.cxf.endpoint.ConduitSelector;
 import org.apache.cxf.feature.AbstractFeature;
@@ -153,6 +156,32 @@ public class FailoverTest extends Abstra
             getFeature(false, false, "http://localhost:8182/non-existent"); 
         strategyTest(Server.ADDRESS1, feature, null, null, false, false, false);
     }
+    
+    @Test
+    public void testSequentialStrategyWithRetries() throws Exception {
+        String address = "http://localhost:8182/non-existent";
+        String address2 = "http://localhost:8182/non-existent2";
+        
+        FailoverFeature feature = new FailoverFeature();
+        List<String> alternateAddresses = new ArrayList<String>();
+        alternateAddresses.add(address);
+        alternateAddresses.add(address2);
+        CustomRetryStrategy strategy = new CustomRetryStrategy();
+        strategy.setMaxNumberOfRetries(5);
+        strategy.setAlternateAddresses(alternateAddresses);
+        feature.setStrategy(strategy);
+            
+        BookStore store = getBookStore(address, feature);
+        try {
+            store.getBook("1");
+            fail("Exception expected");
+        } catch (ClientWebApplicationException ex) {
+            assertEquals(10, strategy.getTotalCount());
+            assertEquals(5, strategy.getAddressCount(address));
+            assertEquals(5, strategy.getAddressCount(address2));
+        }
+    }
+    
 
     private FailoverFeature getFeature(boolean custom, boolean random, String ...address) {
         FailoverFeature feature = new FailoverFeature();
@@ -342,4 +371,30 @@ public class FailoverTest extends Abstra
             return false;
         }
     }
+    
+    private static class CustomRetryStrategy extends RetryStrategy {
+        private int totalCount;
+        private Map<String, Integer> map = new HashMap<String, Integer>(); 
+        @Override
+        protected <T> T getNextAlternate(List<T> alternates) {
+            totalCount++;
+            T next = super.getNextAlternate(alternates);
+            String address = (String)next;
+            Integer count = map.get(address);
+            if (count == null) {
+                count = 0; 
+            }
+            count++;
+            map.put(address, count);
+            return next;
+        }
+        
+        public int getTotalCount() {
+            return totalCount - 2;
+        }
+        
+        public int getAddressCount(String address) {
+            return map.get(address) - 1;
+        }
+    }
 }