Spring Integration

org.springframework.integration.mongodb.inbound
Class MongoDbMessageSource

java.lang.Object
  extended by org.springframework.integration.context.IntegrationObjectSupport
      extended by org.springframework.integration.mongodb.inbound.MongoDbMessageSource
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, NamedComponent, MessageSource<java.lang.Object>

public class MongoDbMessageSource
extends IntegrationObjectSupport
implements MessageSource<java.lang.Object>

An instance of MessageSource which returns a Message with a payload which is the result of execution of a Query. When expectSingleResult is false (default), the MongoDb Query is executed using MongoOperations.find(Query, Class) method which returns a List. The returned List will be used as the payoad of the Message returned by the {receive() method. An empty List is treated as null, thus resulting in no Message returned by the {receive() method.

When expectSingleResult is true, the MongoOperations.findOne(Query, Class) is used instead, and the message payload will be the single object returned from the query.

Since:
2.2
Author:
Amol Nayak, Oleg Zhurakousky

Field Summary
 
Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport
logger
 
Constructor Summary
MongoDbMessageSource(org.springframework.data.mongodb.MongoDbFactory mongoDbFactory, org.springframework.expression.Expression queryExpression)
          Creates an instance with the provided MongoDbFactory and SpEL expression which should resolve to a MongoDb 'query' string (see http://www.mongodb.org/display/DOCS/Querying).
MongoDbMessageSource(org.springframework.data.mongodb.core.MongoOperations mongoTemplate, org.springframework.expression.Expression queryExpression)
          Creates an instance with the provided MongoOperations and SpEL expression which should resolve to a Mongo 'query' string (see http://www.mongodb.org/display/DOCS/Querying).
 
Method Summary
protected  void onInit()
          Subclasses may implement this for initialization logic.
 Message<java.lang.Object> receive()
          Will execute a Query returning its results as the Message payload.
 void setCollectionNameExpression(org.springframework.expression.Expression collectionNameExpression)
          Sets the SpEL Expression that should resolve to a collection name used by the Query.
 void setEntityClass(java.lang.Class<?> entityClass)
          Allows you to set the type of the entityClass that will be passed to the MongoTemplate.find(Query, Class) or MongoTemplate.findOne(Query, Class) method.
 void setExpectSingleResult(boolean expectSingleResult)
          Allows you to manage which find* method to invoke on MongoTemplate.
 void setMongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter mongoConverter)
          Allows you to provide a custom MongoConverter used to assist in deserialization data read from MongoDb.
 
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, getBeanFactory, getComponentName, getComponentType, getConversionService, getTaskScheduler, setBeanFactory, setBeanName, setComponentName, setConversionService, setTaskScheduler, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MongoDbMessageSource

public MongoDbMessageSource(org.springframework.data.mongodb.MongoDbFactory mongoDbFactory,
                            org.springframework.expression.Expression queryExpression)
Creates an instance with the provided MongoDbFactory and SpEL expression which should resolve to a MongoDb 'query' string (see http://www.mongodb.org/display/DOCS/Querying). The 'queryExpression' will be evaluated on every call to the receive() method.

Parameters:
mongoDbFactory -
queryExpression -

MongoDbMessageSource

public MongoDbMessageSource(org.springframework.data.mongodb.core.MongoOperations mongoTemplate,
                            org.springframework.expression.Expression queryExpression)
Creates an instance with the provided MongoOperations and SpEL expression which should resolve to a Mongo 'query' string (see http://www.mongodb.org/display/DOCS/Querying). It assumes that the MongoOperations is fully initialized and ready to be used. The 'queryExpression' will be evaluated on every call to the receive() method.

Parameters:
mongoTemplate -
queryExpression -
Method Detail

setEntityClass

public void setEntityClass(java.lang.Class<?> entityClass)
Allows you to set the type of the entityClass that will be passed to the MongoTemplate.find(Query, Class) or MongoTemplate.findOne(Query, Class) method. Default is DBObject.

Parameters:
entityClass -

setExpectSingleResult

public void setExpectSingleResult(boolean expectSingleResult)
Allows you to manage which find* method to invoke on MongoTemplate. Default is 'false', which means the receive() method will use the MongoTemplate.find(Query, Class) method. If set to 'true', receive() will use MongoTemplate.findOne(Query, Class), and the payload of the returned Message will be the returned target Object of type identified by {entityClass instead of a List.

Parameters:
expectSingleResult -

setCollectionNameExpression

public void setCollectionNameExpression(org.springframework.expression.Expression collectionNameExpression)
Sets the SpEL Expression that should resolve to a collection name used by the Query. The resulting collection name will be included in the MongoHeaders.COLLECTION_NAME header.

Parameters:
collectionNameExpression -

setMongoConverter

public void setMongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter mongoConverter)
Allows you to provide a custom MongoConverter used to assist in deserialization data read from MongoDb. Only allowed if this instance was constructed with a MongoDbFactory.

Parameters:
mongoConverter -

onInit

protected void onInit()
               throws java.lang.Exception
Description copied from class: IntegrationObjectSupport
Subclasses may implement this for initialization logic.

Overrides:
onInit in class IntegrationObjectSupport
Throws:
java.lang.Exception

receive

public Message<java.lang.Object> receive()
Will execute a Query returning its results as the Message payload. The payload can be either List of elements of objects of type identified by {entityClass, or a single element of type identified by {entityClass based on the value of {expectSingleResult attribute which defaults to 'false' resulting Message with payload of type List. The collection name used in the query will be provided in the MongoHeaders.COLLECTION_NAME header.

Specified by:
receive in interface MessageSource<java.lang.Object>

Spring Integration