Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Camel MongoDB GridFS component

Available as of Camel 2.17

...

Code Block
xml
xml
<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 format

Code Block
gridfs:connectionBean?database=databaseName&bucket=bucketName[&moreOptions...]

Endpoint options

GridFS 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).

...

Name

Default Value

Description

Producer

Consumer

database

none

Required. The name of the database to which this endpoint will be bound. All operations will be executed against this database.

(tick)

(tick)

bucket

fs

The name of the GridFS bucket within the Database. The default is the GridFS.DEFAULT_BUCKET value ("fs").

(tick)

(tick)

operation

create

The id of the operation this endpoint will execute. Pick from the following:

  • Query operations: findOne, listAll, count
  • Write operations: create
  • Delete operations: remove

(tick)

 

querynoneCombined with the query strategy parameters to create the query used to search for new files. (tick)
queryStrategyTimeStamp

The strategy that is used to find new files. Can be one of:

  • TimeStamp - files that are uploaded after the Consumer starts are processed
  • PersistentTimestamp - Like TimeStamp, but the last timestamp used is persisted to a collection so when the Consumer restarts, it can resume where it left off
  • FileAttribute - finds files that do NOT have the give attribute. After processing, it adds the attribute.
  • TimestampAndFileAttribute - finds files that are newer than the TimeStamp and are missing the attribute
  • PersistentTimestampAndFileAttribute
 (tick)
persistentTSCollectioncamel-timestampsWhen using persistent timestamps, this is the Collection that the timestamp is stored into. (tick)
persistenTSObjectcamel-timestampWhen using persistent timestamps, this is the object ID for the timestamp object. Each consumer can have it's own timestamp ID stored in a common Collection (tick)
fileAttributeNamecamel-processedWhen using FileAttribute, this is the name of the attribute that is used. When a file is about to be processed, the attribute is set to "processing" and then set to "done" when the file processing is done. (tick)
delay500 (ms)The delay between polling GridFS for new files (tick)
initialDelay1000 (ms)The initial delay before the first poll (tick)

Configuration of database in Spring XML

The following Spring XML creates a bean defining the connection to a MongoDB instance.

Code Block
languagexml
<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 route

The following route defined in Spring XML executes the operation findOne on a collection.

Code Block
languagexml
titleGet a file from GridFS
<route>
  <from uri="direct:start" />
  <!-- using bean 'mongoBean' defined above -->
  <to uri="gridfs:mongoBean?database=${mongodb.database}&amp;operation=findOne" />
  <to uri="direct:result" />
</route>

 

GridFS operations - producer endpoint

count

Returns the total number of file in the collection, returning an Integer as the OUT message body.

...

Code Block
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(Exchange.FILE_NAME, "filename.txt");
Integer count = template.requestBodyAndHeaders("direct:count", query, headers);

listAll

Returns an Reader that lists all the filenames and their IDs in a tab separated stream.

Code Block
// from("direct:listAll").to("gridfs?database=tickets&operation=listAll");
Reader result = template.requestBodyAndHeader("direct:listAll", "irrelevantBody");

filename1.txt	1252314321
filename2.txt	2897651254

 

findOne

Finds 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.

Code Block
// from("direct:findOne").to("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);

 

create

Creates 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.

Code Block
// from("direct:create").to("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);

remove

Removes a file from the GridFS database.

Code Block
// from("direct:remove").to("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 Consumer

 

 

 

 

See also