34
34
import org .springframework .integration .mongodb .support .MongoHeaders ;
35
35
import org .springframework .integration .support .AbstractIntegrationMessageBuilder ;
36
36
import org .springframework .integration .transaction .IntegrationResourceHolder ;
37
+ import org .springframework .lang .Nullable ;
37
38
import org .springframework .transaction .support .TransactionSynchronizationManager ;
38
39
import org .springframework .util .Assert ;
39
40
import org .springframework .util .CollectionUtils ;
45
46
* a {@link org.springframework.messaging.Message} with a payload which is the result of
46
47
* execution of a {@link Query}. When expectSingleResult is false (default), the MongoDb
47
48
* {@link Query} is executed using {@link MongoOperations#find(Query, Class)} method which
48
- * returns a {@link List}. The returned {@link List} will be used as the payoad of the
49
+ * returns a {@link List}. The returned {@link List} will be used as the payload of the
49
50
* {@link org.springframework.messaging.Message} returned by the {{@link #receive()}
50
51
* method. An empty {@link List} is treated as null, thus resulting in no
51
52
* {@link org.springframework.messaging.Message} returned by the {{@link #receive()}
65
66
*/
66
67
public class MongoDbMessageSource extends AbstractMessageSource <Object > {
67
68
68
- private final Expression queryExpression ;
69
+ @ Nullable
70
+ private final MongoDbFactory mongoDbFactory ;
69
71
70
- private volatile Expression collectionNameExpression = new LiteralExpression ( "data" ) ;
72
+ private final Expression queryExpression ;
71
73
72
- private volatile StandardEvaluationContext evaluationContext ;
74
+ private Expression collectionNameExpression = new LiteralExpression ( "data" ) ;
73
75
74
- private volatile MongoOperations mongoTemplate ;
76
+ private StandardEvaluationContext evaluationContext ;
75
77
76
- private volatile MongoConverter mongoConverter ;
78
+ private MongoOperations mongoTemplate ;
77
79
78
- private volatile MongoDbFactory mongoDbFactory ;
80
+ private MongoConverter mongoConverter ;
79
81
80
- private volatile boolean initialized = false ;
82
+ private Class <?> entityClass = DBObject . class ;
81
83
82
- private volatile Class <?> entityClass = DBObject . class ;
84
+ private boolean expectSingleResult = false ;
83
85
84
- private volatile boolean expectSingleResult = false ;
86
+ private volatile boolean initialized = false ;
85
87
86
88
/**
87
89
* Creates an instance with the provided {@link MongoDbFactory} and SpEL expression
88
- * which should resolve to a MongoDb 'query' string
89
- * (see https://www.mongodb.org/display/DOCS/Querying).
90
+ * which should resolve to a MongoDb 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
90
91
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
91
92
* @param mongoDbFactory The mongodb factory.
92
93
* @param queryExpression The query expression.
93
94
*/
94
95
public MongoDbMessageSource (MongoDbFactory mongoDbFactory , Expression queryExpression ) {
95
96
Assert .notNull (mongoDbFactory , "'mongoDbFactory' must not be null" );
96
97
Assert .notNull (queryExpression , "'queryExpression' must not be null" );
97
-
98
98
this .mongoDbFactory = mongoDbFactory ;
99
99
this .queryExpression = queryExpression ;
100
100
}
101
101
102
102
/**
103
103
* Creates an instance with the provided {@link MongoOperations} and SpEL expression
104
- * which should resolve to a Mongo 'query' string
105
- * (see https://www.mongodb.org/display/DOCS/Querying).
104
+ * which should resolve to a Mongo 'query' string (see https://www.mongodb.org/display/DOCS/Querying).
106
105
* It assumes that the {@link MongoOperations} is fully initialized and ready to be used.
107
106
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
108
107
* @param mongoTemplate The mongo template.
@@ -111,15 +110,14 @@ public MongoDbMessageSource(MongoDbFactory mongoDbFactory, Expression queryExpre
111
110
public MongoDbMessageSource (MongoOperations mongoTemplate , Expression queryExpression ) {
112
111
Assert .notNull (mongoTemplate , "'mongoTemplate' must not be null" );
113
112
Assert .notNull (queryExpression , "'queryExpression' must not be null" );
114
-
113
+ this . mongoDbFactory = null ;
115
114
this .mongoTemplate = mongoTemplate ;
116
115
this .queryExpression = queryExpression ;
117
116
}
118
117
119
118
/**
120
119
* Allows you to set the type of the entityClass that will be passed to the
121
- * {@link MongoTemplate#find(Query, Class)} or {@link MongoTemplate#findOne(Query, Class)}
122
- * method.
120
+ * {@link MongoTemplate#find(Query, Class)} or {@link MongoTemplate#findOne(Query, Class)} method.
123
121
* Default is {@link DBObject}.
124
122
* @param entityClass The entity class.
125
123
*/
@@ -135,7 +133,7 @@ public void setEntityClass(Class<?> entityClass) {
135
133
* {@link #receive()} will use {@link MongoTemplate#findOne(Query, Class)},
136
134
* and the payload of the returned {@link org.springframework.messaging.Message}
137
135
* will be the returned target Object of type
138
- * identified by {{ @link #entityClass} instead of a List.
136
+ * identified by {@link #entityClass} instead of a List.
139
137
* @param expectSingleResult true if a single result is expected.
140
138
*/
141
139
public void setExpectSingleResult (boolean expectSingleResult ) {
@@ -188,8 +186,8 @@ protected void onInit() {
188
186
/**
189
187
* Will execute a {@link Query} returning its results as the Message payload.
190
188
* The payload can be either {@link List} of elements of objects of type
191
- * identified by {{ @link #entityClass}, or a single element of type identified by { {@link #entityClass}
192
- * based on the value of {{ @link #expectSingleResult} attribute which defaults to 'false' resulting
189
+ * identified by {@link #entityClass}, or a single element of type identified by {@link #entityClass}
190
+ * based on the value of {@link #expectSingleResult} attribute which defaults to 'false' resulting
193
191
* {@link org.springframework.messaging.Message} with payload of type
194
192
* {@link List}. The collection name used in the
195
193
* query will be provided in the {@link MongoHeaders#COLLECTION_NAME} header.
@@ -218,12 +216,10 @@ else if (value instanceof Query) {
218
216
219
217
Object result = null ;
220
218
if (this .expectSingleResult ) {
221
- result = this .mongoTemplate .
222
- findOne (query , this .entityClass , collectionName );
219
+ result = this .mongoTemplate .findOne (query , this .entityClass , collectionName );
223
220
}
224
221
else {
225
- List <?> results = this .mongoTemplate .
226
- find (query , this .entityClass , collectionName );
222
+ List <?> results = this .mongoTemplate .find (query , this .entityClass , collectionName );
227
223
if (!CollectionUtils .isEmpty (results )) {
228
224
result = results ;
229
225
}
0 commit comments