From 4b6f457c339a816aa46b103ad3dfc55454fbe091 Mon Sep 17 00:00:00 2001 From: Jorge Date: Fri, 27 Apr 2018 14:38:48 +0200 Subject: [PATCH] [Bael 1687] - Code Peer Review (#4034) * [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 * [BAEL-1687] - Code Peer Review - Grzegorz's code refactor & delete unnecessary code * [BAEL-1687] - Code Peer Review - Grzegorz's code refactor & delete unnecessary code * [BAEL-1687] - Code Peer Review - Fixed emitter --- algorithms/README.md | 2 +- spring-data-5-reactive/pom.xml | 37 +++++++++++++------ .../main/kotlin/com/baeldung/Application.kt | 3 +- .../kotlin/com/baeldung/EventRepository.kt | 4 ++ .../main/kotlin/com/baeldung/MongoConfig.kt | 16 ++------ .../main/kotlin/com/baeldung/SendEmitter.kt | 35 ++---------------- .../src/main/resources/static/index.html | 14 +++++-- 7 files changed, 50 insertions(+), 61 deletions(-) diff --git a/algorithms/README.md b/algorithms/README.md index df445146d9..b3e11de6c5 100644 --- a/algorithms/README.md +++ b/algorithms/README.md @@ -20,4 +20,4 @@ - [A Maze Solver in Java](http://www.baeldung.com/java-solve-maze) - [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku) - [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words) -- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations) +- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations) \ No newline at end of file diff --git a/spring-data-5-reactive/pom.xml b/spring-data-5-reactive/pom.xml index 0eb933394d..710f6f9d67 100644 --- a/spring-data-5-reactive/pom.xml +++ b/spring-data-5-reactive/pom.xml @@ -8,7 +8,7 @@ jar Spring-5-data-reactive Spring-5-data-reactive with Springboot 2.0.1 - + org.springframework.boot spring-boot-starter-parent @@ -16,6 +16,13 @@ + + UTF-8 + UTF-8 + 1.8 + 1.2.40 + + org.springframework.boot @@ -56,8 +63,24 @@ kotlin-stdlib-jdk8 ${kotlin.version} + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + - + spring-libs-snapshot @@ -65,7 +88,7 @@ http://repo.spring.io/libs-snapshot - + src/main/kotlin @@ -112,12 +135,4 @@ - - - UTF-8 - UTF-8 - 1.2.20 - 2.1 - - 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 index b21dd6bfb5..af29a429a4 100644 --- a/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/Application.kt @@ -1,4 +1,4 @@ -package org.jetbrains.kotlin.demo +package com.baeldung import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication @@ -9,4 +9,3 @@ class Application fun main(args: Array) { SpringApplication.run(Application::class.java, *args) } - 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 index 33d4b85a93..a73ef8c807 100644 --- a/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/EventRepository.kt @@ -1,5 +1,9 @@ package com.baeldung +import org.springframework.data.mongodb.core.mapping.Document import org.springframework.data.mongodb.repository.ReactiveMongoRepository interface EventRepository : ReactiveMongoRepository + +@Document +data class Event(val id: String, val name: String) 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 index a45a630f38..64d51a176a 100644 --- a/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/MongoConfig.kt @@ -13,21 +13,13 @@ import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRep @EnableReactiveMongoRepositories(basePackageClasses = arrayOf(EventRepository::class)) class MongoConfig : AbstractReactiveMongoConfiguration() { - override fun reactiveMongoClient(): com.mongodb.reactivestreams.client.MongoClient { - return mongoClient() - } + override fun reactiveMongoClient(): MongoClient = mongoClient() @Bean - fun mongoClient(): MongoClient { - return MongoClients.create() - } + fun mongoClient(): MongoClient = MongoClients.create() - override fun getDatabaseName(): String { - return "mongoDatabase" - } + override fun getDatabaseName(): String = "mongoDatabase" @Bean - override fun reactiveMongoTemplate(): ReactiveMongoTemplate { - return ReactiveMongoTemplate(mongoClient(), databaseName) - } + override fun reactiveMongoTemplate(): ReactiveMongoTemplate = 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 index bc879e10df..6fa3118d8f 100644 --- a/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt +++ b/spring-data-5-reactive/src/main/kotlin/com/baeldung/SendEmitter.kt @@ -1,43 +1,16 @@ package com.baeldung +import org.springframework.http.MediaType 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 - } + @GetMapping(value = "/save", produces = arrayOf(MediaType.TEXT_EVENT_STREAM_VALUE)) + fun executeExample(@RequestParam("eventName") eventName: String) = + eventRepository.save(Event(UUID.randomUUID().toString(), eventName)).flux() } diff --git a/spring-data-5-reactive/src/main/resources/static/index.html b/spring-data-5-reactive/src/main/resources/static/index.html index 8fbb3b6b05..a0b8f6f884 100644 --- a/spring-data-5-reactive/src/main/resources/static/index.html +++ b/spring-data-5-reactive/src/main/resources/static/index.html @@ -1,33 +1,39 @@ -
+ +
- +