This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch bind in repository https://gitbox.apache.org/repos/asf/camel.git
commit f4f3b69087c1692b5cb62a4407a52e37700fe3c4 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Mar 5 07:23:14 2019 +0100 CAMEL-13283: Add example --- .../camel/impl/CamelPostProcessorHelper.java | 4 ++ .../src/main/java/org/apache/camel/main/Main.java | 4 ++ .../java/org/apache/camel/main/MainSupport.java | 41 +++++++++++++-- .../java/org/apache/camel/main/MainIoCTest.java | 17 ++++++- examples/camel-example-main/pom.xml | 4 ++ .../org/apache/camel/example/MyApplication.java | 59 ++++------------------ .../main/java/org/apache/camel/example/MyBean.java | 30 +++++++++++ .../org/apache/camel/example/MyConfiguration.java | 40 +++++++++++++++ .../org/apache/camel/example/MyRouteBuilder.java | 29 +++++++++++ .../src/main/resources/application.properties | 21 ++++++++ 10 files changed, 195 insertions(+), 54 deletions(-) diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java b/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java index 2845ac8..7ca5f9d 100644 --- a/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java +++ b/core/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java @@ -294,6 +294,10 @@ public class CamelPostProcessorHelper implements CamelContextAware { public Object getInjectionBeanValue(Class<?> type, String name) { if (ObjectHelper.isEmpty(name)) { + // is it camel context itself? + if (type.isAssignableFrom(camelContext.getClass())) { + return camelContext; + } Set<?> found = getCamelContext().getRegistry().findByType(type); if (found == null || found.isEmpty()) { throw new NoSuchBeanException(name, type.getName()); diff --git a/core/camel-core/src/main/java/org/apache/camel/main/Main.java b/core/camel-core/src/main/java/org/apache/camel/main/Main.java index e2ea96c..2309fe2 100644 --- a/core/camel-core/src/main/java/org/apache/camel/main/Main.java +++ b/core/camel-core/src/main/java/org/apache/camel/main/Main.java @@ -35,6 +35,10 @@ public class Main extends MainSupport { public Main() { } + public Main(Class configurationClass) { + super(configurationClass); + } + public static void main(String... args) throws Exception { Main main = new Main(); instance = main; diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java index 832e610..4154844 100644 --- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java +++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java @@ -16,6 +16,7 @@ */ package org.apache.camel.main; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -52,6 +53,9 @@ import org.apache.camel.util.concurrent.ThreadHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.camel.support.ObjectHelper.invokeMethod; +import static org.apache.camel.util.ReflectionHelper.findMethod; + /** * Base class for main implementations to allow starting up a JVM with Camel embedded. */ @@ -72,6 +76,7 @@ public abstract class MainSupport extends ServiceSupport { protected CamelContext camelContext; protected List<RouteBuilder> routeBuilders = new ArrayList<>(); + protected Class configurationClass; protected String routeBuilderClasses; protected String fileWatchDirectory; protected boolean fileWatchDirectoryRecursively; @@ -104,6 +109,11 @@ public abstract class MainSupport extends ServiceSupport { } } + protected MainSupport(Class configurationClass) { + this(); + this.configurationClass = configurationClass; + } + protected MainSupport() { addOption(new Option("h", "help", "Displays the help screen") { protected void doProcess(String arg, LinkedList<String> remainingArgs) { @@ -416,6 +426,22 @@ public abstract class MainSupport extends ServiceSupport { return exitCode.get(); } + public Class getConfigurationClass() { + return configurationClass; + } + + /** + * Sets optional configuration class which allows to do any initial configuration. + * The class can/should have a method named <tt>configure</tt> which is called. + */ + public void setConfigurationClass(Class configurationClass) { + this.configurationClass = configurationClass; + } + + public String getRouteBuilderClasses() { + return routeBuilderClasses; + } + public void setRouteBuilderClasses(String builders) { this.routeBuilderClasses = builders; } @@ -446,10 +472,6 @@ public abstract class MainSupport extends ServiceSupport { this.fileWatchDirectoryRecursively = fileWatchDirectoryRecursively; } - public String getRouteBuilderClasses() { - return routeBuilderClasses; - } - public ReloadStrategy getReloadStrategy() { return reloadStrategy; } @@ -672,6 +694,17 @@ public abstract class MainSupport extends ServiceSupport { autoConfigurationFromProperties(camelContext); } + if (configurationClass != null) { + // create instance of configuration class as it may do dependency injection and bind to registry + Object config = camelContext.getInjector().newInstance(configurationClass); + // invoke configure method if exists + Method method = findMethod(configurationClass, "configure"); + if (method != null) { + log.info("Calling configure method on configuration class: {}", configurationClass); + invokeMethod(method, config); + } + } + // try to load the route builders from the routeBuilderClasses loadRouteBuilders(camelContext); for (RouteBuilder routeBuilder : routeBuilders) { diff --git a/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java b/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java index 3a7af5d..2d57f1b 100644 --- a/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/main/MainIoCTest.java @@ -16,6 +16,7 @@ */ package org.apache.camel.main; +import org.apache.camel.BeanInject; import org.apache.camel.CamelContext; import org.apache.camel.PropertyInject; import org.apache.camel.builder.RouteBuilder; @@ -29,7 +30,8 @@ public class MainIoCTest extends Assert { @Test public void testMainIoC() throws Exception { - Main main = new Main(); + // use configuration class + Main main = new Main(MyConfiguration.class); // add as class so we get IoC main.addRouteBuilder(MyRouteBuilder.class); main.start(); @@ -52,9 +54,22 @@ public class MainIoCTest extends Assert { DirectComponent direct = camelContext.getComponent("direct", DirectComponent.class); assertEquals(1234, direct.getTimeout()); + // should have called the configure class + assertEquals("123", camelContext.getGlobalOptions().get("foo")); + main.stop(); } + public static class MyConfiguration { + + @BeanInject + private CamelContext camel; + + public void configure() { + camel.getGlobalOptions().put("foo", "123"); + } + } + public static class MyRouteBuilder extends RouteBuilder { // properties is automatic loaded from classpath:application.properties diff --git a/examples/camel-example-main/pom.xml b/examples/camel-example-main/pom.xml index f2c9489..31bfa23 100644 --- a/examples/camel-example-main/pom.xml +++ b/examples/camel-example-main/pom.xml @@ -43,6 +43,10 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-quartz2</artifactId> + </dependency> <!-- logging --> <dependency> diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java index e60df81..1a3617c 100644 --- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java +++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java @@ -16,65 +16,26 @@ */ package org.apache.camel.example; -import java.util.Date; -import java.util.Objects; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.builder.RouteBuilder; import org.apache.camel.main.Main; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +/** + * Main class that boot the Camel application + */ public final class MyApplication { - private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class); private MyApplication() { } public static void main(String[] args) throws Exception { + // use Camels Main class Main main = new Main(); - main.addRouteBuilder(new MyRouteBuilder()); + // lets use a configuration class + // properties are automatic loaded from application.properties + main.setConfigurationClass(MyConfiguration.class); + // and add the routes + main.addRouteBuilder(MyRouteBuilder.class); + // now keep the application running until the JVM is terminated (ctrl + c or sigterm) main.run(args); } - private static class MyRouteBuilder extends RouteBuilder { - @Override - public void configure() throws Exception { - from("timer:simple?period=503") - .id("simple-route") - .transform() - .exchange(this::dateToTime) - .process() - .message(this::log) - .process() - .body(this::log) - .choice() - .when() - .body(Integer.class, b -> (b & 1) == 0) - .log("Received even number") - .when() - .body(Integer.class, (b, h) -> h.containsKey("skip") ? false : (b & 1) == 0) - .log("Received odd number") - .when() - .body(Objects::isNull) - .log("Received null body") - .when() - .body(Integer.class, b -> (b & 1) != 0) - .log("Received odd number") - .endChoice(); - } - - private Long dateToTime(Exchange e) { - return e.getProperty(Exchange.TIMER_FIRED_TIME, Date.class).getTime(); - } - - private void log(Object b) { - LOGGER.info("body is: {}", b); - } - - private void log(Message m) { - LOGGER.info("message is: {}", m); - } - } } diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java new file mode 100644 index 0000000..5abc1c7 --- /dev/null +++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java @@ -0,0 +1,30 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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; + +public class MyBean { + + private String hi; + + public MyBean(String hi) { + this.hi = hi; + } + + public String hello() { + return hi + " how are you?"; + } +} diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java new file mode 100644 index 0000000..81289db --- /dev/null +++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java @@ -0,0 +1,40 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.BindToRegistry; +import org.apache.camel.PropertyInject; + +/** + * Class to configure the Camel application. + */ +public class MyConfiguration { + + @PropertyInject("hi") + private String hi; + + @BindToRegistry + public MyBean myBean() { + // this will create an instance of this bean with the name of the method (eg myBean) + return new MyBean(hi); + } + + public void configure() { + // this method is optional and can be removed if no additional configuration is needed. + } + +} diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java new file mode 100644 index 0000000..1e38eab --- /dev/null +++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java @@ -0,0 +1,29 @@ +/** + * 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.builder.RouteBuilder; + +public class MyRouteBuilder extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("quartz2:foo?cron={{myCron}}") + .bean("myBean") + .log("${body}"); + } +} diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties new file mode 100644 index 0000000..febdbea --- /dev/null +++ b/examples/camel-example-main/src/main/resources/application.properties @@ -0,0 +1,21 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +// TODO: add some configuration here + +camel.component.quartz2.startDelayedSeconds = 3 +hi = Hello +myCron = 0/2+*+*+*+*+? \ No newline at end of file