org.apache.camel.component.mongodb
Class MongoDbEndpoint

java.lang.Object
  extended by org.apache.camel.support.ServiceSupport
      extended by org.apache.camel.impl.DefaultEndpoint
          extended by org.apache.camel.component.mongodb.MongoDbEndpoint
All Implemented Interfaces:
org.apache.camel.CamelContextAware, org.apache.camel.Endpoint, org.apache.camel.IsSingleton, org.apache.camel.Service, org.apache.camel.ShutdownableService, org.apache.camel.spi.HasId, org.apache.camel.StatefulService, org.apache.camel.SuspendableService

public class MongoDbEndpoint
extends org.apache.camel.impl.DefaultEndpoint

Represents a MongoDb endpoint. It is responsible for creating MongoDbProducer and MongoDbTailableCursorConsumer instances. It accepts a number of options to customise the behaviour of consumers and producers.


Field Summary
 
Fields inherited from class org.apache.camel.support.ServiceSupport
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
 
Constructor Summary
MongoDbEndpoint()
           
MongoDbEndpoint(String endpointUri)
           
MongoDbEndpoint(String uri, MongoDbComponent component)
           
 
Method Summary
 org.apache.camel.Consumer createConsumer(org.apache.camel.Processor processor)
           
 List<com.mongodb.DBObject> createIndex()
          Create technical list index
 org.apache.camel.Exchange createMongoDbExchange(com.mongodb.DBObject dbObj)
           
 org.apache.camel.Producer createProducer()
           
protected  void doStart()
          Applies validation logic specific to this endpoint type.
 void ensureIndex(com.mongodb.DBCollection collection, List<com.mongodb.DBObject> dynamicIndex)
          Add Index
 String getCollection()
           
 String getCollectionIndex()
           
 MongoDbConsumerType getConsumerType()
           
 long getCursorRegenerationDelay()
           
 String getDatabase()
           
 com.mongodb.DB getDb()
           
 com.mongodb.DBCollection getDbCollection()
           
 com.mongodb.Mongo getMongoConnection()
           
 MongoDbOperation getOperation()
           
 String getPersistentId()
           
 com.mongodb.ReadPreference getReadPreference()
           
 String getTailTrackCollection()
           
 String getTailTrackDb()
           
 String getTailTrackField()
           
 String getTailTrackIncreasingField()
           
 MongoDbTailTrackingConfig getTailTrackingConfig()
           
 com.mongodb.WriteConcern getWriteConcern()
           
 com.mongodb.WriteConcern getWriteConcernRef()
           
 void initializeConnection()
          Initialises the MongoDB connection using the Mongo object provided to the endpoint
 boolean isCreateCollection()
           
 boolean isDynamicity()
           
 boolean isInvokeGetLastError()
           
 boolean isPersistentTailTracking()
           
 boolean isSingleton()
           
 boolean isWriteResultAsHeader()
           
 void setCollection(String collection)
          Sets the name of the MongoDB collection to bind to this endpoint
 void setCollectionIndex(String collectionIndex)
          Sets the collection index (JSON FORMAT : { "field1" : order1, "field2" : order2})
 void setConsumerType(String consumerType)
          Reserved for future use, when more consumer types are supported.
 void setCreateCollection(boolean createCollection)
          Create collection during initialisation if it doesn't exist.
 void setCursorRegenerationDelay(long cursorRegenerationDelay)
          MongoDB tailable cursors will block until new data arrives.
 void setDatabase(String database)
          Sets the name of the MongoDB database to target
 void setDynamicity(boolean dynamicity)
          Sets whether this endpoint will attempt to dynamically resolve the target database and collection from the incoming Exchange properties.
 void setInvokeGetLastError(boolean invokeGetLastError)
          Instructs this endpoint to invoke WriteResult.getLastError() with every operation.
 void setMongoConnection(com.mongodb.Mongo mongoConnection)
          Sets the Mongo instance that represents the backing connection
 void setOperation(String operation)
          Sets the operation this endpoint will execute against MongoDB.
 void setPersistentId(String persistentId)
          One tail tracking collection can host many trackers for several tailable consumers.
 void setPersistentTailTracking(boolean persistentTailTracking)
          Enable persistent tail tracking, which is a mechanism to keep track of the last consumed message across system restarts.
 void setReadPreference(String readPreference)
          Sets a MongoDB ReadPreference on the Mongo connection.
 void setTailTrackCollection(String tailTrackCollection)
          Collection where tail tracking information will be persisted.
 void setTailTrackDb(String tailTrackDb)
          Indicates what database the tail tracking mechanism will persist to.
 void setTailTrackField(String tailTrackField)
          Field where the last tracked value will be placed.
 void setTailTrackIncreasingField(String tailTrackIncreasingField)
          Correlation field in the incoming record which is of increasing nature and will be used to position the tailing cursor every time it is generated.
 void setWriteConcern(String writeConcern)
          Set the WriteConcern for write operations on MongoDB using the standard ones.
 void setWriteConcernRef(String writeConcernRef)
          Set the WriteConcern for write operations on MongoDB, passing in the bean ref to a custom WriteConcern which exists in the Registry.
 void setWriteResultAsHeader(boolean writeResultAsHeader)
          In write operations, it determines whether instead of returning WriteResult as the body of the OUT message, we transfer the IN message to the OUT and attach the WriteResult as a header.
 
