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 @@ + + + +
+ + +
+ + + +
+ + + +