You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2017/06/16 15:39:08 UTC

[07/13] camel git commit: CAMEL-11362: create a LeaderElectionservice

CAMEL-11362: create a LeaderElectionservice


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/abb0ef47
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/abb0ef47
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/abb0ef47

Branch: refs/heads/master
Commit: abb0ef47a28f19e808bcb595d4c592661a1f4727
Parents: 14f88f2
Author: lburgazzoli <lb...@gmail.com>
Authored: Fri Jun 2 08:12:58 2017 +0200
Committer: lburgazzoli <lb...@gmail.com>
Committed: Fri Jun 16 17:37:54 2017 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/ha/CamelCluster.java  |  6 +--
 .../apache/camel/ha/CamelClusterService.java    | 31 -----------
 .../camel/impl/ha/AbstractCamelCluster.java     | 16 ++++--
 .../camel/impl/ha/ClusteredRoutePolicy.java     | 11 +++-
 .../impl/ha/ClusteredRoutePolicyFactory.java    | 55 ++++++++++++++++++++
 5 files changed, 81 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/abb0ef47/camel-core/src/main/java/org/apache/camel/ha/CamelCluster.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/ha/CamelCluster.java b/camel-core/src/main/java/org/apache/camel/ha/CamelCluster.java
index d6c151c..a0a797f 100644
--- a/camel-core/src/main/java/org/apache/camel/ha/CamelCluster.java
+++ b/camel-core/src/main/java/org/apache/camel/ha/CamelCluster.java
@@ -22,11 +22,11 @@ import org.apache.camel.spi.HasId;
 
 public interface CamelCluster extends Service, CamelContextAware, HasId {
     /**
-     * Creates a view of the cluster bound to a namespace.
+     * Get a view of the cluster bound to a namespace creating it if needed.
      *
      * @param namespace the namespace the view refer to.
-     * @return the cluster view.
+     * @return the view.
      * @throws Exception if the view can't be created.
      */
-    CamelClusterView createView(String namespace) throws Exception;
+    CamelClusterView getView(String namespace) throws Exception;
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/abb0ef47/camel-core/src/main/java/org/apache/camel/ha/CamelClusterService.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/ha/CamelClusterService.java b/camel-core/src/main/java/org/apache/camel/ha/CamelClusterService.java
deleted file mode 100644
index 004eab3..0000000
--- a/camel-core/src/main/java/org/apache/camel/ha/CamelClusterService.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- * 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.camel.ha;
-
-import org.apache.camel.Service;
-
-public interface CamelClusterService extends Service {
-    /**
-     * Get the {@linke CamelCluster} instance managed by the service
-     */
-    CamelCluster getCluster() throws Exception;
-
-    /**
-     * Create a {@linke CamelClusterView} for the given namespace
-     */
-    CamelClusterView createView(String namespace) throws Exception;
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/abb0ef47/camel-core/src/main/java/org/apache/camel/impl/ha/AbstractCamelCluster.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/ha/AbstractCamelCluster.java b/camel-core/src/main/java/org/apache/camel/impl/ha/AbstractCamelCluster.java
index 9c4691e..16a869b 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/ha/AbstractCamelCluster.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/ha/AbstractCamelCluster.java
@@ -50,6 +50,16 @@ public abstract class AbstractCamelCluster<T extends CamelClusterView> extends S
     @Override
     public void setCamelContext(CamelContext camelContext) {
         this.camelContext = camelContext;
+
+        long stamp = lock.writeLock();
+
+        try {
+            for (T view : views.values()) {
+                view.setCamelContext(camelContext);
+            }
+        } finally {
+            lock.unlockWrite(stamp);
+        }
     }
 
     @Override
@@ -84,14 +94,14 @@ public abstract class AbstractCamelCluster<T extends CamelClusterView> extends S
     }
 
     @Override
-    public CamelClusterView createView(String namespace) throws Exception {
+    public CamelClusterView getView(String namespace) throws Exception {
         long stamp = lock.writeLock();
 
         try {
             T view = views.get(namespace);
 
             if (view == null) {
-                view = doCreateView(namespace);
+                view = createView(namespace);
                 view.setCamelContext(this.camelContext);
 
                 views.put(namespace, view);
@@ -111,5 +121,5 @@ public abstract class AbstractCamelCluster<T extends CamelClusterView> extends S
     // Implementation
     // **********************************
 
-    protected abstract T doCreateView(String namespace) throws Exception;
+    protected abstract T createView(String namespace) throws Exception;
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/abb0ef47/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicy.java b/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicy.java
index b08cddb..74ca781 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicy.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicy.java
@@ -246,8 +246,17 @@ public final class ClusteredRoutePolicy extends RoutePolicySupport implements Ca
     // Static helpers
     // ****************************************************
 
+    public static ClusteredRoutePolicy forNamespace(CamelContext camelContext, String namespace) throws Exception {
+        CamelCluster cluster = camelContext.hasService(CamelCluster.class);
+        if (cluster == null) {
+            throw new IllegalStateException("CamelCluster service not found");
+        }
+
+        return forNamespace(cluster, namespace);
+    }
+
     public static ClusteredRoutePolicy forNamespace(CamelCluster cluster, String namespace) throws Exception {
-        return forView(cluster.createView(namespace));
+        return forView(cluster.getView(namespace));
     }
 
     public static ClusteredRoutePolicy forView(CamelClusterView view) throws Exception  {

http://git-wip-us.apache.org/repos/asf/camel/blob/abb0ef47/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicyFactory.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicyFactory.java b/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicyFactory.java
new file mode 100644
index 0000000..da07706
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/impl/ha/ClusteredRoutePolicyFactory.java
@@ -0,0 +1,55 @@
+/**
+ * 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.camel.impl.ha;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.ha.CamelCluster;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.spi.RoutePolicy;
+import org.apache.camel.spi.RoutePolicyFactory;
+import org.apache.camel.util.ObjectHelper;
+
+public class ClusteredRoutePolicyFactory implements RoutePolicyFactory {
+    private final String namespace;
+
+    public ClusteredRoutePolicyFactory(String viewName) {
+        this.namespace = ObjectHelper.notNull(viewName, "Cluster View Namespace");
+    }
+
+    @Override
+    public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, RouteDefinition route) {
+        try {
+            CamelCluster cluster = camelContext.hasService(CamelCluster.class);
+            if (cluster == null) {
+                throw new IllegalStateException("CamelCluster service not found");
+            }
+
+            return ClusteredRoutePolicy.forNamespace(cluster, namespace);
+        } catch (Exception e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
+
+    // ****************************************************
+    // Static helpers
+    // ****************************************************
+
+    public static ClusteredRoutePolicyFactory forNamespace(String namespace) {
+        return new ClusteredRoutePolicyFactory(namespace);
+    }
+}