This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 253f726 CAMEL-13662 connectionBean fallback to properties (#2984) 253f726 is described below commit 253f726bc48495731c4a66649a20f9569fa81319 Author: Pasquale Congiusti <pasquale.congiu...@gmail.com> AuthorDate: Wed Jun 19 16:06:22 2019 +0200 CAMEL-13662 connectionBean fallback to properties (#2984) * CAMEL-13662 connectionBean fallback to properties * Adding the possibility to get connection from properties * Extend a basic unit test in order to use the new feature * Add a timeout as the test was hanging up indefinitely * CAMEL-13662 amended with PR suggestions * CAMEL-13662 checkstyle fixed * CAMEL-13662 fix typo in the autogenerated doc --- .../src/main/docs/mongodb3-component.adoc | 14 +++-- .../camel/component/mongodb3/MongoDbEndpoint.java | 12 +++- .../mongodb3/MongoDbConnectionBeansTest.java | 67 ++++++++++++++++++++++ .../dsl/MongoDbEndpointBuilderFactory.java | 61 ++++++++++++++++++++ 4 files changed, 145 insertions(+), 9 deletions(-) diff --git a/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc b/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc index 4939cda..bb4ea19 100644 --- a/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc +++ b/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc @@ -87,11 +87,12 @@ with the following path and query parameters: [width="100%",cols="2,5,^1,2",options="header"] |=== | Name | Description | Default | Type -| *connectionBean* | *Required* Name of com.mongodb.Mongo to use. | | String +| *connectionBean* | *Required* Name of com.mongodb.Mongo reference to use. | | String |=== +You may use `mongoConnection` query parameter to provide a connection instead of the `connectionBean` reference. In such circumstance, the `connectionBean` will be used as a simple reference name for your route with no relation to the connection client used and it's still required parameter. -==== Query Parameters (23 parameters): +==== Query Parameters (22 parameters): [width="100%",cols="2,5,^1,2",options="header"] @@ -101,6 +102,7 @@ with the following path and query parameters: | *collectionIndex* (common) | Sets the collection index (JSON FORMAT : { field1 : order1, field2 : order2}) | | String | *createCollection* (common) | Create collection during initialisation if it doesn't exist. Default is true. | true | boolean | *database* (common) | Sets the name of the MongoDB database to target | | String +| *mongoConnection* (common) | Sets the Mongo instance that represents the backing connection | | MongoClient | *operation* (common) | Sets the operation this endpoint will execute against MongoDB. For possible values, see MongoDbOperation. | | MongoDbOperation | *outputType* (common) | Convert the output of the producer to the selected type : DocumentList Document or MongoIterable. DocumentList or MongoIterable applies to findAll and aggregate. Document applies to all other operations. | | MongoDbOutputType | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean @@ -152,7 +154,7 @@ The component supports 3 options, which are listed below. // spring-boot-auto-configure options: END -Note on options of MongoDB component +Note on options of MongoDB component writeConcern *Remove in camel 2.19.* See Mongo client options <<MongoDB-ConfigurationofdatabaseinSpringXML>>. Set the WriteConcern for write operations on MongoDB using the standard ones. Resolved from the fields of the WriteConcern class by calling the link WriteConcernvalueOf(String) method. @@ -844,7 +846,7 @@ new objects are inserted, MongoDB will push them as `Document` in natural order to your tailable cursor consumer, who will transform them to an Exchange and will trigger your route logic. -===== How the tailable cursor consumer works +=== How the tailable cursor consumer works To turn a cursor into a tailable cursor, a few special flags are to be signalled to MongoDB when first generating the cursor. Once created, the @@ -895,7 +897,7 @@ The above route will consume from the "flights.cancellations" capped collection, using "departureTime" as the increasing field, with a default regeneration cursor delay of 1000ms. -===== Persistent tail tracking +=== Persistent tail tracking Standard tail tracking is volatile and the last value is only kept in memory. However, in practice you will need to restart your Camel @@ -915,7 +917,7 @@ persisting at regular intervals too in the future (flush every 5 seconds) for added robustness if the demand is there. To request this feature, please open a ticket in the Camel JIRA. -===== Enabling persistent tail tracking +=== Enabling persistent tail tracking To enable this function, set at least the following options on the endpoint URI: diff --git a/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbEndpoint.java b/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbEndpoint.java index 45d077b..36dd67f 100644 --- a/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbEndpoint.java +++ b/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/MongoDbEndpoint.java @@ -61,9 +61,10 @@ import static org.apache.camel.component.mongodb3.MongoDbOutputType.MongoIterabl label = "database,nosql") public class MongoDbEndpoint extends DefaultEndpoint { + @UriParam(description = "Sets the connection bean used as a client for connecting to a database.") private MongoClient mongoConnection; - @UriPath + @UriPath(description = "Sets the connection bean reference used to lookup a client for connecting to a database.") @Metadata(required = true) private String connectionBean; @UriParam @@ -302,8 +303,13 @@ public class MongoDbEndpoint extends DefaultEndpoint { @Override protected void doStart() throws Exception { - mongoConnection = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, MongoClient.class); - log.debug("Resolved the connection with the name {} as {}", connectionBean, mongoConnection); + if (mongoConnection == null) { + mongoConnection = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, MongoClient.class); + log.debug("Resolved the connection provided by {} context reference as {}", connectionBean, + mongoConnection); + } else { + log.debug("Resolved the connection provided by mongoConnection property parameter as {}", mongoConnection); + } super.doStart(); } diff --git a/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java b/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java new file mode 100644 index 0000000..8809c7f --- /dev/null +++ b/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbConnectionBeansTest.java @@ -0,0 +1,67 @@ +/* + * 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.component.mongodb3; + +import java.util.Properties; + +import com.mongodb.MongoClient; + +import org.apache.camel.CamelContext; +import org.apache.camel.NoSuchBeanException; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.spring.SpringCamelContext; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class MongoDbConnectionBeansTest extends AbstractMongoDbTest { + + @Test + public void checkConnectionFromProperties() { + MongoDbEndpoint testEndpoint = context.getEndpoint( + "mongodb3:anyName?mongoConnection=#myDb&database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true", + MongoDbEndpoint.class); + assertNotEquals("myDb", testEndpoint.getConnectionBean()); + assertEquals(mongo, testEndpoint.getMongoConnection()); + } + + @Test + public void checkConnectionFromBean() { + MongoDbEndpoint testEndpoint = context.getEndpoint( + "mongodb3:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true", + MongoDbEndpoint.class); + assertEquals("myDb", testEndpoint.getConnectionBean()); + assertEquals(mongo, testEndpoint.getMongoConnection()); + } + + @Test + public void checkConnectionBothExisting() { + MongoDbEndpoint testEndpoint = context.getEndpoint( + "mongodb3:myDb?mongoConnection=#myDbS&database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true", + MongoDbEndpoint.class); + assertEquals("myDb", testEndpoint.getConnectionBean()); + MongoClient myDbS = mongo = applicationContext.getBean("myDbS", MongoClient.class); + assertEquals(myDbS, testEndpoint.getMongoConnection()); + } + + @Test(expected = Exception.class) + public void checkMissingConnection() { + MongoDbEndpoint testEndpoint = context.getEndpoint( + "mongodb3:anythingNotRelated?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true", + MongoDbEndpoint.class); + } + +} diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java index 8c90b96..104a43f 100644 --- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java +++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/MongoDbEndpointBuilderFactory.java @@ -103,6 +103,27 @@ public interface MongoDbEndpointBuilderFactory { return this; } /** + * Sets the Mongo instance that represents the backing connection. + * The option is a <code>com.mongodb.MongoClient</code> type. + * @group common + */ + default MongoDbEndpointConsumerBuilder mongoConnection( + Object mongoConnection) { + setProperty("mongoConnection", mongoConnection); + return this; + } + /** + * Sets the Mongo instance that represents the backing connection. + * The option will be converted to a + * <code>com.mongodb.MongoClient</code> type. + * @group common + */ + default MongoDbEndpointConsumerBuilder mongoConnection( + String mongoConnection) { + setProperty("mongoConnection", mongoConnection); + return this; + } + /** * Sets the operation this endpoint will execute against MongoDB. For * possible values, see MongoDbOperation. * The option is a @@ -557,6 +578,27 @@ public interface MongoDbEndpointBuilderFactory { return this; } /** + * Sets the Mongo instance that represents the backing connection. + * The option is a <code>com.mongodb.MongoClient</code> type. + * @group common + */ + default MongoDbEndpointProducerBuilder mongoConnection( + Object mongoConnection) { + setProperty("mongoConnection", mongoConnection); + return this; + } + /** + * Sets the Mongo instance that represents the backing connection. + * The option will be converted to a + * <code>com.mongodb.MongoClient</code> type. + * @group common + */ + default MongoDbEndpointProducerBuilder mongoConnection( + String mongoConnection) { + setProperty("mongoConnection", mongoConnection); + return this; + } + /** * Sets the operation this endpoint will execute against MongoDB. For * possible values, see MongoDbOperation. * The option is a @@ -953,6 +995,25 @@ public interface MongoDbEndpointBuilderFactory { return this; } /** + * Sets the Mongo instance that represents the backing connection. + * The option is a <code>com.mongodb.MongoClient</code> type. + * @group common + */ + default MongoDbEndpointBuilder mongoConnection(Object mongoConnection) { + setProperty("mongoConnection", mongoConnection); + return this; + } + /** + * Sets the Mongo instance that represents the backing connection. + * The option will be converted to a + * <code>com.mongodb.MongoClient</code> type. + * @group common + */ + default MongoDbEndpointBuilder mongoConnection(String mongoConnection) { + setProperty("mongoConnection", mongoConnection); + return this; + } + /** * Sets the operation this endpoint will execute against MongoDB. For * possible values, see MongoDbOperation. * The option is a