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 00a2093d78b20c00af0a6424c910e7aab1c3da9a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Jun 17 11:13:10 2019 +0200 CAMEL-13647: Allow to do autowrire by classpath. Quick and dirty prototype. --- .../apache/camel/component/jms/JmsComponent.java | 2 +- .../camel/component/jms/JmsConfiguration.java | 6 ++- .../component/jms/reply/QueueReplyManager.java | 2 +- .../jms/reply/TemporaryQueueReplyManager.java | 2 +- .../camel/support/PropertyBindingSupport.java | 7 +-- examples/camel-example-main-artemis/pom.xml | 2 +- examples/camel-example-main-artemis/readme.adoc | 25 ++++++--- .../src/main/data/foo.properties | 18 ------- .../main/java/org/apache/camel/example/MyBean.java | 36 ------------- .../org/apache/camel/example/MyConfiguration.java | 25 +++++++-- .../org/apache/camel/example/MyRouteBuilder.java | 8 ++- .../org/apache/camel/example/StandaloneCamel.java | 62 ---------------------- .../src/main/resources/application.properties | 34 ++++-------- .../src/main/resources/log4j2.properties | 2 +- 14 files changed, 62 insertions(+), 169 deletions(-) diff --git a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java index d767111..bbd6ac8 100644 --- a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java +++ b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsComponent.java @@ -1328,7 +1328,7 @@ public class JmsComponent extends HeaderFilterStrategyComponent implements Appli String cfUsername = getAndRemoveParameter(parameters, "username", String.class, getConfiguration().getUsername()); String cfPassword = getAndRemoveParameter(parameters, "password", String.class, getConfiguration().getPassword()); if (cfUsername != null && cfPassword != null) { - cf = endpoint.getConfiguration().getConnectionFactory(); + cf = endpoint.getConfiguration().getOrCreateConnectionFactory(); ObjectHelper.notNull(cf, "ConnectionFactory"); log.debug("Wrapping existing ConnectionFactory with UserCredentialsConnectionFactoryAdapter using username: {} and password: ******", cfUsername); UserCredentialsConnectionFactoryAdapter ucfa = new UserCredentialsConnectionFactoryAdapter(); diff --git a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java index de0a5d8..d394b9f 100644 --- a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java +++ b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java @@ -766,6 +766,10 @@ public class JmsConfiguration implements Cloneable { } public ConnectionFactory getConnectionFactory() { + return connectionFactory; + } + + public ConnectionFactory getOrCreateConnectionFactory() { if (connectionFactory == null) { connectionFactory = createConnectionFactory(); } @@ -1621,7 +1625,7 @@ public class JmsConfiguration implements Cloneable { */ protected PlatformTransactionManager createTransactionManager() { JmsTransactionManager answer = new JmsTransactionManager(); - answer.setConnectionFactory(getConnectionFactory()); + answer.setConnectionFactory(getOrCreateConnectionFactory()); return answer; } diff --git a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/QueueReplyManager.java b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/QueueReplyManager.java index 52f4aa8..b4f5d2e 100644 --- a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/QueueReplyManager.java +++ b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/QueueReplyManager.java @@ -177,7 +177,7 @@ public class QueueReplyManager extends ReplyManagerSupport { if (endpoint.getReplyToMaxConcurrentConsumers() > 0) { answer.setMaxConcurrentConsumers(endpoint.getReplyToMaxConcurrentConsumers()); } - answer.setConnectionFactory(endpoint.getConnectionFactory()); + answer.setConnectionFactory(endpoint.getConfiguration().getOrCreateConnectionFactory()); String clientId = endpoint.getClientId(); if (clientId != null) { clientId += ".CamelReplyManager"; diff --git a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java index 93983d9..5115315 100644 --- a/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java +++ b/components/camel-jms/src/main/java/org/apache/camel/component/jms/reply/TemporaryQueueReplyManager.java @@ -111,7 +111,7 @@ public class TemporaryQueueReplyManager extends ReplyManagerSupport { if (endpoint.getReplyToMaxConcurrentConsumers() > 0) { answer.setMaxConcurrentConsumers(endpoint.getReplyToMaxConcurrentConsumers()); } - answer.setConnectionFactory(endpoint.getConnectionFactory()); + answer.setConnectionFactory(endpoint.getConfiguration().getOrCreateConnectionFactory()); // we use CACHE_CONSUMER by default to cling to the consumer as long as we can, since we can only consume // msgs from the JMS Connection that created the temp destination in the first place if (endpoint.getReplyToCacheLevelName() != null) { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java index 94a425e..fd20f95 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java @@ -595,12 +595,7 @@ public final class PropertyBindingSupport { private static boolean isComplexUserType(Class type) { // lets consider all non java, as complex types - return type != null && !type.isPrimitive() && !type.getName().startsWith("java"); - } - - private static boolean isInterface(Class type) { - // lets consider all non java, as complex types - return type != null && type.isInterface(); + return type != null && !type.isPrimitive() && !type.getName().startsWith("java."); } private static void setReferenceProperties(CamelContext context, Object target, Map<String, Object> parameters) { diff --git a/examples/camel-example-main-artemis/pom.xml b/examples/camel-example-main-artemis/pom.xml index 33afe33..cbc0485 100644 --- a/examples/camel-example-main-artemis/pom.xml +++ b/examples/camel-example-main-artemis/pom.xml @@ -34,7 +34,7 @@ <description>An example for showing standalone Camel with Artemis (autowiring JMS client via classpath scanning)</description> <properties> - <category>Beginner</category> + <category>Messaging</category> </properties> <dependencies> diff --git a/examples/camel-example-main-artemis/readme.adoc b/examples/camel-example-main-artemis/readme.adoc index ad5da44..f772448 100644 --- a/examples/camel-example-main-artemis/readme.adoc +++ b/examples/camel-example-main-artemis/readme.adoc @@ -1,22 +1,33 @@ -== Camel Example Main +== Camel Example Main Artemis This example shows how to run Camel standalone via the built-in Main class. +The example requires a running Apache ActiveMQ Artemis broker running. + 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 example also demonstrates how you can configure the ActiveMQ Artemis JMS `ConnectionFactory` +via two different styles -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_. +- via `@BindToRegistry` from Java source code in the `MyConfiguration.java` source file +- via `application.properties` and the `camel-main-maven-plugin` + that performs classpath scanning to auto-detect the JMS client and prepare for autowiring + by generating the `META-INF/services/org/apache/camel/autowire.properties` file. === How to run -You can run this example using +First install https://activemq.apache.org/components/artemis/[Apache ActiveMQ Artemis] +and create a broker, such as `mybroker`, and create the admin user as `admin` as username +and `admin` as password: + + bin/artemis create mybroker + cd mybroker + bin/artemis run + +Then you can run this example using mvn camel:run diff --git a/examples/camel-example-main-artemis/src/main/data/foo.properties b/examples/camel-example-main-artemis/src/main/data/foo.properties deleted file mode 100644 index b43e6bc..0000000 --- a/examples/camel-example-main-artemis/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/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyBean.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyBean.java deleted file mode 100644 index 1e1bdb8..0000000 --- a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyBean.java +++ /dev/null @@ -1,36 +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. - */ -package org.apache.camel.example; - -public class MyBean { - - private String hi; - private String bye; - - public MyBean(String hi, String bye) { - this.hi = hi; - this.bye = bye; - } - - public String hello() { - return hi + " how are you?"; - } - - public String bye() { - return bye + " World"; - } -} diff --git a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java index 9c26fdd..38c1e89 100644 --- a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java +++ b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyConfiguration.java @@ -16,6 +16,10 @@ */ package org.apache.camel.example; +import javax.jms.ConnectionFactory; + +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; +import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory; import org.apache.camel.BindToRegistry; import org.apache.camel.PropertyInject; @@ -24,11 +28,22 @@ import org.apache.camel.PropertyInject; */ public class MyConfiguration { - @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); - } + /** + * Creates the Artemis JMS ConnectionFactory and bind it to the Camel registry + * so we can do autowiring on the Camel JMS component. + * See more details in the application.properties file. + * <p/> + * Note: This autowiring is disabled in this example as we use camel-main-maven-plugin + * to do classpath scanning to detect the Artemis JMS Client and automatic create a autowire.properties + * file with some binding details, and then provide additional configuraions in the application.properties file. + */ +// @BindToRegistry +// public ConnectionFactory myArtemisClient(@PropertyInject("artemisBroker") String brokerUrl) { +// ActiveMQConnectionFactory cf = new ActiveMQJMSConnectionFactory(brokerUrl); +// cf.setUser("admin"); +// cf.setPassword("admin"); +// return cf; +// } public void configure() { // this method is optional and can be removed if no additional configuration is needed. diff --git a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java index 09546c0..0fb4b4d 100644 --- a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java +++ b/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/MyRouteBuilder.java @@ -22,11 +22,9 @@ public class MyRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { - from("quartz2:foo?cron={{myCron}}") - .bean("myBean", "hello") - .log("${body}") - .bean("myBean", "bye") - .log("${body}"); + from("timer:foo?period=3s") + .transform(constant("Hello World")) + .to("jms:queue:cheese"); from("jms:queue:cheese").to("log:cheese"); } 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 deleted file mode 100644 index f01c5b6..0000000 --- a/examples/camel-example-main-artemis/src/main/java/org/apache/camel/example/StandaloneCamel.java +++ /dev/null @@ -1,62 +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. - */ -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-artemis/src/main/resources/application.properties b/examples/camel-example-main-artemis/src/main/resources/application.properties index ac6de98..fe60043 100644 --- a/examples/camel-example-main-artemis/src/main/resources/application.properties +++ b/examples/camel-example-main-artemis/src/main/resources/application.properties @@ -17,37 +17,23 @@ # to configure camel main # here you can configure options on camel main (see MainConfigurationProperties class) -camel.main.name = MyCoolCamel +camel.main.name = MyCamelJmsClient camel.main.jmx-enabled = false -# you can also configure camel context directly -# camel.context.shutdown-strategy.shutdown-now-on-timeout = false - -# load additional property placeholders from this folder -camel.main.file-configurations=src/main/data/*.properties - -# to configure the camel quartz component -# here we can configure the options on the component level (and we can use dash-naming-style) -camel.component.quartz2.start-delayed-seconds = 3 - -# to configure Hystrix EIP (global and you need to add camel-hystrix to the classpath) -### camel.hystrix.group-key=myGroup -### camel.hystrix.execution-timeout-in-milliseconds=5000 - -# to configure Rest DSL (global and you need to add camel-undertow to the classpath) -### camel.rest.component=undertow -### camel.rest.port=8080 -### camel.rest.component-properties[host-options.buffer-size]=8192 - # 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 +# see in pom.xml file how we use camel-main-maven-plugin to detect the Artemis JMS Client +# and automatic generate META-INF/services/org/apache/camel/autowire.properties +# which contains some additional autowires so we only need here to specify the URL to the broker camel.component.jms.connectionFactory.brokerURL=tcp://localhost:61616 +camel.component.jms.connectionFactory.user=admin +camel.component.jms.connectionFactory.password=admin -# properties used in the route -myCron = 0/2 * * * * ? +# this is used if you enable the @BindToRegistry in MyConfiguration class to autowire +# the Artemis JMS ConnectionFactory via Java source code +# the url to the Artemis Broker +### artemisBroker=tcp://localhost:61616 -# application properties -hi = Hello diff --git a/examples/camel-example-main-artemis/src/main/resources/log4j2.properties b/examples/camel-example-main-artemis/src/main/resources/log4j2.properties index d050a4f..c63f861 100644 --- a/examples/camel-example-main-artemis/src/main/resources/log4j2.properties +++ b/examples/camel-example-main-artemis/src/main/resources/log4j2.properties @@ -23,4 +23,4 @@ rootLogger.level = INFO rootLogger.appenderRef.out.ref = out logger.camel-main.name = org.apache.camel.main -logger.camel-main.level = DEBUG +logger.camel-main.level = INFO