[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:
parent
f514212ceb
commit
4b6f457c33
|
@ -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)
|
|
@ -8,7 +8,7 @@
|
|||
<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>
|
||||
|
@ -16,6 +16,13 @@
|
|||
<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.40</kotlin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -56,8 +63,24 @@
|
|||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</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>
|
||||
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-libs-snapshot</id>
|
||||
|
@ -65,7 +88,7 @@
|
|||
<url>http://repo.spring.io/libs-snapshot</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/kotlin</sourceDirectory>
|
||||
<plugins>
|
||||
|
@ -112,12 +135,4 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</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>
|
||||
|
|
|
@ -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<String>) {
|
||||
SpringApplication.run(Application::class.java, *args)
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Event, String>
|
||||
|
||||
@Document
|
||||
data class Event(val id: String, val name: String)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
}
|
||||
@GetMapping(value = "/save", produces = arrayOf(MediaType.TEXT_EVENT_STREAM_VALUE))
|
||||
fun executeExample(@RequestParam("eventName") eventName: String) =
|
||||
eventRepository.save(Event(UUID.randomUUID().toString(), eventName)).flux()
|
||||
}
|
||||
|
|
|
@ -1,33 +1,39 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
<form method="get" action="/saveEvent">
|
||||
<!-- Save new event -->
|
||||
<form method="get" action="/save">
|
||||
<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-->
|
||||
<!-- Receive saved event -->
|
||||
<div id="content"></div>
|
||||
|
||||
<script>
|
||||
var source = new EventSource("receiveChanges");
|
||||
|
||||
|
||||
let source = new EventSource("save");
|
||||
|
||||
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>";
|
||||
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>
|
||||
|
|
Loading…
Reference in New Issue