INT-3335: Implement MongoDb Outbound Gateway

JIRA: https://jira.spring.io/browse/INT-3335

INT-3335: PR fixes

INT-3335: fix code style

INT-3335: Add queryExpressionString + minor fixes

* Polishing. Mostly code style
This commit is contained in:
Xavier Padro
2016-12-09 00:02:45 +01:00
committed by Artem Bilan
parent 318bb4c4b7
commit 0fa8849f7f
18 changed files with 1857 additions and 16 deletions

View File

@@ -22,23 +22,23 @@ To connect to MongoDB you can use an implementation of the `MongoDbFactory` inte
----
public interface MongoDbFactory {
/**
* Creates a default {@link DB} instance.
*
* @return the DB instance
* @throws DataAccessException
*/
DB getDb() throws DataAccessException;
/**
* Creates a default {@link DB} instance.
*
* @return the DB instance
* @throws DataAccessException
*/
DB getDb() throws DataAccessException;
/**
* Creates a {@link DB} instance to access the database with the given name.
*
* @param dbName must not be {@literal null} or empty.
*
* @return the DB instance
* @throws DataAccessException
*/
DB getDb(String dbName) throws DataAccessException;
/**
* Creates a {@link DB} instance to access the database with the given name.
*
* @param dbName must not be {@literal null} or empty.
*
* @return the DB instance
* @throws DataAccessException
*/
DB getDb(String dbName) throws DataAccessException;
}
----
@@ -290,3 +290,120 @@ and other attributes that are common across all other inbound adapters (e.g., 'c
The example above is relatively simple and static since it has a literal value for the `collection-name`.
Sometimes you may need to change this value at runtime based on some condition.
To do that, simply use `collection-name-expression` where the provided expression can be any valid SpEL expression.
[[mongodb-outbound-gateway]]
=== MongoDB Outbound Gateway
Starting with _version 5.0_, the MongoDb Outbound Gateway is provided and it allows you to query a database by sending a Message to its request channel.
The gateway will then send the response to the reply channel.
The Message payload and headers can be used to specify the query, as well as collection name.
[source,xml]
----
<int-mongodb:outbound-gateway id="gatewayQuery"
mongodb-factory="mongoDbFactory"
mongo-converter="mongoConverter"
query="{firstName: 'Bob'}"
collection-name="foo"
request-channel="in"
reply-channel="out"
entity-class="org.springframework.integration.mongodb.test.entity$Person"/>
----
* `collection-name` or `collection-name-expression` - identifies the name of the MongoDb collection to use;
* `mongo-converter` - reference to an instance of `o.s.data.mongodb.core.convert.MongoConverter` to assist with converting a raw java object to a JSON document representation
* `mongodb-factory` - reference to an instance of `o.s.data.mongodb.MongoDbFactory`
* `mongo-template` - reference to an instance of `o.s.data.mongodb.core.MongoTemplate` (NOTE: you can not have both mongo-template and mongodb-factory set)
* `entity-class` - the fully qualified name of the entity class to be passed to `find(..)` or `findOne(..)` method in MongoTemplate.
If this attribute is not provided the default value is `org.bson.Document`;
* `query` or `query-expression` - specifies the MongoDb query.
Please refer to http://www.mongodb.org/display/DOCS/Querying[MongoDB documentation] for more query samples.
==== Configuring with Java Configuration
The following Spring Boot application provides an example of configuring the outbound gateway using Java configuration:
[source, java]
----
@SpringBootApplication
public class MongoDbJavaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MongoDbJavaApplication.class)
.web(false)
.run(args);
}
@Autowired
private MongoDbFactory mongoDbFactory;
@Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}
@Bean
public MessageChannel replyChannel() {
return new QueueChannel(5);
}
@Bean
@ServiceActivator(inputChannel = "requestChannel")
public MessageHandler mongoDbOutboundGateway() {
MongoDbOutboundGateway gateway = new MongoDbOutboundGateway(this.mongoDbFactory);
gateway.setCollectionNameExpressionString("'foo'");
gateway.setQueryExpressionString("'{''name'' : ''Bob''}'");
gateway.setExpectSingleResult(true);
gateway.setEntityClass(Person.class);
gateway.setOutputChannelName("replyChannel");
return gateway;
}
@Bean
@ServiceActivator(inputChannel = "replyChannel")
public MessageHandler handler() {
return message -> System.out.println(message.getPayload());
}
}
----
==== Configuring with the Java DSL
The following Spring Boot application provides an example of configuring the Outbound Gateway using the Java DSL:
[source, java]
----
@SpringBootApplication
public class MongoDbJavaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MongoDbJavaApplication.class)
.web(false)
.run(args);
}
@Autowired
private MongoDbFactory;
@Autowired
private MongoConverter;
@Bean
public IntegrationFlow gatewaySingleQueryFlow() {
return f -> f
.handle(queryOutboundGateway())
.channel(c -> c.queue("retrieveResults"));
}
private MongoDbOutboundGatewaySpec queryOutboundGateway() {
return MongoDb.outboundGateway(this.mongoDbFactory, this.mongoConverter)
.query("{name : 'Bob'}")
.collectionNameFunction(m -> m.getHeaders().get("collection"))
.expectSingleResult(true)
.entityClass(Person.class);
}
}
----

View File

@@ -9,6 +9,10 @@ development process.
[[x5.0-new-components]]
=== New Components
==== MongoDB Outbound Gateway
The new `MongoDbOutboundGateway` allows you to make queries to the database on demand by sending a message to its request channel.
See <<mongodb-outbound-gateway>> for more information.
[[x5.0-general]]
=== General Changes