[Bael 1687] - Spring Data Reactive Mongo DB microservice in Kotlin (#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
This commit is contained in:
Jorge 2018-04-16 21:09:15 +02:00 committed by Grzegorz Piwowarek
parent 72b5ac265c
commit c32bbe3be7
7 changed files with 256 additions and 0 deletions

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>com.baeldung</groupId>
<artifactId>spring-5-data-reactive</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring-5-data-reactive</name>
<description>Spring-5-data-reactive with Springboot 2.0.1</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.20</kotlin.version>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<name>Spring Snapshot Repository</name>
<url>http://repo.spring.io/libs-snapshot</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<groupId>org.jetbrains.kotlin</groupId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package org.jetbrains.kotlin.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class Application
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}

View File

@ -0,0 +1,6 @@
package com.baeldung
import org.springframework.data.mongodb.core.mapping.Document
@Document
data class Event(val id: String, val name: String)

View File

@ -0,0 +1,5 @@
package com.baeldung
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
interface EventRepository : ReactiveMongoRepository<Event, String>

View File

@ -0,0 +1,33 @@
package com.baeldung
import com.mongodb.reactivestreams.client.MongoClient
import com.mongodb.reactivestreams.client.MongoClients
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration
import org.springframework.data.mongodb.core.ReactiveMongoTemplate
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories
@Configuration
@EnableReactiveMongoRepositories(basePackageClasses = arrayOf(EventRepository::class))
class MongoConfig : AbstractReactiveMongoConfiguration() {
override fun reactiveMongoClient(): com.mongodb.reactivestreams.client.MongoClient {
return mongoClient()
}
@Bean
fun mongoClient(): MongoClient {
return MongoClients.create()
}
override fun getDatabaseName(): String {
return "mongoDatabase"
}
@Bean
override fun reactiveMongoTemplate(): ReactiveMongoTemplate {
return ReactiveMongoTemplate(mongoClient(), databaseName)
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.util.*
import javax.ws.rs.core.MediaType
@RestController
class SendEmitter(val eventRepository: EventRepository) {
private var emitter = SseEmitter()
/**
* Save and send an SSE to all subscribed clients
*/
@GetMapping("/saveEvent")
fun executeExample(@RequestParam("eventName") eventName: String): Flux<Event> {
// Create new event
var event = Event(UUID.randomUUID().toString(), eventName)
// Save event
var stream = eventRepository.saveAll(Mono.just(event))
// Send event
emitter.send(SseEmitter.event().data(event))
// Return SSE
return stream
}
/**
* Receive SSEs
*/
@GetMapping(value = "/receiveChanges")
fun handle(): SseEmitter {
// Create new emitter
this.emitter = SseEmitter()
// Return SSE
return emitter
}
}

View File

@ -0,0 +1,33 @@
<html>
<body>
<form method="get" action="/saveEvent">
<input type="text" name="eventName">
<button type="submit">Save new event</button>
</form>
<!-- Here will be painted each new received Server-Sent event's content-->
<div id="content"></div>
<script>
var source = new EventSource("receiveChanges");
source.addEventListener('message', function (e) {
console.log('New message is received');
const index = JSON.parse(e.data);
const content = "New event added: " + index.name + "<br>";
document.getElementById("content").innerHTML += content;
}, false);
source.addEventListener('open', function(e) {
console.log('The connection has been opened');
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED){
console.log('The connection has been closed');
}
}, false);
</script>
</body>
<html>