[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
This commit is contained in:
Jorge 2018-04-27 14:38:48 +02:00 committed by Grzegorz Piwowarek
parent f514212ceb
commit 4b6f457c33
7 changed files with 50 additions and 61 deletions

View File

@ -16,6 +16,13 @@
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </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.40</kotlin.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -56,6 +63,22 @@
<artifactId>kotlin-stdlib-jdk8</artifactId> <artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version> <version>${kotlin.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
@ -112,12 +135,4 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<kotlin.version>1.2.20</kotlin.version>
<javax.ws.rs-api.version>2.1</javax.ws.rs-api.version>
</properties>
</project> </project>

View File

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

View File

@ -1,5 +1,9 @@
package com.baeldung package com.baeldung
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.ReactiveMongoRepository import org.springframework.data.mongodb.repository.ReactiveMongoRepository
interface EventRepository : ReactiveMongoRepository<Event, String> interface EventRepository : ReactiveMongoRepository<Event, String>
@Document
data class Event(val id: String, val name: String)

View File

@ -13,21 +13,13 @@ import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRep
@EnableReactiveMongoRepositories(basePackageClasses = arrayOf(EventRepository::class)) @EnableReactiveMongoRepositories(basePackageClasses = arrayOf(EventRepository::class))
class MongoConfig : AbstractReactiveMongoConfiguration() { class MongoConfig : AbstractReactiveMongoConfiguration() {
override fun reactiveMongoClient(): com.mongodb.reactivestreams.client.MongoClient { override fun reactiveMongoClient(): MongoClient = mongoClient()
return mongoClient()
}
@Bean @Bean
fun mongoClient(): MongoClient { fun mongoClient(): MongoClient = MongoClients.create()
return MongoClients.create()
}
override fun getDatabaseName(): String { override fun getDatabaseName(): String = "mongoDatabase"
return "mongoDatabase"
}
@Bean @Bean
override fun reactiveMongoTemplate(): ReactiveMongoTemplate { override fun reactiveMongoTemplate(): ReactiveMongoTemplate = ReactiveMongoTemplate(mongoClient(), databaseName)
return ReactiveMongoTemplate(mongoClient(), databaseName)
}
} }

View File

@ -1,43 +1,16 @@
package com.baeldung package com.baeldung
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController 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 java.util.*
import javax.ws.rs.core.MediaType
@RestController @RestController
class SendEmitter(val eventRepository: EventRepository) { class SendEmitter(val eventRepository: EventRepository) {
private var emitter = SseEmitter() @GetMapping(value = "/save", produces = arrayOf(MediaType.TEXT_EVENT_STREAM_VALUE))
fun executeExample(@RequestParam("eventName") eventName: String) =
/** eventRepository.save(Event(UUID.randomUUID().toString(), eventName)).flux()
* 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

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