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 2019/06/17 12:04:49 UTC

[camel] 14/20: CAMEL-13647: Allow to do autowrire by classpath. Quick and dirty prototype.

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 530aac0ccdf2b9d2cd45ad376282ac3c445a17b1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 17 10:30:35 2019 +0200

    CAMEL-13647: Allow to do autowrire by classpath. Quick and dirty prototype.
---
 .../pom.xml                                        |  8 +--
 examples/camel-example-main-artemis/readme.adoc    | 25 +++++++++
 .../src/main/data/foo.properties                   | 18 +++++++
 .../org/apache/camel/example/MyApplication.java}   | 28 ++++++----
 .../java/org/apache/camel/example/MyBean.java}     | 23 ++++----
 .../org/apache/camel/example/MyConfiguration.java} | 24 +++++----
 .../org/apache/camel/example/MyRouteBuilder.java   |  0
 .../org/apache/camel/example/StandaloneCamel.java  | 62 ++++++++++++++++++++++
 .../src/main/resources/application.properties      |  0
 .../src/main/resources/log4j2.properties           | 26 +++++++++
 examples/camel-example-main/pom.xml                | 29 ----------
 .../org/apache/camel/example/MyRouteBuilder.java   |  2 -
 .../src/main/resources/application.properties      |  3 --
 examples/pom.xml                                   |  1 +
 14 files changed, 181 insertions(+), 68 deletions(-)

diff --git a/examples/camel-example-main/pom.xml b/examples/camel-example-main-artemis/pom.xml
similarity index 93%
copy from examples/camel-example-main/pom.xml
copy to examples/camel-example-main-artemis/pom.xml
index e56f087..33afe33 100644
--- a/examples/camel-example-main/pom.xml
+++ b/examples/camel-example-main-artemis/pom.xml
@@ -28,10 +28,10 @@
         <version>3.0.0-SNAPSHOT</version>
     </parent>
 
-    <artifactId>camel-example-main</artifactId>
+    <artifactId>camel-example-main-artemis</artifactId>
     <packaging>jar</packaging>
-    <name>Camel :: Example :: Main</name>
-    <description>An example for showing standalone Camel</description>
+    <name>Camel :: Example :: Main :: Artemis</name>
+    <description>An example for showing standalone Camel with Artemis (autowiring JMS client via classpath scanning)</description>
 
     <properties>
         <category>Beginner</category>
@@ -58,7 +58,7 @@
         <dependency>
             <groupId>org.apache.activemq</groupId>
             <artifactId>artemis-jms-client</artifactId>
-            <version>2.9.0</version>
+            <version>${activemq-artemis-version}</version>
         </dependency>
 
         <!-- logging -->
diff --git a/examples/camel-example-main-artemis/readme.adoc b/examples/camel-example-main-artemis/readme.adoc
new file mode 100644
index 0000000..ad5da44
--- /dev/null
+++ b/examples/camel-example-main-artemis/readme.adoc
@@ -0,0 +1,25 @@
+== Camel Example Main
+
+This example shows how to run Camel standalone via the built-in Main class.
+The example also demonstrates how you can configure the Camel application
+via Camel built-in dependency-injection that supports binding via the
+`@BindToRegistry`, `@BeanInject` and `@PropertyInject` annotations.
+
+Also notice how you can configure Camel in the `application.properties` file.
+
+=== Alternative example
+
+The class `StandaloneCamel` is an alternative example that uses a
+_public static void main_ class and where you manually setup Camel without
+any help from Camel's built-in Main class. However it shows how to do this
+in a _raw style_ without using any _magic_.
+
+=== How to run
+
+You can run this example using
+
+    mvn camel:run   
+
+=== More information
+
+You can find more information about Apache Camel at the website: http://camel.apache.org/
diff --git a/examples/camel-example-main-artemis/src/main/data/foo.properties b/examples/camel-example-main-artemis/src/main/data/foo.properties
new file mode 100644
index 0000000..b43e6bc
--- /dev/null
+++ b/examples/camel-example-main-artemis/src/main/data/foo.properties
@@ -0,0 +1,18 @@
+## ---------------------------------------------------------------------------
+## 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/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyApplication.java
similarity index 53%
copy from examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
copy to examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyApplication.java
index 09546c0..c6214d0 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
+++ b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyApplication.java
@@ -16,18 +16,26 @@
  */
 package org.apache.camel.example;
 
