You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/08/11 14:05:49 UTC

[camel] branch master updated (21fb273 -> ca7f8ea)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from 21fb273  CAMEL-15389: camel-main - Add docs about camel.beans
     new 4ff0019  CAMEL-14297: Introduce RouteBuilderConfigurer
     new ca7f8ea  CAMEL-14297: Introduce RouteBuilderConfigurer

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/camel/builder/RouteBuilder.java     |  2 +-
 .../camel/builder/RouteBuilderConfigurer.java}     | 17 ++++++---
 ...ckTest.java => RouteBuilderConfigurerTest.java} | 42 ++++++++++++++--------
 .../apache/camel/main/DefaultRoutesCollector.java  | 14 ++++++++
 ...ectorTest.java => MainRouteConfigurerTest.java} | 14 +++-----
 .../apache/camel/main/MainRoutesCollectorTest.java |  1 +
 6 files changed, 61 insertions(+), 29 deletions(-)
 copy core/{camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java => camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java} (63%)
 copy core/camel-core/src/test/java/org/apache/camel/processor/{SimpleMockTest.java => RouteBuilderConfigurerTest.java} (67%)
 copy core/camel-main/src/test/java/org/apache/camel/main/{MainRoutesCollectorTest.java => MainRouteConfigurerTest.java} (83%)


[camel] 02/02: CAMEL-14297: Introduce RouteBuilderConfigurer

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit ca7f8ea52f4706e1a6bcbcb2a0031844f05e5ba5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 11 16:05:05 2020 +0200

    CAMEL-14297: Introduce RouteBuilderConfigurer
---
 .../java/org/apache/camel/main/DefaultRoutesCollector.java | 14 ++++++++++++++
 ...utesCollectorTest.java => MainRouteConfigurerTest.java} | 14 ++++----------
 .../org/apache/camel/main/MainRoutesCollectorTest.java     |  1 +
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
index 65fbdef..ebe5444 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultRoutesCollector.java
@@ -27,6 +27,8 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteBuilderConfigurer;
 import org.apache.camel.model.RouteTemplatesDefinition;
 import org.apache.camel.model.RoutesDefinition;
 import org.apache.camel.model.rest.RestsDefinition;
@@ -51,6 +53,18 @@ public class DefaultRoutesCollector implements RoutesCollector {
         final List<RoutesBuilder> routes = new ArrayList<>();
 
         final AntPathMatcher matcher = new AntPathMatcher();
+
+        Set<RouteBuilderConfigurer> configurers = camelContext.getRegistry().findByType(RouteBuilderConfigurer.class);
+        for (RouteBuilderConfigurer configurer : configurers) {
+            RouteBuilder rb = new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    configurer.accept(this);
+                }
+            };
+            routes.add(rb);
+        }
+
         Set<RoutesBuilder> builders = camelContext.getRegistry().findByType(RoutesBuilder.class);
         for (RoutesBuilder routesBuilder : builders) {
             // filter out abstract classes
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainRouteConfigurerTest.java
similarity index 83%
copy from core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java
copy to core/camel-main/src/test/java/org/apache/camel/main/MainRouteConfigurerTest.java
index f1edae9..99d425e 100644
--- a/core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainRouteConfigurerTest.java
@@ -17,18 +17,19 @@
 package org.apache.camel.main;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteBuilderConfigurer;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
-public class MainRoutesCollectorTest {
+
+public class MainRouteConfigurerTest {
 
     @Test
     public void testMainRoutesCollector() throws Exception {
         Main main = new Main();
-        main.bind("myBarRoute", new MyRouteBuilder());
+        main.bind("myBarRoute", (RouteBuilderConfigurer) rb -> rb.from("direct:start").to("mock:results"));
         main.start();
 
         CamelContext camelContext = main.getCamelContext();
@@ -45,11 +46,4 @@ public class MainRoutesCollectorTest {
         main.stop();
     }
 
-    public static class MyRouteBuilder extends RouteBuilder {
-
-        @Override
-        public void configure() throws Exception {
-            from("direct:start").to("mock:results");
-        }
-    }
 }
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java
index f1edae9..b7a4009 100644
--- a/core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainRoutesCollectorTest.java
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+
 public class MainRoutesCollectorTest {
 
     @Test


[camel] 01/02: CAMEL-14297: Introduce RouteBuilderConfigurer

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 4ff0019ee4dc66ee4fd1e001c2cfbdccaebe79c7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 11 14:38:40 2020 +0200

    CAMEL-14297: Introduce RouteBuilderConfigurer
---
 .../org/apache/camel/builder/RouteBuilder.java     |  2 +-
 .../camel/builder/RouteBuilderConfigurer.java      | 36 ++++++++++
 .../processor/RouteBuilderConfigurerTest.java      | 76 ++++++++++++++++++++++
 3 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java
index c30c0e6..8e608f1 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java
@@ -101,7 +101,7 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild
      *            to create routes
      * @throws Exception if an error occurs
      */
-    public static void addRoutes(CamelContext context, ThrowingConsumer<RouteBuilder, Exception> rbc) throws Exception {
+    public static void addRoutes(CamelContext context, RouteBuilderConfigurer rbc) throws Exception {
         context.addRoutes(new RouteBuilder(context) {
             @Override
             public void configure() throws Exception {
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java
new file mode 100644
index 0000000..05ac752
--- /dev/null
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java
@@ -0,0 +1,36 @@
+/*
+ * 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.builder;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.util.function.ThrowingConsumer;
+
+/**
+ * Functional interface for adding routes to a context using a lambda expression. It can be used as
+ * following:
+ *
+ * <pre>
+ * RouteBuilder.addRoutes(context, rb ->
+ *     rb.from("direct:inbound").bean(ProduceTemplateBean.class)));
+ * </pre>
+ *
+ * @see RouteBuilder#addRoutes(CamelContext, RouteBuilderConfigurer)
+ */
+@FunctionalInterface
+public interface RouteBuilderConfigurer extends ThrowingConsumer<RouteBuilder, Exception> {
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RouteBuilderConfigurerTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RouteBuilderConfigurerTest.java
new file mode 100644
index 0000000..37cf971
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/RouteBuilderConfigurerTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteBuilderConfigurer;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RouteBuilderConfigurerTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testRouteBuilderConfigurer() throws Exception {
+        assertEquals(0, context.getRoutesSize());
+
+        RouteBuilderConfigurer builder = rb -> rb.from("direct:start").to("mock:result");
+        context.addRoutes(new RouteBuilder(context) {
+            @Override
+            public void configure() throws Exception {
+                builder.accept(this);
+            }
+        });
+        context.start();
+
+        assertEquals(1, context.getRoutesSize());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testRouteBuilderConfigurerLambda() throws Exception {
+        assertEquals(0, context.getRoutesSize());
+
+        RouteBuilder.addRoutes(context, rb -> rb.from("direct:start").to("mock:result"));
+
+        context.start();
+
+        assertEquals(1, context.getRoutesSize());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}