Author: dblevins
Date: Wed Jan 27 12:46:03 2010
New Revision: 903627
URL: http://svn.apache.org/viewvc?rev=903627&view=rev
Log:
Added RoundRobin and Random server selection strategies for selecting which
server to send a request to
Added:
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RandomConnectionStrategy.java
(with props)
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RoundRobinConnectionStrategy.java
- copied, changed from r903030,
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java
Modified:
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ConnectionManager.java
Modified:
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ConnectionManager.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ConnectionManager.java?rev=903627&r1=903626&r2=903627&view=diff
==============================================================================
---
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ConnectionManager.java
(original)
+++
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/ConnectionManager.java
Wed Jan 27 12:46:03 2010
@@ -37,7 +37,10 @@
factories.register("multicast", new MulticastConnectionFactory());
- strategies.register("default", new StickyConnectionStrategy());
+ strategies.register("sticky", new StickyConnectionStrategy());
+ strategies.register("random", new RandomConnectionStrategy());
+ strategies.register("roundrobin", new RoundRobinConnectionStrategy());
+ strategies.register("default", strategies.get("sticky"));
}
Added:
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RandomConnectionStrategy.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RandomConnectionStrategy.java?rev=903627&view=auto
==============================================================================
---
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RandomConnectionStrategy.java
(added)
+++
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RandomConnectionStrategy.java
Wed Jan 27 12:46:03 2010
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.client;
+
+import java.io.IOException;
+import java.net.URI;
+import java.rmi.RemoteException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.Set;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Random;
+
+public class RandomConnectionStrategy implements ConnectionStrategy {
+ private static final Logger LOGGER = Logger.getLogger("OpenEJB.client");
+ private final Random random = new Random();
+
+
+ public Connection connect(ClusterMetaData cluster, ServerMetaData server)
throws IOException {
+ Set<URI> failed = Client.getFailed();
+
+ URI[] locations = cluster.getLocations();
+
+ if (locations.length == 0){
+ return connect(cluster, server.getLocation());
+ }
+
+ List<URI> available = Arrays.asList(locations);
+ available.removeAll(failed);
+
+ URI lastLocation = cluster.getLastLocation();
+
+ if (available.size() > 2) available.remove(lastLocation);
+
+
+ while (available.size() > 0) {
+
+ URI uri = next(available);
+
+ try {
+ return connect(cluster, uri);
+ } catch (IOException e) {
+ failed.add(uri);
+ available.remove(uri);
+ LOGGER.log(Level.WARNING, "Random: Failover: Cannot connect to
server(s): " + uri.toString() + " Exception: " + e.getMessage()+". Trying
next.");
+ } catch (Throwable e) {
+ failed.add(uri);
+ available.remove(uri);
+ throw new RemoteException("Random: Failover: Cannot connect to
server: " + uri.toString() + " due to an unkown exception in the OpenEJB
client: ", e);
+ }
+ }
+
+ if (available.size() == 0 && !failed.contains(server.getLocation())){
+ return connect(cluster, server.getLocation());
+ }
+
+ // If no servers responded, throw an error
+ StringBuilder buffer = new StringBuilder();
+ for (int i = 0; i < locations.length; i++) {
+ URI uri = locations[i];
+ buffer.append((i != 0 ? ", " : "") + "Server #" + i + ": " + uri);
+ }
+ throw new RemoteException("Cannot connect to any servers: " +
buffer.toString());
+ }
+
+ private URI next(List<URI> available) {
+ int i = Math.abs(random.nextInt()) % available.size();
+ URI uri = available.get(i);
+ return uri;
+ }
+
+ protected Connection connect(ClusterMetaData cluster, URI uri) throws
IOException {
+ Connection connection = ConnectionManager.getConnection(uri);
+
+ // Grabbing the URI from the associated connection allows the
ConnectionFactory to
+ // employ discovery to find and connect to a server. We then attempt
to connect
+ // to the discovered server rather than repeat the discovery process
again.
+ cluster.setLastLocation(connection.getURI());
+ return connection;
+ }
+
+}
\ No newline at end of file
Propchange:
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RandomConnectionStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Copied:
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RoundRobinConnectionStrategy.java
(from r903030,
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java)
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RoundRobinConnectionStrategy.java?p2=openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RoundRobinConnectionStrategy.java&p1=openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java&r1=903030&r2=903627&rev=903627&view=diff
==============================================================================
---
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/StickyConnectionStrategy.java
(original)
+++
openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/RoundRobinConnectionStrategy.java
Wed Jan 27 12:46:03 2010
@@ -24,8 +24,9 @@
import java.util.Set;
import java.util.Arrays;
import java.util.LinkedHashSet;
+import java.util.List;
-public class StickyConnectionStrategy implements ConnectionStrategy {
+public class RoundRobinConnectionStrategy implements ConnectionStrategy {
private static final Logger LOGGER = Logger.getLogger("OpenEJB.client");
public Connection connect(ClusterMetaData cluster, ServerMetaData server)
throws IOException {
@@ -37,19 +38,27 @@
return connect(cluster, server.getLocation());
}
+
+ List<URI> list = Arrays.asList(locations);
URI lastLocation = cluster.getLastLocation();
if (null != lastLocation && !failed.contains(lastLocation)) {
try {
- return connect(cluster, lastLocation);
+ int i = list.indexOf(lastLocation) + 1;
+ if (i >= list.size()) i = 0;
+
+ URI uri = list.get(i);
+
+ return connect(cluster, uri);
} catch (IOException e) {
if (locations.length > 1){
- LOGGER.log(Level.WARNING, "Failing over. Cannot connect
to last server: " + lastLocation.toString() + " Exception: " +
e.getClass().getName() +" " + e.getMessage());
+ LOGGER.log(Level.WARNING, "RoundRobin: Failing over.
Cannot connect to next server: " + lastLocation.toString() + " Exception: " +
e.getClass().getName() +" " + e.getMessage());
}
}
}
- Set<URI> remaining = new LinkedHashSet<URI>(Arrays.asList(locations));
+ Set<URI> remaining = new LinkedHashSet<URI>(list);
+
remaining.remove(lastLocation);
remaining.removeAll(failed);
@@ -58,10 +67,10 @@
return connect(cluster, uri);
} catch (IOException e) {
failed.add(uri);
- LOGGER.log(Level.WARNING, "Failover: Cannot connect to
server(s): " + uri.toString() + " Exception: " + e.getMessage()+". Trying
next.");
+ LOGGER.log(Level.WARNING, "RoundRobin: Failover: Cannot
connect to server(s): " + uri.toString() + " Exception: " + e.getMessage()+".
Trying next.");
} catch (Throwable e) {
failed.add(uri);
- throw new RemoteException("Failover: Cannot connect to server:
" + uri.toString() + " due to an unkown exception in the OpenEJB client: ", e);
+ throw new RemoteException("RoundRobin: Failover: Cannot
connect to server: " + uri.toString() + " due to an unkown exception in the
OpenEJB client: ", e);
}
}
@@ -90,4 +99,4 @@
return connection;
}
-}
+}
\ No newline at end of file