I am going
to use embedded mongo db for this application.
Step 1:
Create new maven
project ‘springWebflux’.
Step 2:
Update pom.xml with maven
dependencies.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springWebflux</groupId>
<artifactId>springWebflux</artifactId>
<version>1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<!-- <scope>test</scope> -->
</dependency>
</dependencies>
</project>
Step 3:
Create a package
'com.sample.app' and define App.java like below.
package com.sample.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Total
project structure looks like below.
Run
App.java, you can see below messages in console.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2019-08-08 13:22:55.301 INFO 39294 --- [ main] com.sample.app.App : Starting App on C02X902SJGH5 with PID 39294 (/Users/krishna/Documents/EclipseWorkSpaces/Learnings/springWebflux/target/classes started by krishna in /Users/krishna/Documents/EclipseWorkSpaces/Learnings/springWebflux)
2019-08-08 13:22:55.304 INFO 39294 --- [ main] com.sample.app.App : No active profile set, falling back to default profiles: default
2019-08-08 13:22:55.790 INFO 39294 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-08-08 13:22:55.813 INFO 39294 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 15ms. Found 0 repository interfaces.
2019-08-08 13:22:55.817 INFO 39294 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-08-08 13:22:55.818 INFO 39294 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1ms. Found 0 repository interfaces.
2019-08-08 13:22:56.650 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : starting...
2019-08-08 13:22:58.667 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : DownloadSize: 81088927
2019-08-08 13:22:58.668 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 0 %
2019-08-08 13:23:00.678 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 10 %
2019-08-08 13:23:01.800 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 20 %
2019-08-08 13:23:02.420 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 30 %
2019-08-08 13:23:02.881 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 40 %
2019-08-08 13:23:04.714 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 50 %
2019-08-08 13:23:05.978 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 60 %
2019-08-08 13:23:07.626 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 70 %
2019-08-08 13:23:08.501 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 80 %
2019-08-08 13:23:09.093 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 90 %
2019-08-08 13:23:09.637 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : 100 %
2019-08-08 13:23:09.637 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : downloaded with 7918kb/s
2019-08-08 13:23:09.638 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Download Version{3.5.5}:OS_X:B64 : finished
2019-08-08 13:23:09.646 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Extract /Users/krishna/.embedmongo/osx/mongodb-osx-x86_64-3.5.5.tgz : starting...
2019-08-08 13:23:10.407 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Extract /Users/krishna/.embedmongo/osx/mongodb-osx-x86_64-3.5.5.tgz : extract mongodb-macOS-x86_64-3.5.5/bin/mongod
2019-08-08 13:23:10.407 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Extract /Users/krishna/.embedmongo/osx/mongodb-osx-x86_64-3.5.5.tgz : nothing left
2019-08-08 13:23:10.408 INFO 39294 --- [ main] o.s.b.a.mongo.embedded.EmbeddedMongo : Extract /Users/krishna/.embedmongo/osx/mongodb-osx-x86_64-3.5.5.tgz : finished
2019-08-08 13:23:10.581 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : note: noprealloc may hurt performance in many applications
2019-08-08 13:23:10.592 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.591+0530 I CONTROL [initandlisten] MongoDB starting : pid=39296 port=49337 dbpath=/var/folders/z6/sfz8h4s11bg905bg7yf0g6340000gq/T/embedmongo-db-e4f48c75-6980-485d-b5ee-420bb822a30b 64-bit host=C02X902SJGH5
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] db version v3.5.5
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] git version: 98515c812b6fa893613f063dae568ff8319cbfbd
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] allocator: system
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] modules: none
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] build environment:
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] distarch: x86_64
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] target_arch: x86_64
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.592+0530 I CONTROL [initandlisten] options: { net: { bindIp: "127.0.0.1", http: { enabled: false }, port: 49337 }, security: { authorization: "disabled" }, storage: { dbPath: "/var/folders/z6/sfz8h4s11bg905bg7yf0g6340000gq/T/embedmongo-db-e4f48c75-6980-485d-b5ee-420bb822a30b", journal: { enabled: false }, mmapv1: { preallocDataFiles: false, smallFiles: true }, syncPeriodSecs: 0.0 } }
2019-08-08 13:23:10.593 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.593+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=0,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),,log=(enabled=false),
2019-08-08 13:23:10.803 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.803+0530 W STORAGE [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2019-08-08 13:23:10.803 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.803+0530 I CONTROL [initandlisten]
2019-08-08 13:23:10.803 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.803+0530 I CONTROL [initandlisten] ** NOTE: This is a development version (3.5.5) of MongoDB.
2019-08-08 13:23:10.804 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.803+0530 I CONTROL [initandlisten] ** Not recommended for production.
2019-08-08 13:23:10.804 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.803+0530 I CONTROL [initandlisten]
2019-08-08 13:23:10.860 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.860+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/folders/z6/sfz8h4s11bg905bg7yf0g6340000gq/T/embedmongo-db-e4f48c75-6980-485d-b5ee-420bb822a30b/diagnostic.data'
2019-08-08 13:23:10.940 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.940+0530 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2019-08-08 13:23:10.940 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.940+0530 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-08-08 13:23:10.946 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.946+0530 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
2019-08-08 13:23:10.946 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.946+0530 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2019-08-08 13:23:10.946 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:10.946+0530 I NETWORK [thread1] waiting for connections on port 49337
2019-08-08 13:23:10.947 INFO 39294 --- [ main] d.f.embed.mongo.MongodExecutable : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@28d79cba
2019-08-08 13:23:11.071 INFO 39294 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:49337], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-08-08 13:23:11.071 INFO 39294 --- [ main] org.mongodb.driver.cluster : Adding discovered server localhost:49337 to client view of cluster
2019-08-08 13:23:11.103 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:11.103+0530 I NETWORK [thread1] connection accepted from 127.0.0.1:49342 #1 (1 connection now open)
2019-08-08 13:23:11.113 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:11.113+0530 I NETWORK [conn1] received client metadata from 127.0.0.1:49342 conn1: { driver: { name: "mongo-java-driver", version: "3.8.2" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "10.14.4" }, platform: "Java/Oracle Corporation/1.8.0_191-b12" }
2019-08-08 13:23:11.129 INFO 39294 --- [localhost:49337] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:49337
2019-08-08 13:23:11.131 INFO 39294 --- [localhost:49337] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:49337, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 5, 5]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=937881}
2019-08-08 13:23:11.132 INFO 39294 --- [localhost:49337] org.mongodb.driver.cluster : Discovered cluster type of STANDALONE
2019-08-08 13:23:11.389 INFO 39294 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:49337], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-08-08 13:23:11.466 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:11.466+0530 I NETWORK [thread1] connection accepted from 127.0.0.1:49343 #2 (2 connections now open)
2019-08-08 13:23:11.510 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:23:11.509+0530 I NETWORK [conn2] received client metadata from 127.0.0.1:49343 conn2: { driver: { name: "mongo-java-driver|mongo-java-driver-reactivestreams", version: "3.8.2|1.9.2" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "10.14.4" }, platform: "Java/Oracle Corporation/1.8.0_191-b12" }
2019-08-08 13:23:11.527 INFO 39294 --- [localhost:49337] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:49337
2019-08-08 13:23:11.529 INFO 39294 --- [localhost:49337] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:49337, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 5, 5]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=1450951}
2019-08-08 13:23:11.591 INFO 39294 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2019-08-08 13:23:11.594 INFO 39294 --- [ main] com.sample.app.App : Started App in 16.694 seconds (JVM running for 17.154)
2019-08-08 13:24:09.226 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:24:09.226+0530 I NETWORK [thread1] connection accepted from 127.0.0.1:49348 #3 (3 connections now open)
2019-08-08 13:24:09.226 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:24:09.226+0530 I NETWORK [thread1] connection accepted from 127.0.0.1:49349 #4 (4 connections now open)
2019-08-08 13:24:09.226 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:24:09.226+0530 I - [conn3] end connection 127.0.0.1:49348 (4 connections now open)
2019-08-08 13:24:09.588 INFO 39294 --- [ Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo : 2019-08-08T13:24:09.588+0530 I - [conn4] end connection 127.0.0.1:49349 (3 connections now open)
As you see
the console messages, you can see that
mongoDB is downloaded and started at port 49337.
You can
download complete working application from this link.
No comments:
Post a Comment