CAMEL-10635 - camel-mongodb-gridfs - The component should not store state
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7ef99c17 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7ef99c17 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7ef99c17 Branch: refs/heads/master Commit: 7ef99c17d082c4e2302f215f2f1374f912b24af8 Parents: a56afad Author: Andrea Cosentino <anco...@gmail.com> Authored: Fri Dec 23 08:54:54 2016 +0100 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Fri Dec 23 08:54:54 2016 +0100 ---------------------------------------------------------------------- .../camel/component/gridfs/GridFsComponent.java | 19 ------------------- .../camel/component/gridfs/GridFsEndpoint.java | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7ef99c17/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsComponent.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsComponent.java index 62701a0..5b91313 100644 --- a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsComponent.java +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsComponent.java @@ -18,35 +18,20 @@ package org.apache.camel.component.gridfs; import java.util.Map; -import com.mongodb.Mongo; import org.apache.camel.Endpoint; import org.apache.camel.impl.UriEndpointComponent; -import org.apache.camel.util.CamelContextHelper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class GridFsComponent extends UriEndpointComponent { - private static final Logger LOG = LoggerFactory.getLogger(GridFsComponent.class); - - private volatile Mongo db; - public GridFsComponent() { super(GridFsEndpoint.class); } protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - if (db == null) { - db = CamelContextHelper.mandatoryLookup(getCamelContext(), remaining, Mongo.class); - LOG.debug("Resolved the connection with the name {} as {}", remaining, db); - } GridFsEndpoint endpoint = new GridFsEndpoint(uri, this); - parameters.put("mongoConnection", db); endpoint.setConnectionBean(remaining); - endpoint.setMongoConnection(db); setProperties(endpoint, parameters); return endpoint; @@ -54,10 +39,6 @@ public class GridFsComponent extends UriEndpointComponent { @Override protected void doShutdown() throws Exception { - if (db != null) { - LOG.debug("Closing the connection {} on {}", db, this); - db.close(); - } super.doShutdown(); } http://git-wip-us.apache.org/repos/asf/camel/blob/7ef99c17/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java index f4d5d65..da5a064 100644 --- a/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java +++ b/components/camel-mongodb-gridfs/src/main/java/org/apache/camel/component/gridfs/GridFsEndpoint.java @@ -19,6 +19,7 @@ package org.apache.camel.component.gridfs; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; +import com.mongodb.MongoClient; import com.mongodb.ReadPreference; import com.mongodb.WriteConcern; import com.mongodb.gridfs.GridFS; @@ -30,6 +31,7 @@ import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriPath; +import org.apache.camel.util.CamelContextHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -129,10 +131,21 @@ public class GridFsEndpoint extends DefaultEndpoint { + ", " + writeConcernRef + ". Aborting initialization."; throw new IllegalArgumentException(msg); } - + mongoConnection = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, MongoClient.class); + LOG.debug("Resolved the connection with the name {} as {}", connectionBean, mongoConnection); setWriteReadOptionsOnConnection(); super.doStart(); } + + @Override + protected void doStop() throws Exception { + super.doStop(); + if (mongoConnection != null) { + LOG.debug("Closing connection"); + mongoConnection.close(); + } + } + private void setWriteReadOptionsOnConnection() { // Set the WriteConcern if (writeConcern != null) {