Skip to content

Commit 414fcf1

Browse files
committed
refine
1 parent f98048e commit 414fcf1

File tree

4 files changed

+40
-61
lines changed

4 files changed

+40
-61
lines changed

source/includes/integrations/spring-data-tutorial-code/main/java/MongoConfig.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.data.mongodb.core.MongoTemplate;
1111

12-
import javax.net.ssl.SSLContext;
13-
import java.nio.file.Paths;
14-
import nl.altindag.ssl.SSLFactory;
15-
1612
// start-mongoconfig
1713
@Configuration
1814
public class MongoConfig {
@@ -21,35 +17,13 @@ public class MongoConfig {
2117
@Value("${mongodb.database}")
2218
private String databaseName;
2319

24-
@Value("${truststore.path}")
25-
private String trustStorePath;
26-
@Value("${truststore.pwd}")
27-
private String trustStorePwd;
28-
29-
@Value("${mongodb.atlas}")
30-
private boolean atlas;
31-
3220
@Bean
3321
public MongoClient mongo() {
3422

3523
ConnectionString connectionString = new ConnectionString(uri);
3624

3725
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
3826
.applyConnectionString(connectionString)
39-
.applyToSslSettings(builder -> {
40-
if (!atlas) {
41-
// Use SSLContext if a trustStore has been provided
42-
if (!trustStorePath.isEmpty()) {
43-
SSLFactory sslFactory = SSLFactory.builder()
44-
.withTrustMaterial(Paths.get(trustStorePath), trustStorePwd.toCharArray())
45-
.build();
46-
SSLContext sslContext = sslFactory.getSslContext();
47-
builder.context(sslContext);
48-
builder.invalidHostNameAllowed(true);
49-
}
50-
}
51-
builder.enabled(true);
52-
})
5327
.build();
5428

5529

source/includes/integrations/spring-data-tutorial-code/main/java/Products.java renamed to source/includes/integrations/spring-data-tutorial-code/main/java/Product.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
// start-products-class
1313
@Document("products")
14-
public class Products {
14+
public class Product {
1515

1616
private static final Logger LOG = LoggerFactory
1717
.getLogger(Products.class);
@@ -25,7 +25,7 @@ public class Products {
2525
private Date unavailable;
2626
private String skuId;
2727

28-
public Products(String name, int qty, double price, Date available, Date unavailable, String skuId) {
28+
public Product(String name, int qty, double price, Date available, Date unavailable, String skuId) {
2929
this.name = name;
3030
this.qty = qty;
3131
this.price = price;
@@ -34,15 +34,15 @@ public Products(String name, int qty, double price, Date available, Date unavail
3434
this.skuId = skuId;
3535
}
3636

37-
public static Products [] RandomProducts( int count) {
37+
public static Product [] RandomProducts( int count) {
3838

3939
Faker faker = new Faker();
4040
Random rand = new Random();
4141

42-
Products [] retProds = new Products[count];
42+
Product [] retProds = new Product[count];
4343
for (int i=0; i<count; ++i) {
4444

45-
Products p = new Products( faker.animal().name(),
45+
Product p = new Product( faker.animal().name(),
4646
1+rand.nextInt(998),
4747
10.0+rand.nextInt(9999),
4848
new Date(), new Date(),

source/includes/integrations/spring-data-tutorial-code/main/java/ProductRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void updateProductQuantity(String name, int newQuantity) {
3232
Update update = new Update();
3333
update.set("quantity", newQuantity);
3434

35-
UpdateResult result = mongoTemplate.updateFirst(query, update, Products.class);
35+
UpdateResult result = mongoTemplate.updateFirst(query, update, Product.class);
3636

3737
if(result == null)
3838
LOG.error("No documents updated");
@@ -43,14 +43,14 @@ public void updateProductQuantity(String name, int newQuantity) {
4343
public int bulkInsertProducts(int count) {
4444

4545
LOG.info("Dropping collection...");
46-
mongoTemplate.dropCollection(Products.class);
46+
mongoTemplate.dropCollection(Product.class);
4747
LOG.info("Dropped!");
4848

4949
Instant start = Instant.now();
5050
mongoTemplate.setWriteConcern(WriteConcern.W1.withJournal(true));
5151

52-
Products [] productList = Products.RandomProducts(count);
53-
BulkOperations bulkInsertion = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Products.class);
52+
Product [] productList = Product.RandomProducts(count);
53+
BulkOperations bulkInsertion = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Product.class);
5454

5555
for (int i=0; i<productList.length; ++i)
5656
bulkInsertion.insert(productList[i]);

source/integrations/spring-data-integration.txt

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Overview
2424

2525
In this tutorial, you can learn how to use `Spring Data
2626
MongoDB <https://spring.io/projects/spring-data-mongodb>`__ with the
27-
{+driver-short+} to build a bulk write application.
27+
{+driver-short+} to perform high-performance bulk inserts in a Spring Boot application.
2828

2929
Spring Data MongoDB
3030
~~~~~~~~~~~~~~~~~~~
@@ -51,7 +51,7 @@ page of the Spring Framework documentation.
5151
The Spring Data BulkOperations Interface
5252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5353

54-
BulkOperations is an Spring Framework Data interface that contains a list of write
54+
BulkOperations is a Spring Data MongoDB interface that contains a list of write
5555
operations that can be applied to your database. It can handle any combination of
5656
the following operations which map to similar {+driver-long+} operations:
5757

@@ -65,8 +65,8 @@ the following operations which map to similar {+driver-long+} operations:
6565

6666
A BulkOperation can be ordered or unordered. Ordered bulk operations run operations
6767
sequentially, and if an error is detected, return with an error code.
68-
Unordered operations are run in parallel, which means they are typically faster
69-
faster. However, you must manually check if there were errors during the operations.
68+
Unordered operations are run in parallel, which means they are typically faster.
69+
However, you must manually check if there were errors during the operations.
7070

7171
For more information about bulk operations, see the following resources:
7272

@@ -83,7 +83,7 @@ You can find the completed sample app for this tutorial in the
8383
:github:`SpringDataBulkInsert sample project
8484
</mongodb-developer/SpringDataMongoBulkInsert>` GitHub repository.
8585

86-
.. note:: Imports Not specified
86+
.. note:: Imports Not Specified
8787

8888
The import statements required for the files in the tutorial have not been included on the page. See the :github:`GitHub repository</mongodb-developer/SpringDataMongoBulkInsert>` for complete files.
8989

@@ -112,7 +112,7 @@ of the Spring Data MongoDB documentation, and the :ref:`Compatibility
112112

113113
If you used the `Spring Initializr <https://start.spring.io/>`__ or a clone of the :github:`Spring Boot sample project <spring-guides/gs-spring-boot>` to create your project, versioning compatibility has already been accounted for, and the ``spring-boot-starter-data-mongodb`` component will already be included in your ``pom.xml`` file.
114114

115-
Add the dependencies to your ``pom.xml``:
115+
Add the following dependencies to your ``pom.xml``:
116116

117117
.. code-block:: xml
118118

@@ -127,12 +127,13 @@ Add the dependencies to your ``pom.xml``:
127127
<version>2.4.3</version>
128128
</dependency>
129129

130-
The ``datafaker`` dependency is used to generate a large quantity of Product objects.
130+
The ``datafaker`` dependency is used to generate a large quantity of ``Product``
131+
objects to use in the bulk write operation.
131132

132-
MongoClient Configuration
133-
~~~~~~~~~~~~~~~~~~~~~~~~~
133+
Configure your MongoClient
134+
~~~~~~~~~~~~~~~~~~~~~~~~~~
134135

135-
The ``MongodConfig`` class contains the configuration for the ``MongoClient``
136+
The ``MongoConfig`` class contains the configuration for the ``MongoClient``
136137
that will allow the Spring Data framework to interact with the MongoDB server,
137138
and sets other configuration options. For more information about configuration
138139
options, see the :ref:`Specify Connection Options <specify-mongoclient-settings>`
@@ -150,15 +151,21 @@ Spring Data framework guide:
150151
- ``@Value`` annotations: `Using @Value
151152
<https://docs.spring.io/spring-framework/docs/5.3.22/reference/html/core.html#beans-value-annotations>`__
152153

153-
Create configuration and template classes to set up your MongoDB connection by adding the
154-
following code to a ``MongoConfig.java`` file:
154+
Create a ``MongoConfig.java`` file and add the following configuration and
155+
template classes to set up your MongoDB connection:
155156

156157
.. literalinclude:: /includes/integrations/spring-data-tutorial-code/main/java/MongoConfig.java
157158
:start-after: // start-mongoconfig
158159
:end-before: // end-mongoconfig
159160
:language: java
160161
:dedent:
161162

163+
.. note:: API vs Interface
164+
165+
This implementation uses the ``MongoTemplate`` API, rather than extending a
166+
Spring Data repository interface such as ``MongoRepository``, to allow for
167+
fine-grained control over bulk operations.
168+
162169
Set the values of your connection string (``mongodb.uri``),
163170
database name (``mongodb.database``), and bulk operation count (``documentCount``) in your ``application.properties`` file:
164171

@@ -190,24 +197,23 @@ the `How the _id field is handled in the mapping layer
190197
<https://docs.spring.io/spring-data/mongodb/reference/mongodb/mapping/mapping.html#mapping.conventions.id-field>`__
191198
section of the Spring Data MongoDB documentation.
192199

193-
Create your ``Products`` class and map it to your ``products`` collection by
194-
adding the following code to a ``Products.java`` file:
200+
Create a ``Product.java`` file to define your ``Product`` class and map it to
201+
your ``products`` collection with the following code:
195202

196-
.. literalinclude:: /includes/integrations/spring-data-tutorial-code/main/java/Products.java
203+
.. literalinclude:: /includes/integrations/spring-data-tutorial-code/main/java/Product.java
197204
:start-after: // start-products-class
198205
:end-before: // end-products-class
199206
:language: java
200207
:dedent:
201208

202-
The ``Products`` class includes a static method that generates an array of
209+
The ``Product`` class includes a static method that generates an array of
203210
``Product`` objects. You may also define getters and setters for each field.
204211

205-
Implement a Repository to Store Your Products
206-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212+
Define a Repository to Store Your Products
213+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207214

208-
The ``ProductRepository`` will manage a collection of ``Products``.
209-
When you implement your ``ProductRepository``, you must reference the
210-
client creation methods defined in the ``MongoConfig`` class, which were
215+
The ``ProductRepository`` will manage a collection of ``Product`` objects.
216+
Your ``ProductRepository`` references the client creation methods defined in the ``MongoConfig`` class, which you
211217
annotated with ``@Bean``. By using the ``@Autowired`` annotation with the
212218
``mongoTemplate`` variable together with a constructor that includes
213219
``mongoTemplate`` as an argument, the Spring container uses constructor
@@ -216,8 +222,8 @@ constructor injection, see the `Constructor-based Dependency Injection
216222
<https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html#beans-constructor-injection>`__
217223
section of the Spring Framework documentation.
218224

219-
Create the ``ProductRepository`` class that manages a collection of ``Products``
220-
by adding the following code to a ``ProductRepository.java`` file:
225+
Create a ``ProductRepository.java`` file and define your ``ProductRepository``
226+
class to manage a collection of ``Product`` objects with the following code:
221227

222228
.. literalinclude:: /includes/integrations/spring-data-tutorial-code/main/java/ProductRepository.java
223229
:start-after: // start-productrepo
@@ -232,11 +238,10 @@ Perform a Bulk Operation
232238
~~~~~~~~~~~~~~~~~~~~~~~~
233239

234240
The main application class triggers the ``ProductRepository`` to generate the
235-
specified number of ``Products`` and save them to your MongoDB database. It uses
241+
specified number of ``Product`` objects and save them to your MongoDB database. It uses
236242
the ``@Autowired`` annotation to inject a ``ProductRepository``, and implements logging.
237243

238-
Add the following code to a class of the same name as your project file, and add
239-
the following code:
244+
Add the following code to your main class to run your application:
240245

241246
.. literalinclude:: /includes/integrations/spring-data-tutorial-code/main/java/MyProjectName.java
242247
:start-after: // start-application

0 commit comments

Comments
 (0)