Class MongoDbMessageSource
java.lang.Object
org.springframework.integration.util.AbstractExpressionEvaluator
org.springframework.integration.endpoint.AbstractMessageSource<Object>
org.springframework.integration.mongodb.inbound.MongoDbMessageSource
- All Implemented Interfaces:
Aware,BeanFactoryAware,BeanNameAware,DisposableBean,InitializingBean,MessageSource<Object>,IntegrationPattern,NamedComponent,IntegrationInboundManagement,IntegrationManagement
public class MongoDbMessageSource extends AbstractMessageSource<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 payload of the
Message returned by the {AbstractMessageSource.receive()
method. An empty List is treated as null, thus resulting in no
Message returned by the {AbstractMessageSource.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, Yaron Yamin, Artem Bilan, Gary Russell
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.integration.support.management.IntegrationManagement
IntegrationManagement.ManagementOverrides -
Field Summary
Fields inherited from class org.springframework.integration.util.AbstractExpressionEvaluator
EXPRESSION_PARSER, loggerFields inherited from interface org.springframework.integration.support.management.IntegrationManagement
METER_PREFIX, RECEIVE_COUNTER_NAME, SEND_TIMER_NAME -
Constructor Summary
Constructors Constructor Description MongoDbMessageSource(MongoOperations mongoTemplate, Expression queryExpression)Creates an instance with the providedMongoOperationsand SpEL expression which should resolve to a Mongo 'query' string (see https://www.mongodb.org/display/DOCS/Querying).MongoDbMessageSource(MongoDatabaseFactory mongoDbFactory, Expression queryExpression)Creates an instance with the providedMongoDatabaseFactoryand SpEL expression which should resolve to a MongoDb 'query' string (see https://www.mongodb.org/display/DOCS/Querying). -
Method Summary
Modifier and Type Method Description protected ObjectdoReceive()Will execute aQueryreturning its results as the Message payload.StringgetComponentType()protected voidonInit()voidsetCollectionNameExpression(Expression collectionNameExpression)Sets the SpELExpressionthat should resolve to a collection name used by theQuery.voidsetEntityClass(Class<?> entityClass)Allows you to set the type of the entityClass that will be passed to theMongoTemplate.find(Query, Class)orMongoTemplate.findOne(Query, Class)method.voidsetExpectSingleResult(boolean expectSingleResult)Allows you to manage which find* method to invoke onMongoTemplate.voidsetMongoConverter(MongoConverter mongoConverter)Allows you to provide a customMongoConverterused to assist in deserialization data read from MongoDb.Methods inherited from class org.springframework.integration.endpoint.AbstractMessageSource
buildMessage, destroy, getBeanName, getComponentName, getManagedName, getManagedType, getOverrides, isLoggingEnabled, receive, registerMetricsCaptor, setBeanName, setHeaderExpressions, setLoggingEnabled, setManagedName, setManagedTypeMethods inherited from class org.springframework.integration.util.AbstractExpressionEvaluator
afterPropertiesSet, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, evaluateExpression, getBeanFactory, getEvaluationContext, getEvaluationContext, getMessageBuilderFactory, setBeanFactory, setConversionServiceMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.integration.support.management.IntegrationManagement
getThisAsMethods inherited from interface org.springframework.integration.core.MessageSource
getIntegrationPatternType
-
Constructor Details
-
MongoDbMessageSource
Creates an instance with the providedMongoDatabaseFactoryand SpEL expression which should resolve to a MongoDb 'query' string (see https://www.mongodb.org/display/DOCS/Querying). The 'queryExpression' will be evaluated on every call to theAbstractMessageSource.receive()method.- Parameters:
mongoDbFactory- The mongodb factory.queryExpression- The query expression.
-
MongoDbMessageSource
Creates an instance with the providedMongoOperationsand SpEL expression which should resolve to a Mongo 'query' string (see https://www.mongodb.org/display/DOCS/Querying). It assumes that theMongoOperationsis fully initialized and ready to be used. The 'queryExpression' will be evaluated on every call to theAbstractMessageSource.receive()method.- Parameters:
mongoTemplate- The mongo template.queryExpression- The query expression.
-
-
Method Details
-
setEntityClass
Allows you to set the type of the entityClass that will be passed to theMongoTemplate.find(Query, Class)orMongoTemplate.findOne(Query, Class)method. Default isDBObject.- Parameters:
entityClass- The entity class.
-
setExpectSingleResult
public void setExpectSingleResult(boolean expectSingleResult)Allows you to manage which find* method to invoke onMongoTemplate. Default is 'false', which means theAbstractMessageSource.receive()method will use theMongoTemplate.find(Query, Class)method. If set to 'true',AbstractMessageSource.receive()will useMongoTemplate.findOne(Query, Class), and the payload of the returnedMessagewill be the returned target Object of type identified byentityClassinstead of a List.- Parameters:
expectSingleResult- true if a single result is expected.
-
setCollectionNameExpression
Sets the SpELExpressionthat should resolve to a collection name used by theQuery. The resulting collection name will be included in theMongoHeaders.COLLECTION_NAMEheader.- Parameters:
collectionNameExpression- The collection name expression.
-
setMongoConverter
Allows you to provide a customMongoConverterused to assist in deserialization data read from MongoDb. Only allowed if this instance was constructed with aMongoDatabaseFactory.- Parameters:
mongoConverter- The mongo converter.
-
getComponentType
-
onInit
protected void onInit()- Overrides:
onInitin classAbstractExpressionEvaluator
-
doReceive
Will execute aQueryreturning its results as the Message payload. The payload can be eitherListof elements of objects of type identified byentityClass, or a single element of type identified byentityClassbased on the value ofexpectSingleResultattribute which defaults to 'false' resultingMessagewith payload of typeList. The collection name used in the query will be provided in theMongoHeaders.COLLECTION_NAMEheader.- Specified by:
doReceivein classAbstractMessageSource<Object>- Returns:
- The value returned.
-