From c32bbe3be776add922ef0ec5c4e27ceaf1d5e2d6 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 16 Apr 2018 21:09:15 +0200 Subject: [PATCH] [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 --- spring-data-5-reactive/pom.xml | 124 ++++++++++++++++++ .../main/kotlin/com/baeldung/Application.kt | 12 ++ .../src/main/kotlin/com/baeldung/Event.kt | 6 + .../kotlin/com/baeldung/EventRepository.kt | 5 + .../main/kotlin/com/baeldung/MongoConfig.kt | 33 +++++ .../main/kotlin/com/baeldung/SendEmitter.kt | 43 ++++++ .../src/main/resources/static/index.html | 33 +++++ 7 files changed, 256 insertions(+) create mode 100644 spring-data-5-reactive/pom.xml create mode 100644 spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt create mode 100644 spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt create mode 100644 spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt create mode 100644 spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt create mode 100644 spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt create mode 100644 spring-data-5-reactive/src/main/resources/static/index.html diff --git a/spring-data-5-reactive/pom.xml b/spring-data-5-reactive/pom.xml new file mode 100644 index 0000000000..85d025bd9c --- /dev/null +++ b/spring-data-5-reactive/pom.xml @@ -0,0 +1,124 @@ + + + 4.0.0 + + com.baeldung + spring-5-data-reactive + 0.0.1-SNAPSHOT + jar + + Spring-5-data-reactive + Spring-5-data-reactive with Springboot 2.0.1 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 1.2.20 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + + + org.springframework.boot + spring-boot-starter-web + + + javax.ws.rs + javax.ws.rs-api + 2.1 + + + com.fasterxml.jackson.module + jackson-module-kotlin + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + + + org.jetbrains.kotlin + kotlin-reflect + + + org.projectlombok + lombok + + + io.projectreactor + reactor-test + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + + + + spring-libs-snapshot + Spring Snapshot Repository + http://repo.spring.io/libs-snapshot + + + + + src/main/kotlin + + + org.springframework.boot + spring-boot-maven-plugin + + + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + org.jetbrains.kotlin + + + -Xjsr305=strict + + + spring + + 1.8 + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + + + + diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt b/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt new file mode 100644 index 0000000000..b21dd6bfb5 --- /dev/null +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt @@ -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) { + SpringApplication.run(Application::class.java, *args) +} + diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt b/spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt new file mode 100644 index 0000000000..17fa9699a8 --- /dev/null +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/Event.kt @@ -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) diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt b/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt new file mode 100644 index 0000000000..33d4b85a93 --- /dev/null +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt @@ -0,0 +1,5 @@ +package com.baeldung + +import org.springframework.data.mongodb.repository.ReactiveMongoRepository + +interface EventRepository : ReactiveMongoRepository diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt b/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt new file mode 100644 index 0000000000..a45a630f38 --- /dev/null +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt @@ -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) + } +} diff --git a/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt b/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt new file mode 100644 index 0000000000..bc879e10df --- /dev/null +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt @@ -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 { + // 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 + } +} diff --git a/spring-data-5-reactive/src/main/resources/static/index.html b/spring-data-5-reactive/src/main/resources/static/index.html new file mode 100644 index 0000000000..8fbb3b6b05 --- /dev/null +++ b/spring-data-5-reactive/src/main/resources/static/index.html @@ -0,0 +1,33 @@ + + + +
+ + +
+ + + +
+ + + +