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/07/18 14:13:25 UTC

camel git commit: CAMEL-11547: camel-core: load route definitions from registry

Repository: camel
Updated Branches:
  refs/heads/master d32c8bc3c -> 1b8c168c8


CAMEL-11547: camel-core: load route definitions from registry


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

Branch: refs/heads/master
Commit: 1b8c168c8aedd4f17179ca0d187c0df43352b9aa
Parents: d32c8bc
Author: lburgazzoli <lb...@gmail.com>
Authored: Tue Jul 18 13:44:46 2017 +0200
Committer: lburgazzoli <lb...@gmail.com>
Committed: Tue Jul 18 16:08:22 2017 +0200

----------------------------------------------------------------------
 .../apache/camel/impl/DefaultCamelContext.java  | 19 ++++
 .../camel/model/RouteDefinitionHelper.java      | 20 ++++
 .../camel/impl/RoutesFromRegistryTest.java      | 98 ++++++++++++++++++++
 .../spring/boot/RoutesFromRegistryTest.java     | 87 +++++++++++++++++
 4 files changed, 224 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1b8c168c/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 3ebad6d..bfb41be 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -229,6 +229,7 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
     private ManagementStrategy managementStrategy;
     private ManagementMBeanAssembler managementMBeanAssembler;
     private final List<RouteDefinition> routeDefinitions = new ArrayList<RouteDefinition>();
+    private final Map<String, RouteDefinition> routeDefinitionsFromRegistry = new HashMap<>();
     private final List<RestDefinition> restDefinitions = new ArrayList<RestDefinition>();
     private Map<String, RestConfiguration> restConfigurations = new ConcurrentHashMap<>();
     private Map<String, ServiceCallConfigurationDefinition> serviceCallConfigurations = new ConcurrentHashMap<>();
@@ -3304,6 +3305,24 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         // start components
         startServices(components.values());
 
+        // Remove routes definition previously found on the registry so in case
+        // context restart new routes eventually removed from the registry won't
+        // stay as zombie.
+        removeRouteDefinitions(routeDefinitionsFromRegistry.values());
+
+        Map<String, RouteDefinition> defs = getRegistry().findByTypeWithName(RouteDefinition.class);
+        if (!defs.isEmpty()) {
+            routeDefinitionsFromRegistry.putAll(defs);
+            for (Map.Entry<String, RouteDefinition> entry: defs.entrySet()) {
+                if (ObjectHelper.isEmpty(entry.getValue().getId())) {
+                    // If routes do not have an id, use the bean name
+                    entry.getValue().setId(entry.getKey());
+                }
+            }
+
+            addRouteDefinitions(defs.values());
+        }
+
         // start the route definitions before the routes is started
         startRouteDefinitions(routeDefinitions);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/1b8c168c/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
index fccc918..0e48c565 100644
--- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.EndpointHelper;
@@ -643,4 +644,23 @@ public final class RouteDefinitionHelper {
         }
     }
 