Methods inherited from class org.apache.camel.impl.DefaultEndpoint
configureConsumer, configurePollingConsumer, configureProperties, createEndpointConfiguration, createEndpointUri, createExchange, createExchange, createExchange, createPollingConsumer, doStop, equals, getCamelContext, getComponent, getConsumerProperties, getEndpointConfiguration, getEndpointKey, getEndpointUri, getExchangePattern, getId, hashCode, isLenientProperties, isSynchronous, setCamelContext, setConsumerProperties, setEndpointConfiguration, setEndpointUri, setEndpointUriIfNotSpecified, setExchangePattern, setProperties, setSynchronous, toString
 
Methods inherited from class org.apache.camel.support.ServiceSupport
doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, resume, shutdown, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.camel.Service
start, stop
 

Constructor Detail

MongoDbEndpoint

public MongoDbEndpoint()

MongoDbEndpoint

public MongoDbEndpoint(String uri,
                       MongoDbComponent component)

MongoDbEndpoint

public MongoDbEndpoint(String endpointUri)
Method Detail

createProducer

public org.apache.camel.Producer createProducer()
                                         throws Exception
Throws:
Exception

createConsumer

public org.apache.camel.Consumer createConsumer(org.apache.camel.Processor processor)
                                         throws Exception
Throws:
Exception

isSingleton

public boolean isSingleton()

initializeConnection

public void initializeConnection()
                          throws CamelMongoDbException
Initialises the MongoDB connection using the Mongo object provided to the endpoint

Throws:
CamelMongoDbException

ensureIndex

public void ensureIndex(com.mongodb.DBCollection collection,
                        List<com.mongodb.DBObject> dynamicIndex)
Add Index

Parameters:
collection -

createIndex

public List<com.mongodb.DBObject> createIndex()
                                       throws Exception
Create technical list index

Returns:
technical list index
Throws:
Exception

doStart

protected void doStart()
                throws Exception
Applies validation logic specific to this endpoint type. If everything succeeds, continues initialization

Overrides:
doStart in class org.apache.camel.impl.DefaultEndpoint
Throws:
Exception

createMongoDbExchange

public org.apache.camel.Exchange createMongoDbExchange(com.mongodb.DBObject dbObj)

setCollection

public void setCollection(String collection)
Sets the name of the MongoDB collection to bind to this endpoint

Parameters:
collection - collection name

getCollection

public String getCollection()

setCollectionIndex

public void setCollectionIndex(String collectionIndex)
Sets the collection index (JSON FORMAT : { "field1" : order1, "field2" : order2})


getCollectionIndex

public String getCollectionIndex()

setOperation

public void setOperation(String operation)
                  throws CamelMongoDbException
Sets the operation this endpoint will execute against MongoDB. For possible values, see MongoDbOperation.

Parameters:
operation - name of the operation as per catalogued values
Throws:
CamelMongoDbException

getOperation

public MongoDbOperation getOperation()

setDatabase

public void setDatabase(String database)
Sets the name of the MongoDB database to target

Parameters:
database - name of the MongoDB database

getDatabase

public String getDatabase()

setCreateCollection

public void setCreateCollection(boolean createCollection)
Create collection during initialisation if it doesn't exist. Default is true.

Parameters:
createCollection - true or false

isCreateCollection

public boolean isCreateCollection()

getDb

public com.mongodb.DB getDb()

getDbCollection

public com.mongodb.DBCollection getDbCollection()

setMongoConnection

public void setMongoConnection(com.mongodb.Mongo mongoConnection)
Sets the Mongo instance that represents the backing connection

Parameters:
mongoConnection - the connection to the database

getMongoConnection

public com.mongodb.Mongo getMongoConnection()

setWriteConcern

public void setWriteConcern(String writeConcern)
Set the WriteConcern for write operations on MongoDB using the standard ones. Resolved from the fields of the WriteConcern class by calling the WriteConcern.valueOf(String) method.

Parameters:
writeConcern - the standard name of the WriteConcern
See Also:
possible options

getWriteConcern

public com.mongodb.WriteConcern getWriteConcern()

setInvokeGetLastError

public void setInvokeGetLastError(boolean invokeGetLastError)
Instructs this endpoint to invoke WriteResult.getLastError() with every operation. By default, MongoDB does not wait for the write operation to occur before returning. If set to true, each exchange will only return after the write operation has actually occurred in MongoDB.

Parameters:
invokeGetLastError - true or false

isInvokeGetLastError

public boolean isInvokeGetLastError()

