Skip to content

Commit c32bbe3

Browse files
jorgecolladopivovarit
authored andcommitted
[Bael 1687] - Spring Data Reactive Mongo DB microservice in Kotlin (eugenp#3993)
* [BAEL-1641] Find all pairs of numbers in an array that add up to a given sum * Commiting editor's suggested changes * Commiting article Spring Data Reactive Mongo DB microservice in Kotlin * Revert commit for BAEL 1687 - Moving those files to a new branch * [BAEL-1687] - Real-time data streaming using Reactive MongoDB and Kotlin * Reverting changes [BAEL-1641] - Not from this branch * [BAEL-1687] - Code Peer Review - Added suggested changes
1 parent 72b5ac2 commit c32bbe3

7 files changed

Lines changed: 256 additions & 0 deletions

File tree

‎spring-data-5-reactive/pom.xml‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-5-data-reactive</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Spring-5-data-reactive</name>
12+
<description>Spring-5-data-reactive with Springboot 2.0.1</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<kotlin.version>1.2.20</kotlin.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.ws.rs</groupId>
39+
<artifactId>javax.ws.rs-api</artifactId>
40+
<version>2.1</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.fasterxml.jackson.module</groupId>
44+
<artifactId>jackson-module-kotlin</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.jetbrains.kotlin</groupId>
48+
<artifactId>kotlin-stdlib-jdk8</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.jetbrains.kotlin</groupId>
52+
<artifactId>kotlin-reflect</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.projectlombok</groupId>
56+
<artifactId>lombok</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.projectreactor</groupId>
60+
<artifactId>reactor-test</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.jetbrains.kotlin</groupId>
65+
<artifactId>kotlin-stdlib-jdk8</artifactId>
66+
<version>${kotlin.version}</version>
67+
</dependency>
68+
</dependencies>
69+
70+
<repositories>
71+
<repository>
72+
<id>spring-libs-snapshot</id>
73+
<name>Spring Snapshot Repository</name>
74+
<url>http://repo.spring.io/libs-snapshot</url>
75+
</repository>
76+
</repositories>
77+
78+
<build>
79+
<sourceDirectory>src/main/kotlin</sourceDirectory>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-maven-plugin</artifactId>
84+
</plugin>
85+
<plugin>
86+
<artifactId>kotlin-maven-plugin</artifactId>
87+
<version>${kotlin.version}</version>
88+
<executions>
89+
<execution>
90+
<id>compile</id>
91+
<phase>compile</phase>
92+
<goals>
93+
<goal>compile</goal>
94+
</goals>
95+
</execution>
96+
<execution>
97+
<id>test-compile</id>
98+
<phase>test-compile</phase>
99+
<goals>
100+
<goal>test-compile</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
<groupId>org.jetbrains.kotlin</groupId>
105+
<configuration>
106+
<args>
107+
<arg>-Xjsr305=strict</arg>
108+
</args>
109+
<compilerPlugins>
110+
<plugin>spring</plugin>
111+
</compilerPlugins>
112+
<jvmTarget>1.8</jvmTarget>
113+
</configuration>
114+
<dependencies>
115+
<dependency>
116+
<groupId>org.jetbrains.kotlin</groupId>
117+
<artifactId>kotlin-maven-allopen</artifactId>
118+
<version>${kotlin.version}</version>
119+
</dependency>
120+
</dependencies>
121+
</plugin>
122+
</plugins>
123+
</build>
124+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.jetbrains.kotlin.demo
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
6+
@SpringBootApplication
7+
class Application
8+
9+
fun main(args: Array<String>) {
10+
SpringApplication.run(Application::class.java, *args)
11+
}
12+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung
2+
3+
import org.springframework.data.mongodb.core.mapping.Document
4+
5+
@Document
6+
data class Event(val id: String, val name: String)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung
2+
3+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
4+
5+
interface EventRepository : ReactiveMongoRepository<Event, String>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung
2+
3+
import com.mongodb.reactivestreams.client.MongoClient
4+
import com.mongodb.reactivestreams.client.MongoClients
5+
import org.springframework.context.annotation.Bean
6+
import org.springframework.context.annotation.Configuration
7+
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration
8+
import org.springframework.data.mongodb.core.ReactiveMongoTemplate
9+
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories
10+
11+
12+
@Configuration
13+
@EnableReactiveMongoRepositories(basePackageClasses = arrayOf(EventRepository::class))
14+
class MongoConfig : AbstractReactiveMongoConfiguration() {
15+
16+
override fun reactiveMongoClient(): com.mongodb.reactivestreams.client.MongoClient {
17+
return mongoClient()
18+
}
19+
20+
@Bean
21+
fun mongoClient(): MongoClient {
22+
return MongoClients.create()
23+
}
24+
25+
override fun getDatabaseName(): String {
26+
return "mongoDatabase"
27+
}
28+
29+
@Bean
30+
override fun reactiveMongoTemplate(): ReactiveMongoTemplate {
31+
return ReactiveMongoTemplate(mongoClient(), databaseName)
32+
}
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung
2+
3+
import org.springframework.web.bind.annotation.GetMapping
4+
import org.springframework.web.bind.annotation.RequestParam
5+
import org.springframework.web.bind.annotation.RestController
6+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
7+
import reactor.core.publisher.Flux
8+
import reactor.core.publisher.Mono
9+
import java.util.*
10+
import javax.ws.rs.core.MediaType
11+
12+
13+
@RestController
14+
class SendEmitter(val eventRepository: EventRepository) {
15+
16+
private var emitter = SseEmitter()
17+
18+
/**
19+
* Save and send an SSE to all subscribed clients
20+
*/
21+
@GetMapping("/saveEvent")
22+
fun executeExample(@RequestParam("eventName") eventName: String): Flux<Event> {
23+
// Create new event
24+
var event = Event(UUID.randomUUID().toString(), eventName)
25+
// Save event
26+
var stream = eventRepository.saveAll(Mono.just(event))
27+
// Send event
28+
emitter.send(SseEmitter.event().data(event))
29+
// Return SSE
30+
return stream
31+
}
32+
33+
/**
34+
* Receive SSEs
35+
*/
36+
@GetMapping(value = "/receiveChanges")
37+
fun handle(): SseEmitter {
38+
// Create new emitter
39+
this.emitter = SseEmitter()
40+
// Return SSE
41+
return emitter
42+
}
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<html>
2+
<body>
3+
4+
<form method="get" action="/saveEvent">
5+
<input type="text" name="eventName">
6+
<button type="submit">Save new event</button>
7+
</form>
8+
9+
10+
<!-- Here will be painted each new received Server-Sent event's content-->
11+
<div id="content"></div>
12+
13+
<script>
14+
var source = new EventSource("receiveChanges");
15+
16+
source.addEventListener('message', function (e) {
17+
console.log('New message is received');
18+
const index = JSON.parse(e.data);
19+
const content = "New event added: " + index.name + "<br>";
20+
document.getElementById("content").innerHTML += content;
21+
}, false);
22+
source.addEventListener('open', function(e) {
23+
console.log('The connection has been opened');
24+
}, false);
25+
source.addEventListener('error', function(e) {
26+
if (e.readyState == EventSource.CLOSED){
27+
console.log('The connection has been closed');
28+
}
29+
}, false);
30+
31+
</script>
32+
</body>
33+
<html>

0 commit comments

Comments
 (0)