+    /**
+     * Creates a route definition from uri.
+     *
+     * @param uri the uri
+     * @return the route definition
+     */
+    public static RouteDefinition from(String uri) {
+        return new RouteDefinition().from(uri);
+    }
+
+    /**
+     * Creates a route definition from endpoint.
+     *
+     * @param endpoint the endpoint
+     * @return the route definition
+     */
+    public static RouteDefinition from(Endpoint endpoint) {
+        return new RouteDefinition().from(endpoint);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1b8c168c/camel-core/src/test/java/org/apache/camel/impl/RoutesFromRegistryTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/RoutesFromRegistryTest.java b/camel-core/src/test/java/org/apache/camel/impl/RoutesFromRegistryTest.java
new file mode 100644
index 0000000..aecaf79
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/RoutesFromRegistryTest.java
@@ -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.camel.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.Route;
+import org.apache.camel.TestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+
+import static org.apache.camel.model.RouteDefinitionHelper.from;
+
+public class RoutesFromRegistryTest extends TestSupport {
+
+    public void testRoutes() throws Exception {
+        SimpleRegistry registry = new SimpleRegistry();
+        registry.put("start", from("direct:start").to("mock:stop"));
+        registry.put("begin", from("direct:begin").to("mock:end"));
+
+        CamelContext context = new DefaultCamelContext(registry);
+
+        try {
+            context.start();
+
+            Route start = context.getRoute("start");
+            assertNotNull(start);
+            assertEquals("start", start.getId());
+
+            Route begin = context.getRoute("begin");
+            assertNotNull(begin);
+            assertEquals("begin", begin.getId());
+
+            context.getEndpoint("mock:stop", MockEndpoint.class).expectedMessageCount(1);
+            context.getEndpoint("mock:stop", MockEndpoint.class).expectedBodiesReceived("start");
+            context.getEndpoint("mock:end", MockEndpoint.class).expectedMessageCount(1);
+            context.getEndpoint("mock:end", MockEndpoint.class).expectedBodiesReceived("begin");
+
+            ProducerTemplate template = context.createProducerTemplate();
+            template.sendBody("direct:start", "start");
+            template.sendBody("direct:begin", "begin");
+
+            MockEndpoint.assertIsSatisfied(context);
+        } finally {
+            context.stop();
+        }
+    }
+
+    public void testUpdateRoutes() throws Exception {
+        SimpleRegistry registry = new SimpleRegistry();
+        registry.put("start", from("direct:start").to("mock:stop"));
+        registry.put("begin", from("direct:begin").to("mock:end"));
+
+        CamelContext context = new DefaultCamelContext(registry);
+
+        try {
+            context.start();
+
+            Route start = context.getRoute("start");
+            assertNotNull(start);
+            assertEquals("start", start.getId());
+
+            Route begin = context.getRoute("begin");
+            assertNotNull(begin);
+            assertEquals("begin", begin.getId());
+
+            context.stop();
+
+            registry.remove("start");
+            registry.put("test", from("direct:test").to("mock:test"));
+
+            context.start();
+
+            start = context.getRoute("start");
+            assertNull(start);
+
+            Route test = context.getRoute("test");
+            assertNotNull(test);
+            assertEquals("test", test.getId());
+
+        } finally {
+            context.stop();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1b8c168c/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RoutesFromRegistryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RoutesFromRegistryTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RoutesFromRegistryTest.java
new file mode 100644
index 0000000..6f7e113
--- /dev/null
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RoutesFromRegistryTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.spring.boot;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.Route;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.RouteDefinition;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.apache.camel.model.RouteDefinitionHelper.from;
+
+@DirtiesContext
+@RunWith(SpringRunner.class)
+@EnableAutoConfiguration
+@SpringBootTest(
+    classes = RoutesFromRegistryTest.TestConfiguration.class
+)
+public class RoutesFromRegistryTest {
+    @Autowired
+    private CamelContext context;
+    @Autowired
+    private ProducerTemplate template;
+
+    @Test
+    public void testRoutes() throws Exception {
+        Route start = context.getRoute("start");
+        Assert.assertNotNull(start);
+        Assert.assertEquals("start", start.getId());
+
+        Route begin = context.getRoute("begin");
+        Assert.assertNotNull(begin);
+        Assert.assertEquals("begin", begin.getId());
+
+        context.getEndpoint("mock:stop", MockEndpoint.class).expectedMessageCount(1);
+        context.getEndpoint("mock:stop", MockEndpoint.class).expectedBodiesReceived("start");
+        context.getEndpoint("mock:end", MockEndpoint.class).expectedMessageCount(1);
+        context.getEndpoint("mock:end", MockEndpoint.class).expectedBodiesReceived("begin");
+
+        template.sendBody("direct:start", "trats");
+        template.sendBody("direct:begin", "nigeb");
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Configuration
+    public static class TestConfiguration {
+        @Bean
+        public RouteDefinition start() {
+            return from("direct:start")
+                .transform()
+                    .body(String.class, b -> new StringBuilder(b).reverse().toString())
+                .to("mock:stop");
+        }
+        @Bean
+        public RouteDefinition begin() {
+            return from("direct:begin")
+                .transform()
+                    .body(String.class, b -> new StringBuilder(b).reverse().toString())
+                .to("mock:end");
+        }
+    }
+}