Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
Camel MongoDB GridFS componentAvailable as of Camel 2.17 Maven users will need to add the following dependency to their <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-mongodb-gridfs</artifactId> <version>x.y.z</version> <!-- use the same version as your Camel core version --> </dependency> URI formatmongodb-gridfs:connectionBean?database=databaseName&bucket=bucketName[&moreOptions...] URI format ( camel < 2.19 )gridfs:connectionBean?database=databaseName&bucket=bucketName[&moreOptions...] Endpoint optionsGridFS endpoints support the following options, depending on whether they are acting like a Producer or as a Consumer (options vary based on the consumer type too).
Configuration of database in Spring XMLThe following Spring XML creates a bean defining the connection to a MongoDB instance. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mongoBean" class="com.mongodb.Mongo"> <constructor-arg name="host" value="${mongodb.host}" /> <constructor-arg name="port" value="${mongodb.port}" /> </bean> </beans> Sample routeThe following route defined in Spring XML executes the operation findOne on a collection. Get a file from GridFS <route> <from uri="direct:start" /> <!-- using bean 'mongoBean' defined above --> <to uri="mongodb-gridfs:mongoBean?database=${mongodb.database}&operation=findOne" /> <to uri="direct:result" /> </route>
GridFS operations - producer endpointcountReturns the total number of file in the collection, returning an Integer as the OUT message body. // from("direct:count").to("mongodb-gridfs?database=tickets&operation=count"); Integer result = template.requestBodyAndHeader("direct:count", "irrelevantBody"); assertTrue("Result is not of type Long", result instanceof Integer); You can provide a filename header to provide a count of files matching that filename. Map<String, Object> headers = new HashMap<String, Object>(); headers.put(Exchange.FILE_NAME, "filename.txt"); Integer count = template.requestBodyAndHeaders("direct:count", query, headers); listAllReturns an Reader that lists all the filenames and their IDs in a tab separated stream. // from("direct:listAll").to("mongodb-gridfs?database=tickets&operation=listAll"); Reader result = template.requestBodyAndHeader("direct:listAll", "irrelevantBody"); filename1.txt 1252314321 filename2.txt 2897651254
findOneFinds a file in the GridFS system and sets the body to an InputStream of the content. Also provides the metadata has headers. It uses Exchange.FILE_NAME from the incoming headers to determine the file to find. // from("direct:findOne").to("mongodb-gridfs?database=tickets&operation=findOne"); Map<String, Object> headers = new HashMap<String, Object>(); headers.put(Exchange.FILE_NAME, "filename.txt"); InputStream result = template.requestBodyAndHeaders("direct:findOne", "irrelevantBody", headers);
createCreates a new file in the GridFs database. It uses the Exchange.FILE_NAME from the incoming headers for the name and the body contents (as an InputStream) as the content. // from("direct:create").to("mongodb-gridfs?database=tickets&operation=create"); Map<String, Object> headers = new HashMap<String, Object>(); headers.put(Exchange.FILE_NAME, "filename.txt"); InputStream stream = ... the data for the file ... template.requestBodyAndHeaders("direct:create", stream, headers); removeRemoves a file from the GridFS database. // from("direct:remove").to("mongodb-gridfs?database=tickets&operation=remove"); Map<String, Object> headers = new HashMap<String, Object>(); headers.put(Exchange.FILE_NAME, "filename.txt"); template.requestBodyAndHeaders("direct:remove", "", headers); GridFS ConsumerThe GridFS component will poll GridFS periodically for new files to process. The two parameters that control this behavior are the delay and initialDelay parameters. The delay parameter specifies how long the background tread will sleep between polling attempts. The default is 500ms. The initialDelay parameter specifies how long the consumer will wait after starting before polling the first time. This is useful if the backend service needs a bit longer to become available. The Consumer has several strategies for determining which files within the grid have not been processed yet:
from("mongodb-gridfs?database=tickets&queryStrategy=FileAttribute").process(.....); from("mongodb-gridfs?database=myData&queryStrategy=PersistentTimestamp&persistentTSCollection=CamelTimestamps&persistentTSObject=myDataTS").process(...)
See also
|