-import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.main.Main;
 
-public class MyRouteBuilder extends RouteBuilder {
+/**
+ * Main class that boot the Camel application
+ */
+public final class MyApplication {
 
-    @Override
-    public void configure() throws Exception {
-        from("quartz2:foo?cron={{myCron}}")
-            .bean("myBean", "hello")
-            .log("${body}")
-            .bean("myBean", "bye")
-            .log("${body}");
+    private MyApplication() {
+    }
 
-        from("jms:queue:cheese").to("log:cheese");
+    public static void main(String[] args) throws Exception {
+        // use Camels Main class
+        Main main = new Main();
+        // lets use a configuration class (you can specify multiple classes)
+        // (properties are automatic loaded from application.properties)
+        main.addConfigurationClass(MyConfiguration.class);
+        // and add the routes (you can specify multiple classes)
+        main.addRouteBuilder(MyRouteBuilder.class);
+        // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
+        main.run(args);
     }
+
 }
diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyBean.java
similarity index 68%
copy from examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
copy to examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyBean.java
index 09546c0..1e1bdb8 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
+++ b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyBean.java
@@ -16,18 +16,21 @@
  */
 package org.apache.camel.example;
 
-import org.apache.camel.builder.RouteBuilder;
+public class MyBean {
 
-public class MyRouteBuilder extends RouteBuilder {
+    private String hi;
+    private String bye;
 
-    @Override
-    public void configure() throws Exception {
-        from("quartz2:foo?cron={{myCron}}")
-            .bean("myBean", "hello")
-            .log("${body}")
-            .bean("myBean", "bye")
-            .log("${body}");
+    public MyBean(String hi, String bye) {
+        this.hi = hi;
+        this.bye = bye;
+    }
+
+    public String hello() {
+        return hi + " how are you?";
+    }
 
-        from("jms:queue:cheese").to("log:cheese");
+    public String bye() {
+        return bye + " World";
     }
 }
diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java
similarity index 61%
copy from examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
copy to examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java
index 09546c0..9c26fdd 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
+++ b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java
@@ -16,18 +16,22 @@
  */
 package org.apache.camel.example;
 
-import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.PropertyInject;
 
-public class MyRouteBuilder extends RouteBuilder {
+/**
+ * Class to configure the Camel application.
+ */
+public class MyConfiguration {
 
-    @Override
-    public void configure() throws Exception {
-        from("quartz2:foo?cron={{myCron}}")
-            .bean("myBean", "hello")
-            .log("${body}")
-            .bean("myBean", "bye")
-            .log("${body}");
+    @BindToRegistry
+    public MyBean myBean(@PropertyInject("hi") String hi, @PropertyInject("bye") String bye) {
+        // this will create an instance of this bean with the name of the method (eg myBean)
+        return new MyBean(hi, bye);
+    }
 
-        from("jms:queue:cheese").to("log:cheese");
+    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-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java
similarity index 100%
copy from examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
copy to examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java
diff --git a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/StandaloneCamel.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/StandaloneCamel.java
new file mode 100644
index 0000000..f01c5b6
--- /dev/null
+++ b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/StandaloneCamel.java
@@ -0,0 +1,62 @@
+/*
+ * 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.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ * This is an alternative example to show how you can use a public static void main class
+ * to run Camel standalone (without help from its Main class). This is to demonstrate
+ * what code you need to write to startup Camel without any help (or magic).
+ * <p/>
+ * Compare this example with {@link MyApplication} which uses Camel's main class to
+ * run Camel standalone in a easier way.
+ */
+public final class StandaloneCamel {
+
+    private StandaloneCamel() {
+    }
+
+    public static void main(String[] args) throws Exception {
+        // create a new CamelContext
+        CamelContext camelContext = new DefaultCamelContext();
+
+        // configure where to load properties file in the properties component
+        camelContext.getPropertiesComponent().setLocation("classpath:application.properties");
+        // resolve property placeholder
+        String hello = camelContext.resolvePropertyPlaceholders("{{hi}}");
+
+        // and create bean with the placeholder
+        MyBean myBean = new MyBean(hello, "Bye");
+        // register bean to Camel
+        camelContext.getRegistry().bind("myBean", myBean);
+
+        // add routes to Camel
+        camelContext.addRoutes(new MyRouteBuilder());
+
+        // start Camel
+        camelContext.start();
+
+        // just run for 10 seconds and stop
+        System.out.println("Running for 10 seconds and then stopping");
+        Thread.sleep(10000);
+
+        // stop and shutdown Camel
+        camelContext.stop();
+    }
+}
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main-artemis/src/main/resources/application.properties
similarity index 100%
copy from examples/camel-example-main/src/main/resources/application.properties
copy to examples/camel-example-main-artemis/src/main/resources/application.properties
diff --git a/examples/camel-example-main-artemis/src/main/resources/log4j2.properties b/examples/camel-example-main-artemis/src/main/resources/log4j2.properties
new file mode 100644
index 0000000..d050a4f
--- /dev/null
+++ b/examples/camel-example-main-artemis/src/main/resources/log4j2.properties
@@ -0,0 +1,26 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.out.type = Console
+appender.out.name = out
+appender.out.layout.type = PatternLayout
+appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+rootLogger.level = INFO
+rootLogger.appenderRef.out.ref = out
+
+logger.camel-main.name = org.apache.camel.main
+logger.camel-main.level = DEBUG
diff --git a/examples/camel-example-main/pom.xml b/examples/camel-example-main/pom.xml
index e56f087..43d1ee8 100644
--- a/examples/camel-example-main/pom.xml
+++ b/examples/camel-example-main/pom.xml
@@ -51,15 +51,6 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-quartz2</artifactId>
         </dependency>
-        <dependency>
-            <groupId>org.apache.camel</groupId>
-            <artifactId>camel-jms</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.activemq</groupId>
-            <artifactId>artemis-jms-client</artifactId>
-            <version>2.9.0</version>
-        </dependency>
 
         <!-- logging -->
         <dependency>
@@ -97,26 +88,6 @@
                 </configuration>
             </plugin>
 
-            <!-- generate autowire.properties file that can automatic detect resources
-                 from the classpath to make convention over configuration for Camel components -->
-            <plugin>
-                <groupId>org.apache.camel</groupId>
-                <artifactId>camel-main-maven-plugin</artifactId>
-                <version>${project.version}</version>
-                <configuration>
-                    <logClasspath>false</logClasspath>
-                </configuration>
-                <executions>
-                    <execution>
-                        <id>generate</id>
-                        <goals>
-                            <goal>autowire</goal>
-                        </goals>
-                        <phase>process-classes</phase>
-                    </execution>
-                </executions>
-            </plugin>
-
         </plugins>
     </build>
 
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
index 09546c0..d98ae5f 100644
--- 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
@@ -27,7 +27,5 @@ public class MyRouteBuilder extends RouteBuilder {
             .log("${body}")
             .bean("myBean", "bye")
             .log("${body}");
-
-        from("jms:queue:cheese").to("log:cheese");
     }
 }
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties
index ac6de98..71ab7ab 100644
--- a/examples/camel-example-main/src/main/resources/application.properties
+++ b/examples/camel-example-main/src/main/resources/application.properties
@@ -42,9 +42,6 @@ camel.component.quartz2.start-delayed-seconds = 3
 # you can configure whether OS environment should override (=2 which is default) or as fallback (=1)
 ### camel.component.properties.environment-variable-mode=1
 
-# setup JMS component with connection to ActiveMQ Artemis broker
-camel.component.jms.connectionFactory.brokerURL=tcp://localhost:61616
-
 # properties used in the route
 myCron = 0/2 * * * * ?
 
diff --git a/examples/pom.xml b/examples/pom.xml
index 39b4adf..983a3d1 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -84,6 +84,7 @@
         <module>camel-example-loan-broker-cxf</module>
         <module>camel-example-loan-broker-jms</module>
         <module>camel-example-main</module>
+        <module>camel-example-main-artemis</module>
         <module>camel-example-management</module>
         <module>camel-example-micrometer</module>
         <module>camel-example-mybatis</module>