setWriteConcernRef

public void setWriteConcernRef(String writeConcernRef)
Set the WriteConcern for write operations on MongoDB, passing in the bean ref to a custom WriteConcern which exists in the Registry. You can also use standard WriteConcerns by passing in their key. See the setWriteConcern method.

Parameters:
writeConcernRef - the name of the bean in the registry that represents the WriteConcern to use

getWriteConcernRef

public com.mongodb.WriteConcern getWriteConcernRef()

setReadPreference

public void setReadPreference(String readPreference)
Sets a MongoDB ReadPreference on the Mongo connection. Read preferences set directly on the connection will be overridden by this setting.

Parameters:
readPreference - the bean name of the read preference to set

getReadPreference

public com.mongodb.ReadPreference getReadPreference()

setDynamicity

public void setDynamicity(boolean dynamicity)
Sets whether this endpoint will attempt to dynamically resolve the target database and collection from the incoming Exchange properties. Can be used to override at runtime the database and collection specified on the otherwise static endpoint URI. It is disabled by default to boost performance. Enabling it will take a minimal performance hit.

Parameters:
dynamicity - true or false indicated whether target database and collection should be calculated dynamically based on Exchange properties.
See Also:
MongoDbConstants.DATABASE, MongoDbConstants.COLLECTION

isDynamicity

public boolean isDynamicity()

setConsumerType

public void setConsumerType(String consumerType)
                     throws CamelMongoDbException
Reserved for future use, when more consumer types are supported.

Parameters:
consumerType - key of the consumer type
Throws:
CamelMongoDbException

getConsumerType

public MongoDbConsumerType getConsumerType()

getTailTrackDb

public String getTailTrackDb()

setTailTrackDb

public void setTailTrackDb(String tailTrackDb)
Indicates what database the tail tracking mechanism will persist to. If not specified, the current database will be picked by default. Dynamicity will not be taken into account even if enabled, i.e. the tail tracking database will not vary past endpoint initialisation.

Parameters:
tailTrackDb - database name

getTailTrackCollection

public String getTailTrackCollection()

setTailTrackCollection

public void setTailTrackCollection(String tailTrackCollection)
Collection where tail tracking information will be persisted. If not specified, MongoDbTailTrackingConfig.DEFAULT_COLLECTION will be used by default.

Parameters:
tailTrackCollection - collection name

getTailTrackField

public String getTailTrackField()

setTailTrackField

public void setTailTrackField(String tailTrackField)
Field where the last tracked value will be placed. If not specified, MongoDbTailTrackingConfig.DEFAULT_FIELD will be used by default.

Parameters:
tailTrackField - field name

setPersistentTailTracking

public void setPersistentTailTracking(boolean persistentTailTracking)
Enable persistent tail tracking, which is a mechanism to keep track of the last consumed message across system restarts. The next time the system is up, the endpoint will recover the cursor from the point where it last stopped slurping records.

Parameters:
persistentTailTracking - true or false

isPersistentTailTracking

public boolean isPersistentTailTracking()

setTailTrackIncreasingField

public void setTailTrackIncreasingField(String tailTrackIncreasingField)
Correlation field in the incoming record which is of increasing nature and will be used to position the tailing cursor every time it is generated. The cursor will be (re)created with a query of type: tailTrackIncreasingField > lastValue (possibly recovered from persistent tail tracking). Can be of type Integer, Date, String, etc. NOTE: No support for dot notation at the current time, so the field should be at the top level of the document.

Parameters:
tailTrackIncreasingField -

getTailTrackIncreasingField

public String getTailTrackIncreasingField()

getTailTrackingConfig

public MongoDbTailTrackingConfig getTailTrackingConfig()

setCursorRegenerationDelay

public void setCursorRegenerationDelay(long cursorRegenerationDelay)
MongoDB tailable cursors will block until new data arrives. If no new data is inserted, after some time the cursor will be automatically freed and closed by the MongoDB server. The client is expected to regenerate the cursor if needed. This value specifies the time to wait before attempting to fetch a new cursor, and if the attempt fails, how long before the next attempt is made. Default value is 1000ms.

Parameters:
cursorRegenerationDelay - delay specified in milliseconds

getCursorRegenerationDelay

public long getCursorRegenerationDelay()

setPersistentId

public void setPersistentId(String persistentId)
One tail tracking collection can host many trackers for several tailable consumers. To keep them separate, each tracker should have its own unique persistentId.

Parameters:
persistentId - the value of the persistent ID to use for this tailable consumer

getPersistentId

public String getPersistentId()

isWriteResultAsHeader

public boolean isWriteResultAsHeader()

setWriteResultAsHeader

public void setWriteResultAsHeader(boolean writeResultAsHeader)
In write operations, it determines whether instead of returning WriteResult as the body of the OUT message, we transfer the IN message to the OUT and attach the WriteResult as a header.

Parameters:
writeResultAsHeader - flag to indicate if this option is enabled


Apache Camel