This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-examples.git
The following commit(s) were added to refs/heads/main by this push: new 6f31a5e CAMEL-11834: Add tests for basic/cdi/kotlin/loadbalancing/main-artemis/routeloader/routes-configuration/routetemplate (#62) 6f31a5e is described below commit 6f31a5ee7eefa0994f9cd017512bdfd59c715fa0 Author: Nicolas Filotto <essob...@users.noreply.github.com> AuthorDate: Fri Jan 28 16:15:06 2022 +0100 CAMEL-11834: Add tests for basic/cdi/kotlin/loadbalancing/main-artemis/routeloader/routes-configuration/routetemplate (#62) --- .gitignore | 3 -- .../src/main/resources/application.properties | 2 + .../activemq-tomcat/src/main/resources/broker.xml | 13 +++-- .../org/apache/camel/example/ActiveMQTomcatIT.java | 1 + .../src/test/resources/arquillian.xml | 13 +++++ .../src/test/resources/test-application.properties | 2 + .../camel/example/ArtemisLargeMessageTest.java | 3 ++ .../apache/camel/example/artemis/ArtemisTest.java | 3 ++ examples/basic/pom.xml | 7 ++- .../org/apache/camel/example/basic/CamelBasic.java | 21 ++++---- .../apache/camel/example/basic/CamelBasicTest.java | 46 ++++++++++++++++ examples/cdi/pom.xml | 7 ++- .../org/apache/camel/example/cdi/CamelCdiTest.java | 48 +++++++++++++++++ examples/kotlin/README.adoc | 22 ++++---- examples/kotlin/pom.xml | 14 ++++- .../kotlin/org/apache/camel/example/KotlinTest.kt | 48 +++++++++++++++++ examples/loadbalancing/README.adoc | 5 +- examples/loadbalancing/pom.xml | 7 ++- .../META-INF/spring/camel-context-loadbalancer.xml | 27 +++------- .../META-INF/spring/camel-context-mina1.xml | 17 +++--- .../META-INF/spring/camel-context-mina2.xml | 16 +++--- .../spring/camel-route-context-loadbalancer.xml | 28 ++++++++++ .../META-INF/spring/camel-route-context-mina1.xml | 18 +++++++ .../META-INF/spring/camel-route-context-mina2.xml | 18 +++++++ .../apache/camel/example/LoadBalancingTest.java | 56 +++++++++++++++++++ .../apache/camel/example/test-camel-context.xml} | 21 ++++---- examples/main-artemis/README.adoc | 11 +++- examples/main-artemis/pom.xml | 13 ++++- .../org/apache/camel/example/MyRouteBuilder.java | 2 +- .../src/main/resources/application.properties | 5 +- .../org/apache/camel/example/MainArtemisTest.java} | 41 +++++++++----- examples/routeloader/README.adoc | 10 +++- examples/routeloader/pom.xml | 7 ++- .../org/apache/camel/example/RouteLoaderTest.java | 59 ++++++++++++++++++++ examples/routes-configuration/README.adoc | 7 +++ examples/routes-configuration/pom.xml | 7 ++- .../camel/example/RoutesConfigurationTest.java | 63 ++++++++++++++++++++++ examples/routetemplate/README.adoc | 8 +++ examples/routetemplate/pom.xml | 7 ++- .../routetemplate/src/main/data/foo.properties | 18 ------- .../org/apache/camel/example/MyApplication.java | 2 +- .../src/main/resources/application.properties | 5 +- .../apache/camel/example/RouteTemplateTest.java | 48 +++++++++++++++++ 43 files changed, 651 insertions(+), 128 deletions(-) diff --git a/.gitignore b/.gitignore index 194a727..5d69a55 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,3 @@ components/camel-cxf/activemq-data *.swp .flattened-pom.xml .java-version -examples/activemq-tomcat/activemq-data -examples/artemis-large-messages/data -examples/artemis/data diff --git a/examples/activemq-tomcat/src/main/resources/application.properties b/examples/activemq-tomcat/src/main/resources/application.properties new file mode 100644 index 0000000..42e9aaa --- /dev/null +++ b/examples/activemq-tomcat/src/main/resources/application.properties @@ -0,0 +1,2 @@ +# The data directory of the active MQ +activemq.data.dir=activemq-data \ No newline at end of file diff --git a/examples/activemq-tomcat/src/main/resources/broker.xml b/examples/activemq-tomcat/src/main/resources/broker.xml index 63accf3..08f1719 100644 --- a/examples/activemq-tomcat/src/main/resources/broker.xml +++ b/examples/activemq-tomcat/src/main/resources/broker.xml @@ -21,18 +21,17 @@ <!-- this is a spring XML file where we have ActiveMQ Broker embedded --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:broker="http://activemq.apache.org/schema/core" xsi:schemaLocation=" http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- create an ActiveMQ broker --> - <!-- do not use the shutdown hook as it would cause the broker to shutdown when you press ctrl + c, + <!-- do not use the shutdown hook as it would cause the broker to shut down when you press ctrl + c, instead we will let Spring shutdown the broker --> <!-- notice this is a basic AMQ broker configuration, for production usage there is many more options you may need to configure accordingly to your needs --> <broker id="broker" brokerName="myBroker" useShutdownHook="false" useJmx="true" - persistent="true" dataDirectory="activemq-data" + persistent="true" dataDirectory="${activemq.data.dir}" xmlns="http://activemq.apache.org/schema/core"> <transportConnectors> @@ -44,5 +43,13 @@ </broker> + <!-- Allow using placeholders --> + <bean id="placeholder" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> + <property name="locations"> + <list> + <value>classpath:application.properties</value> + </list> + </property> + </bean> </beans> <!-- END SNIPPET: e1 --> \ No newline at end of file diff --git a/examples/activemq-tomcat/src/test/java/org/apache/camel/example/ActiveMQTomcatIT.java b/examples/activemq-tomcat/src/test/java/org/apache/camel/example/ActiveMQTomcatIT.java index b496941..47d8183 100644 --- a/examples/activemq-tomcat/src/test/java/org/apache/camel/example/ActiveMQTomcatIT.java +++ b/examples/activemq-tomcat/src/test/java/org/apache/camel/example/ActiveMQTomcatIT.java @@ -48,6 +48,7 @@ public class ActiveMQTomcatIT { .addAsResource("log4j2.properties") .addAsResource("broker.xml") .addAsResource("camel-config.xml") + .addAsResource("test-application.properties", "application.properties") .addAsLibraries( Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve() .withTransitivity().asFile() diff --git a/examples/activemq-tomcat/src/test/resources/arquillian.xml b/examples/activemq-tomcat/src/test/resources/arquillian.xml new file mode 100644 index 0000000..d764c79 --- /dev/null +++ b/examples/activemq-tomcat/src/test/resources/arquillian.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian" + xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> + + <container qualifier="tomcat" default="true"> + <configuration> + <property name="tomcatHome">target/tomcat-embedded-9</property> + <property name="workDir">work</property> + <property name="bindHttpPort">8888</property> + <property name="unpackArchive">true</property> + </configuration> + </container> +</arquillian> \ No newline at end of file diff --git a/examples/activemq-tomcat/src/test/resources/test-application.properties b/examples/activemq-tomcat/src/test/resources/test-application.properties new file mode 100644 index 0000000..b6df58b --- /dev/null +++ b/examples/activemq-tomcat/src/test/resources/test-application.properties @@ -0,0 +1,2 @@ +# The data directory of the active MQ +activemq.data.dir=target/activemq-data \ No newline at end of file diff --git a/examples/artemis-large-messages/src/test/java/org/apache/camel/example/ArtemisLargeMessageTest.java b/examples/artemis-large-messages/src/test/java/org/apache/camel/example/ArtemisLargeMessageTest.java index 1e2b7b8..cea6332 100644 --- a/examples/artemis-large-messages/src/test/java/org/apache/camel/example/ArtemisLargeMessageTest.java +++ b/examples/artemis-large-messages/src/test/java/org/apache/camel/example/ArtemisLargeMessageTest.java @@ -53,6 +53,9 @@ class ArtemisLargeMessageTest { static void init() throws Exception { Configuration config = new ConfigurationImpl(); config.addAcceptorConfiguration("tcp", "tcp://localhost:61616"); + config.setJournalDirectory("target/artemis-data/journal"); + config.setBindingsDirectory("target/artemis-data/bindings"); + config.setLargeMessagesDirectory("target/artemis-data/largemessages"); config.setSecurityEnabled(false); SERVER = new ActiveMQServerImpl(config); SERVER.start(); diff --git a/examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java b/examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java index 4e08de4..033c53e 100644 --- a/examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java +++ b/examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java @@ -45,6 +45,9 @@ class ArtemisTest extends CamelTestSupport { static void init() throws Exception { Configuration config = new ConfigurationImpl(); config.addAcceptorConfiguration("tcp", "tcp://localhost:61616"); + config.setJournalDirectory("target/artemis-data/journal"); + config.setBindingsDirectory("target/artemis-data/bindings"); + config.setLargeMessagesDirectory("target/artemis-data/largemessages"); config.setSecurityEnabled(false); SERVER = new ActiveMQServerImpl(config); SERVER.start(); diff --git a/examples/basic/pom.xml b/examples/basic/pom.xml index 6ee5403..46f82ff 100644 --- a/examples/basic/pom.xml +++ b/examples/basic/pom.xml @@ -75,7 +75,12 @@ <scope>runtime</scope> <version>${log4j2-version}</version> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/examples/basic/src/main/java/org/apache/camel/example/basic/CamelBasic.java b/examples/basic/src/main/java/org/apache/camel/example/basic/CamelBasic.java index da79cc9..22f829b 100644 --- a/examples/basic/src/main/java/org/apache/camel/example/basic/CamelBasic.java +++ b/examples/basic/src/main/java/org/apache/camel/example/basic/CamelBasic.java @@ -31,22 +31,23 @@ public final class CamelBasic { // add routes which can be inlined as anonymous inner class // (to keep all code in a single java file for this basic example) - camel.addRoutes(new RouteBuilder() { - @Override - public void configure() { - from("timer:foo") - .log("Hello Camel"); - } - }); + camel.addRoutes(createBasicRoute()); // start is not blocking camel.start(); // so run for 10 seconds Thread.sleep(10_000); - - // and then stop nicely - camel.stop(); } } + + static RouteBuilder createBasicRoute() { + return new RouteBuilder() { + @Override + public void configure() { + from("timer:foo") + .log("Hello Camel"); + } + }; + } } diff --git a/examples/basic/src/test/java/org/apache/camel/example/basic/CamelBasicTest.java b/examples/basic/src/test/java/org/apache/camel/example/basic/CamelBasicTest.java new file mode 100644 index 0000000..e53340b --- /dev/null +++ b/examples/basic/src/test/java/org/apache/camel/example/basic/CamelBasicTest.java @@ -0,0 +1,46 @@ +/* + * 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.example.basic; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.TimeUnit; + +import static org.apache.camel.example.basic.CamelBasic.createBasicRoute; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A unit test checking that Camel can be launched in standalone mode. + */ +class CamelBasicTest extends CamelTestSupport { + + @Test + void should_support_standalone_mode() { + NotifyBuilder notify = new NotifyBuilder(context).whenCompleted(1).create(); + assertTrue( + notify.matches(20, TimeUnit.SECONDS), "1 message should be completed" + ); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return createBasicRoute(); + } +} diff --git a/examples/cdi/pom.xml b/examples/cdi/pom.xml index 8a27e10..6db914a 100644 --- a/examples/cdi/pom.xml +++ b/examples/cdi/pom.xml @@ -69,7 +69,12 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-cdi</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java b/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java new file mode 100644 index 0000000..b914428 --- /dev/null +++ b/examples/cdi/src/test/java/org/apache/camel/example/cdi/CamelCdiTest.java @@ -0,0 +1,48 @@ +/* + * 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.example.cdi; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.test.cdi.CamelCdiRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertTrue; + +/** + * A unit test checking that Camel can be configured using CDI. + */ +@RunWith(CamelCdiRunner.class) +public class CamelCdiTest { + + @Inject + CamelContext context; + + @Test + public void should_be_configured_using_cdi() { + NotifyBuilder notify = new NotifyBuilder(context).whenCompleted(1) + .whenAllDoneMatches(exchange -> exchange.getIn().getBody(String.class).startsWith("Saying Hello World")) + .create(); + assertTrue( + "1 message should be completed", notify.matches(20, TimeUnit.SECONDS) + ); + } +} diff --git a/examples/kotlin/README.adoc b/examples/kotlin/README.adoc index d715e14..327af3e 100644 --- a/examples/kotlin/README.adoc +++ b/examples/kotlin/README.adoc @@ -6,19 +6,23 @@ little Camel route. The Camel route listen on HTTP port 8080 and return back a constant response. -=== How to run +=== Build -To build this project use +You will need to compile this example first: -.... -$ mvn install -.... +[source,sh] +---- +$ mvn compile +---- -To run this project +=== Run -.... -$ mvn exec:java -.... +To run the example, type: + +[source,sh] +---- +$ mvn camel:run +---- You can then open the following url from a web browser: http://localhost:8080 diff --git a/examples/kotlin/pom.xml b/examples/kotlin/pom.xml index e5a5dd1..0c5c57e 100644 --- a/examples/kotlin/pom.xml +++ b/examples/kotlin/pom.xml @@ -90,11 +90,23 @@ <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2-version}</version> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.rest-assured</groupId> + <artifactId>kotlin-extensions</artifactId> + <version>${rest-assured-version}</version> + <scope>test</scope> + </dependency> </dependencies> <build> <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> + <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> <defaultGoal>install</defaultGoal> <plugins> diff --git a/examples/kotlin/src/test/kotlin/org/apache/camel/example/KotlinTest.kt b/examples/kotlin/src/test/kotlin/org/apache/camel/example/KotlinTest.kt new file mode 100644 index 0000000..85510b4 --- /dev/null +++ b/examples/kotlin/src/test/kotlin/org/apache/camel/example/KotlinTest.kt @@ -0,0 +1,48 @@ +/* + * 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.example + +import io.restassured.module.kotlin.extensions.Given +import io.restassured.module.kotlin.extensions.Then +import io.restassured.module.kotlin.extensions.When +import org.apache.camel.RoutesBuilder +import org.apache.camel.test.junit5.CamelTestSupport +import org.apache.http.HttpStatus +import org.hamcrest.Matchers.equalTo +import org.junit.jupiter.api.Test + +/** + * A unit test checking that Camel supports Kotlin. + */ +class KotlinTest: CamelTestSupport() { + + @Test + fun `should support kotlin`() { + Given { + baseUri("http://localhost:8080") + } When { + get("/") + } Then { + statusCode(HttpStatus.SC_OK) + body(equalTo("Hello from Kotlin")) + } + } + + override fun createRouteBuilder(): RoutesBuilder { + return MyRouteBuilder() + } +} diff --git a/examples/loadbalancing/README.adoc b/examples/loadbalancing/README.adoc index 83c8036..a957f88 100644 --- a/examples/loadbalancing/README.adoc +++ b/examples/loadbalancing/README.adoc @@ -24,12 +24,11 @@ will automatic failover to the next server. === Build -To compile and install the project in your maven repo, execute the -following command on the root of the project +You will need to compile this example first: [source,sh] ---- -$ mvn clean install +$ mvn compile ---- === Run diff --git a/examples/loadbalancing/pom.xml b/examples/loadbalancing/pom.xml index c786156..53e2b16 100644 --- a/examples/loadbalancing/pom.xml +++ b/examples/loadbalancing/pom.xml @@ -85,7 +85,12 @@ <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2-version}</version> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> <profiles> diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-loadbalancer.xml b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-loadbalancer.xml index 60ec241..6088da0 100644 --- a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-loadbalancer.xml +++ b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-loadbalancer.xml @@ -20,33 +20,18 @@ <!-- START SNIPPET: e1 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> - <bean id="service" class="org.apache.camel.example.service.Generator"/> + <bean id="generator" class="org.apache.camel.example.service.Generator"/> - <camelContext xmlns="http://camel.apache.org/schema/spring"> - - <route id="sendMessage"> - <from uri="timer://org.apache.camel.example.loadbalancer?period=10000"/> - <bean ref="service" method="createReport"/> - <to uri="direct:loadbalance"/> - </route> - - <!-- use failover load balancer in round robin mode, to automatic failover to next server - in case of failure --> - <route id="loadbalancer"> - <from uri="direct:loadbalance"/> - <loadBalance inheritErrorHandler="false"> - <failover roundRobin="true"/> - <to uri="mina:tcp://localhost:9991?sync=true"/> - <to uri="mina:tcp://localhost:9992?sync=true"/> - </loadBalance> - <log message="${body}"/> - </route> + <!-- import the routes from another XML file --> + <import resource="camel-route-context-loadbalancer.xml"/> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <!-- refer to given routes to be used --> + <routeContextRef ref="routes-load-balancer"/> </camelContext> </beans> diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina1.xml b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina1.xml index 50f7faf..93f195a 100644 --- a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina1.xml +++ b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina1.xml @@ -20,23 +20,18 @@ <!-- START SNIPPET: e1 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> - <bean id="service" class="org.apache.camel.example.service.Reporting"/> + <bean id="reporting" class="org.apache.camel.example.service.Reporting"/> - <camelContext xmlns="http://camel.apache.org/schema/spring"> - - <route id="mina1"> - <from uri="mina:tcp://localhost:9991"/> - <setHeader name="minaServer"> - <constant>localhost:9991</constant> - </setHeader> - <bean ref="service" method="updateReport"/> - </route> + <!-- import the route from another XML file --> + <import resource="camel-route-context-mina1.xml"/> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <!-- refer to a given route to be used --> + <routeContextRef ref="routes-mina1"/> </camelContext> </beans> diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml index 49c3104..6c7fd78 100644 --- a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml +++ b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml @@ -25,18 +25,14 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> - <bean id="service" class="org.apache.camel.example.service.Reporting"/> + <bean id="reporting" class="org.apache.camel.example.service.Reporting"/> - <camelContext xmlns="http://camel.apache.org/schema/spring"> - - <route id="mina2"> - <from uri="mina:tcp://localhost:9992"/> - <setHeader name="minaServer"> - <constant>localhost:9992</constant> - </setHeader> - <bean ref="service" method="updateReport"/> - </route> + <!-- import the route from another XML file --> + <import resource="camel-route-context-mina2.xml"/> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <!-- refer to a given route to be used --> + <routeContextRef ref="routes-mina2"/> </camelContext> </beans> diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-loadbalancer.xml b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-loadbalancer.xml new file mode 100644 index 0000000..0f70ba9 --- /dev/null +++ b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-loadbalancer.xml @@ -0,0 +1,28 @@ +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <routeContext id="routes-load-balancer" xmlns="http://camel.apache.org/schema/spring"> + <route id="sendMessage"> + <from uri="timer://org.apache.camel.example.loadbalancer?period=2000"/> + <bean ref="generator" method="createReport"/> + <to uri="direct:loadbalance"/> + </route> + + <!-- use failover load balancer in round robin mode, to automatic failover to next server + in case of failure --> + <route id="loadbalancer"> + <from uri="direct:loadbalance"/> + <loadBalance inheritErrorHandler="false"> + <failover roundRobin="true"/> + <to uri="mina:tcp://localhost:9991?sync=true"/> + <to uri="mina:tcp://localhost:9992?sync=true"/> + </loadBalance> + <log message="${body}"/> + </route> + </routeContext> + +</beans> \ No newline at end of file diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-mina1.xml b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-mina1.xml new file mode 100644 index 0000000..badea1c --- /dev/null +++ b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-mina1.xml @@ -0,0 +1,18 @@ +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <routeContext id="routes-mina1" xmlns="http://camel.apache.org/schema/spring"> + <route id="mina1"> + <from uri="mina:tcp://localhost:9991"/> + <setHeader name="minaServer"> + <constant>localhost:9991</constant> + </setHeader> + <bean ref="reporting" method="updateReport"/> + </route> + </routeContext> + +</beans> \ No newline at end of file diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-mina2.xml b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-mina2.xml new file mode 100644 index 0000000..861642a --- /dev/null +++ b/examples/loadbalancing/src/main/resources/META-INF/spring/camel-route-context-mina2.xml @@ -0,0 +1,18 @@ +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <routeContext id="routes-mina2" xmlns="http://camel.apache.org/schema/spring"> + <route id="mina2"> + <from uri="mina:tcp://localhost:9992"/> + <setHeader name="minaServer"> + <constant>localhost:9992</constant> + </setHeader> + <bean ref="reporting" method="updateReport"/> + </route> + </routeContext> + +</beans> \ No newline at end of file diff --git a/examples/loadbalancing/src/test/java/org/apache/camel/example/LoadBalancingTest.java b/examples/loadbalancing/src/test/java/org/apache/camel/example/LoadBalancingTest.java new file mode 100644 index 0000000..555231f --- /dev/null +++ b/examples/loadbalancing/src/test/java/org/apache/camel/example/LoadBalancingTest.java @@ -0,0 +1,56 @@ +/* + * 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.example; + +import org.apache.camel.Predicate; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.example.model.Report; +import org.apache.camel.model.ModelCamelContext; +import org.apache.camel.test.spring.junit5.CamelSpringTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; + +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A unit test allowing to check that Camel can load balance the load. + */ +@CamelSpringTest +@ContextConfiguration(locations = { "test-camel-context.xml" }) +class LoadBalancingTest { + + @Autowired + ModelCamelContext context; + + @Test + void should_aggregate_provided_numbers() { + final String[] minaServers = {"localhost:9991", "localhost:9992"}; + final Predicate predicate = exchange -> { + final Report report = exchange.getIn().getBody(Report.class); + return report.getReply().contains(minaServers[(report.getId() - 1) % 2]); + }; + NotifyBuilder notify = new NotifyBuilder(context).fromRoute("sendMessage").whenCompleted(4) + .whenAllDoneMatches(predicate) + .create(); + assertTrue( + notify.matches(20, TimeUnit.SECONDS), "4 messages should be completed" + ); + } +} diff --git a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml b/examples/loadbalancing/src/test/resources/org/apache/camel/example/test-camel-context.xml similarity index 68% copy from examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml copy to examples/loadbalancing/src/test/resources/org/apache/camel/example/test-camel-context.xml index 49c3104..2e33285 100644 --- a/examples/loadbalancing/src/main/resources/META-INF/spring/camel-context-mina2.xml +++ b/examples/loadbalancing/src/test/resources/org/apache/camel/example/test-camel-context.xml @@ -20,23 +20,22 @@ <!-- START SNIPPET: e1 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> - <bean id="service" class="org.apache.camel.example.service.Reporting"/> + <bean id="generator" class="org.apache.camel.example.service.Generator"/> + <bean id="reporting" class="org.apache.camel.example.service.Reporting"/> + <!-- import the routes from external XML files --> + <import resource="classpath:/META-INF/spring/camel-route-context-loadbalancer.xml"/> + <import resource="classpath:/META-INF/spring/camel-route-context-mina1.xml"/> + <import resource="classpath:/META-INF/spring/camel-route-context-mina2.xml"/> <camelContext xmlns="http://camel.apache.org/schema/spring"> - - <route id="mina2"> - <from uri="mina:tcp://localhost:9992"/> - <setHeader name="minaServer"> - <constant>localhost:9992</constant> - </setHeader> - <bean ref="service" method="updateReport"/> - </route> - + <!-- refer to given routes to be used --> + <routeContextRef ref="routes-load-balancer"/> + <routeContextRef ref="routes-mina1"/> + <routeContextRef ref="routes-mina2"/> </camelContext> </beans> diff --git a/examples/main-artemis/README.adoc b/examples/main-artemis/README.adoc index b0b9472..9426d1e 100644 --- a/examples/main-artemis/README.adoc +++ b/examples/main-artemis/README.adoc @@ -9,6 +9,15 @@ via Camel built-in dependency-injection that supports binding via the Also notice how you can configure Camel in the `application.properties` file. +=== Build + +You will need to compile this example first: + +[source,sh] +---- +$ mvn compile +---- + === How to run First install https://activemq.apache.org/components/artemis/[Apache ActiveMQ Artemis] @@ -26,7 +35,7 @@ Then you can run this example using [source,sh] ---- -$ mvn compile camel:run +$ mvn camel:run ---- === Help and contributions diff --git a/examples/main-artemis/pom.xml b/examples/main-artemis/pom.xml index ab3a075..6b8500e 100644 --- a/examples/main-artemis/pom.xml +++ b/examples/main-artemis/pom.xml @@ -94,7 +94,18 @@ <version>${log4j2-version}</version> <scope>runtime</scope> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-jms-server</artifactId> + <version>${activemq-artemis-version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/examples/main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java index c5c41c0..9f87933 100644 --- a/examples/main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java +++ b/examples/main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java @@ -22,7 +22,7 @@ public class MyRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { - from("timer:foo?period=3000") + from("timer:foo?period={{myPeriod}}") .transform(constant("Hello World")) .to("jms:queue:cheese"); diff --git a/examples/main-artemis/src/main/resources/application.properties b/examples/main-artemis/src/main/resources/application.properties index ac4e21c..67e8dae 100644 --- a/examples/main-artemis/src/main/resources/application.properties +++ b/examples/main-artemis/src/main/resources/application.properties @@ -27,6 +27,8 @@ camel.main.jmx-enabled = false ### camel.component.properties.environment-variable-mode=1 # setup JMS component with connection to ActiveMQ Artemis broker +camel.component.jms.connection-factory.user=admin +camel.component.jms.connection-factory.password=admin camel.component.jms.connection-factory.brokerURL=tcp://localhost:61616 camel.component.jms.connection-factory=#class:org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory @@ -43,4 +45,5 @@ camel.component.jms.connection-factory=#class:org.apache.activemq.artemis.jms.cl # the url to the Artemis Broker ### artemisBroker=tcp://localhost:61616 - +# properties used in the route +myPeriod = 3000 diff --git a/examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java b/examples/main-artemis/src/test/java/org/apache/camel/example/MainArtemisTest.java similarity index 60% copy from examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java copy to examples/main-artemis/src/test/java/org/apache/camel/example/MainArtemisTest.java index 4e08de4..fd9bc55 100644 --- a/examples/artemis/src/test/java/org/apache/camel/example/artemis/ArtemisTest.java +++ b/examples/main-artemis/src/test/java/org/apache/camel/example/MainArtemisTest.java @@ -14,14 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.example.artemis; +package org.apache.camel.example; import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl; import org.apache.activemq.artemis.core.server.ActiveMQServer; import org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.component.jms.JmsComponent; +import org.apache.camel.component.jms.JmsConfiguration; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -29,15 +32,16 @@ import org.junit.jupiter.api.Test; import org.apache.activemq.artemis.core.config.Configuration; +import java.util.Properties; import java.util.concurrent.TimeUnit; -import static org.jgroups.util.Util.assertTrue; +import static org.apache.camel.util.PropertiesHelper.asProperties; +import static org.junit.jupiter.api.Assertions.assertTrue; /** - * A unit test checking that the Widget and Gadget use-case from the Enterprise Integration Patterns book works - * properly using Apache ActiveMQ Artemis. + * A unit test checking that Camel can send and consume messages to/from Apache ActiveMQ Artemis. */ -class ArtemisTest extends CamelTestSupport { +class MainArtemisTest extends CamelTestSupport { private static ActiveMQServer SERVER; @@ -45,6 +49,9 @@ class ArtemisTest extends CamelTestSupport { static void init() throws Exception { Configuration config = new ConfigurationImpl(); config.addAcceptorConfiguration("tcp", "tcp://localhost:61616"); + config.setJournalDirectory("target/artemis-data/journal"); + config.setBindingsDirectory("target/artemis-data/bindings"); + config.setLargeMessagesDirectory("target/artemis-data/largemessages"); config.setSecurityEnabled(false); SERVER = new ActiveMQServerImpl(config); SERVER.start(); @@ -58,11 +65,22 @@ class ArtemisTest extends CamelTestSupport { } @Override + protected Properties useOverridePropertiesWithPropertiesComponent() { + return asProperties("myPeriod", Integer.toString(500)); + } + + @Override protected CamelContext createCamelContext() throws Exception { // create CamelContext CamelContext camelContext = super.createCamelContext(); + // Sets up the Artemis core protocol connection factory + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + + JmsConfiguration configuration = new JmsConfiguration(); + configuration.setConnectionFactory(connectionFactory); + // connect to ActiveMQ Artemis JMS broker - camelContext.addComponent("jms", ArtemisMain.createArtemisComponent()); + camelContext.addComponent("jms", new JmsComponent(configuration)); return camelContext; } @@ -71,18 +89,17 @@ class ArtemisTest extends CamelTestSupport { void should_distribute_orders() { NotifyBuilder notify = new NotifyBuilder(context) - .whenCompleted(1).wereSentTo("jms:queue:widget") - .and().whenCompleted(1).wereSentTo("jms:queue:gadget") + .whenCompleted(1).wereSentTo("jms:queue:cheese") + .and().whenCompleted(1).from("jms:queue:gadget") .create(); assertTrue( - "One order should be distributed to widget and and the other to gadget", - notify.matches(10, TimeUnit.SECONDS) + notify.matches(20, TimeUnit.SECONDS), "1 message should be exchanged" ); } @Override - protected RoutesBuilder[] createRouteBuilders() { - return new RoutesBuilder[]{new CreateOrderRoute(), new WidgetGadgetRoute()}; + protected RoutesBuilder createRouteBuilder() { + return new MyRouteBuilder(); } } diff --git a/examples/routeloader/README.adoc b/examples/routeloader/README.adoc index b72584a..e079672 100644 --- a/examples/routeloader/README.adoc +++ b/examples/routeloader/README.adoc @@ -8,10 +8,18 @@ part of Camel. The example has the following routes in the `src/main/resources/myroutes` directory: -- `MyRouteBuilder.java` which a java file, but the file is not part of the regular source code and is therefore not compiled with the regular Java compiler. The route is loaded by Camels route loader and dynamic compiled via `camel-java-joor-dsl` during startup of Camel. +- `MyRouteBuilder.java` which is a java file, but the file is not part of the regular source code and is therefore not compiled with the regular Java compiler. The route is loaded by Camel's route loader and dynamically compiled via `camel-java-joor-dsl` during startup of Camel. - `my-yaml-route.yaml` which is loaded by the new `camel-yaml-dsl` engine. - `cheese-route.xml` which is loaded by the `camel-xml-io-dsl` engine. +=== Build + +You will need to compile this example first: + +---- +$ mvn compile +---- + === How to run You can run this example using diff --git a/examples/routeloader/pom.xml b/examples/routeloader/pom.xml index aacbb4b..bf1aec4 100644 --- a/examples/routeloader/pom.xml +++ b/examples/routeloader/pom.xml @@ -119,7 +119,12 @@ <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/examples/routeloader/src/test/java/org/apache/camel/example/RouteLoaderTest.java b/examples/routeloader/src/test/java/org/apache/camel/example/RouteLoaderTest.java new file mode 100644 index 0000000..aaaf35d --- /dev/null +++ b/examples/routeloader/src/test/java/org/apache/camel/example/RouteLoaderTest.java @@ -0,0 +1,59 @@ +/* + * 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.example; + +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.spi.PackageScanResourceResolver; +import org.apache.camel.spi.Resource; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A unit test checking that Camel can load routes dynamically. + */ +class RouteLoaderTest extends CamelTestSupport { + + @Test + void should_load_routes_dynamically() { + NotifyBuilder notify = new NotifyBuilder(context) + .from("timer:xml*").whenCompleted(1) + .and().from("timer:java*").whenCompleted(1) + .and().from("timer:yaml*").whenCompleted(1).create(); + assertTrue( + notify.matches(20, TimeUnit.SECONDS), "3 messages should be completed" + ); + } + + @Override + protected RoutesBuilder[] createRouteBuilders() throws Exception { + final ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class); + final PackageScanResourceResolver resolver = ecc.getPackageScanResourceResolver(); + final List<RoutesBuilder> routesBuilders = new ArrayList<>(); + for (Resource resource : resolver.findResources("myroutes/*")) { + routesBuilders.addAll(ecc.getRoutesLoader().findRoutesBuilders(resource)); + } + return routesBuilders.toArray(RoutesBuilder[]::new); + } +} diff --git a/examples/routes-configuration/README.adoc b/examples/routes-configuration/README.adoc index 781fa1e..0efba98 100644 --- a/examples/routes-configuration/README.adoc +++ b/examples/routes-configuration/README.adoc @@ -11,6 +11,13 @@ specific route configuration, which is also _coded_ in the same language as the But this is not required, you can use Java to code your route configurations for advanced error handling, and then _code_ your routes in other languages such as XML or YAML. +=== Build + +You will need to compile this example first: + +---- +$ mvn compile +---- === How to run diff --git a/examples/routes-configuration/pom.xml b/examples/routes-configuration/pom.xml index 19bce91..f1ce82b 100644 --- a/examples/routes-configuration/pom.xml +++ b/examples/routes-configuration/pom.xml @@ -103,7 +103,12 @@ <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/examples/routes-configuration/src/test/java/org/apache/camel/example/RoutesConfigurationTest.java b/examples/routes-configuration/src/test/java/org/apache/camel/example/RoutesConfigurationTest.java new file mode 100644 index 0000000..5fc78ac --- /dev/null +++ b/examples/routes-configuration/src/test/java/org/apache/camel/example/RoutesConfigurationTest.java @@ -0,0 +1,63 @@ +/* + * 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.example; + +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.spi.PackageScanResourceResolver; +import org.apache.camel.spi.Resource; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A unit test checking that Camel can load routes dynamically. + */ +class RoutesConfigurationTest extends CamelTestSupport { + + @Test + void should_load_routes_dynamically() { + NotifyBuilder notify = new NotifyBuilder(context) + .from("timer:xml*").whenCompleted(1) + .and().from("timer:java*").whenCompleted(1) + .and().from("timer:yaml*").whenCompleted(1).create(); + assertTrue( + notify.matches(20, TimeUnit.SECONDS), "3 messages should be completed" + ); + } + + @Override + protected RoutesBuilder[] createRouteBuilders() throws Exception { + final ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class); + final PackageScanResourceResolver resolver = ecc.getPackageScanResourceResolver(); + final List<RoutesBuilder> routesBuilders = new ArrayList<>(); + routesBuilders.add(new MyJavaRouteBuilder()); + routesBuilders.add(new MyJavaErrorHandler()); + for (String location : List.of("myroutes/*","myerror/*")) { + for (Resource resource : resolver.findResources(location)) { + routesBuilders.addAll(ecc.getRoutesLoader().findRoutesBuilders(resource)); + } + } + return routesBuilders.toArray(RoutesBuilder[]::new); + } +} diff --git a/examples/routetemplate/README.adoc b/examples/routetemplate/README.adoc index a15ff1d..04c872b 100644 --- a/examples/routetemplate/README.adoc +++ b/examples/routetemplate/README.adoc @@ -12,6 +12,14 @@ See the `MyApplication.java` where you can change the source to use java instead The example runs standalone via Camel Main in the `MyApplication.java` source file. +=== Build + +You will need to compile this example first: + +---- +$ mvn compile +---- + === How to run You can run this example using diff --git a/examples/routetemplate/pom.xml b/examples/routetemplate/pom.xml index 15e8d4b..1643af0 100644 --- a/examples/routetemplate/pom.xml +++ b/examples/routetemplate/pom.xml @@ -81,7 +81,12 @@ <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> - + <!-- for testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/examples/routetemplate/src/main/data/foo.properties b/examples/routetemplate/src/main/data/foo.properties deleted file mode 100644 index b43e6bc..0000000 --- a/examples/routetemplate/src/main/data/foo.properties +++ /dev/null @@ -1,18 +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. -## --------------------------------------------------------------------------- - -bye = Bye \ No newline at end of file diff --git a/examples/routetemplate/src/main/java/org/apache/camel/example/MyApplication.java b/examples/routetemplate/src/main/java/org/apache/camel/example/MyApplication.java index 2d46da6..3a2d357 100644 --- a/examples/routetemplate/src/main/java/org/apache/camel/example/MyApplication.java +++ b/examples/routetemplate/src/main/java/org/apache/camel/example/MyApplication.java @@ -31,7 +31,7 @@ public final class MyApplication { Main main = new Main(); // and add route templates via routes builder main.configure().addRoutesBuilder(MyRouteTemplates.class); - // add configuration class which setup the routes from the route templates + // add configuration class which set up the routes from the route templates // to configure route templates we can use java code as below from a template builder class, // gives more power as its java code. diff --git a/examples/routetemplate/src/main/resources/application.properties b/examples/routetemplate/src/main/resources/application.properties index 70275c1..658ff9b 100644 --- a/examples/routetemplate/src/main/resources/application.properties +++ b/examples/routetemplate/src/main/resources/application.properties @@ -23,7 +23,7 @@ camel.main.name = MyCoolCamel camel.main.lightweight = true # create routes from the route template -# this can also be done in Java code, see MyConfiguration.java +# this can also be done in Java code, see MyTemplateBuilder.java camel.route-template[0].template-id = myTemplate camel.route-template[0].name = one camel.route-template[0].greeting = Hello @@ -32,6 +32,3 @@ camel.route-template[1].name = two camel.route-template[1].greeting = Bonjour camel.route-template[1].my-period = 5s -# application properties -hi = Hello - diff --git a/examples/routetemplate/src/test/java/org/apache/camel/example/RouteTemplateTest.java b/examples/routetemplate/src/test/java/org/apache/camel/example/RouteTemplateTest.java new file mode 100644 index 0000000..421e5b4 --- /dev/null +++ b/examples/routetemplate/src/test/java/org/apache/camel/example/RouteTemplateTest.java @@ -0,0 +1,48 @@ +/* + * 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.example; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A unit test checking that Camel supports parameterized routes. + */ +class RouteTemplateTest extends CamelTestSupport { + + @Test + void should_support_parameterized_routes() { + // Manually adds the templated routes + new MyTemplateBuilder().configure(context); + + NotifyBuilder notify = new NotifyBuilder(context).from("timer:*").whenCompleted(2).create(); + assertTrue( + notify.matches(20, TimeUnit.SECONDS), "2 messages should be completed" + ); + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new MyRouteTemplates(); + } +}