Skip to content

Commit 922d864

Browse files
committed
KTLN-587 added reactive DSL module
1 parent 59aa383 commit 922d864

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

kotlin-reactive-dsl/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>kotlin-reactive-dsl</artifactId>
7+
<packaging>jar</packaging>
8+
9+
<name>kotlin-reactive-dsl</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-boot-3</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>../parent-boot-3</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-webflux</artifactId>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<sourceDirectory>src/main/kotlin</sourceDirectory>
27+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
28+
<plugins>
29+
<plugin>
30+
<artifactId>kotlin-maven-plugin</artifactId>
31+
<executions>
32+
<execution>
33+
<id>compile</id>
34+
<phase>compile</phase>
35+
<goals>
36+
<goal>compile</goal>
37+
</goals>
38+
</execution>
39+
<execution>
40+
<id>test-compile</id>
41+
<phase>test-compile</phase>
42+
<goals>
43+
<goal>test-compile</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
<groupId>org.jetbrains.kotlin</groupId>
48+
<version>${kotlin.version}</version>
49+
<configuration>
50+
<args>
51+
<arg>-Xjsr305=strict</arg>
52+
</args>
53+
<jvmTarget>1.8</jvmTarget>
54+
<compilerPlugins>
55+
<plugin>spring</plugin>
56+
<plugin>jpa</plugin>
57+
</compilerPlugins>
58+
</configuration>
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.jetbrains.kotlin</groupId>
62+
<artifactId>kotlin-maven-allopen</artifactId>
63+
<version>${kotlin.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.jetbrains.kotlin</groupId>
67+
<artifactId>kotlin-maven-noarg</artifactId>
68+
<version>${kotlin.version}</version>
69+
</dependency>
70+
</dependencies>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.functional_dsl
2+
3+
class User(
4+
var userId: Long,
5+
var name: String,
6+
var age: Int,
7+
var country: String,
8+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.functional_dsl
2+
3+
import java.lang.IllegalArgumentException
4+
import java.util.function.Consumer
5+
import kotlinx.coroutines.flow.Flow
6+
import kotlinx.coroutines.flow.flow
7+
import org.springframework.stereotype.Component
8+
import reactor.core.publisher.Mono
9+
10+
@Component
11+
class UsersRepository {
12+
13+
private val userStorage: MutableMap<Long, User>
14+
15+
init {
16+
userStorage = mutableMapOf(
17+
1L to User(1L, "Alex", 18, "Malta"),
18+
2L to User(2L, "Eugen", 36, "Austria"),
19+
3L to User(3L, "Jenny", 12, "Canada")
20+
)
21+
}
22+
23+
fun findUserById(id: Long) : Mono<User> {
24+
return Mono
25+
.fromCallable { userStorage[id] }
26+
.handle { value, sink ->
27+
if (value == null) {
28+
sink.error(IllegalArgumentException())
29+
} else {
30+
sink.next(value)
31+
}
32+
}
33+
}
34+
35+
fun createUsers(user: Mono<User>) {
36+
user.doOnNext {
37+
userStorage[it.userId] = it
38+
}.subscribe()
39+
}
40+
41+
suspend fun findUserByIdForCoroutines(id: Long) : Flow<User> {
42+
return flow {
43+
val user = userStorage[id] ?: User(0L, "Name", 0, "USA")
44+
emit(user)
45+
}
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.functional_dsl.coroutines
2+
3+
import com.baeldung.functional_dsl.UsersRepository;
4+
import com.baeldung.functional_dsl.User;
5+
import kotlinx.coroutines.flow.Flow
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.boot.autoconfigure.SpringBootApplication
8+
import org.springframework.boot.runApplication
9+
import org.springframework.context.annotation.Bean
10+
import org.springframework.context.annotation.Import
11+
import org.springframework.web.reactive.function.server.ServerResponse
12+
import org.springframework.web.reactive.function.server.bodyAndAwait
13+
import org.springframework.web.reactive.function.server.coRouter
14+
15+
@Import(UsersRepository::class)
16+
@SpringBootApplication
17+
open class AppCoroutines {
18+
19+
@Autowired
20+
private lateinit var usersRepository: UsersRepository
21+
22+
@Bean
23+
open fun registerForCo() =
24+
coRouter {
25+
GET("/users/{id}") {
26+
println("here we are!")
27+
val customers : Flow<User> = usersRepository.findUserByIdForCoroutines(
28+
it.pathVariable("id").toLong()
29+
)
30+
ServerResponse.ok().bodyAndAwait(customers)
31+
}
32+
}
33+
}
34+
35+
fun main(vararg args: String) {
36+
runApplication<AppCoroutines>(*args) {}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.functional_dsl.router_function
2+
3+
import com.baeldung.functional_dsl.UsersRepository;
4+
import com.baeldung.functional_dsl.User;
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.boot.autoconfigure.SpringBootApplication
7+
import org.springframework.boot.runApplication
8+
import org.springframework.context.annotation.Bean
9+
import org.springframework.context.annotation.Import
10+
import org.springframework.web.reactive.function.server.RouterFunction
11+
import org.springframework.web.reactive.function.server.RouterFunctions
12+
import org.springframework.web.reactive.function.server.ServerResponse
13+
import org.springframework.web.reactive.function.server.body
14+
import org.springframework.web.reactive.function.server.router
15+
16+
@Import(UsersRepository::class)
17+
@SpringBootApplication
18+
open class ReactiveConfig {
19+
20+
@Autowired
21+
private lateinit var usersRepository: UsersRepository
22+
23+
@Bean
24+
open fun configure() : RouterFunction<ServerResponse> {
25+
return RouterFunctions.route()
26+
.GET("/users/{id}") {
27+
ServerResponse
28+
.ok()
29+
.body(usersRepository.findUserById(it.pathVariable("id").toLong()))
30+
}
31+
.POST("/create") {
32+
usersRepository.createUsers(it.bodyToMono(User::class.java))
33+
return@POST ServerResponse
34+
.ok()
35+
.build()
36+
}
37+
.build()
38+
}
39+
}
40+
41+
fun main(vararg args: String) {
42+
runApplication<ReactiveConfig>(*args) {}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.functional_dsl.router_function_dsl
2+
3+
import com.baeldung.functional_dsl.UsersRepository;
4+
import com.baeldung.functional_dsl.User;
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.boot.autoconfigure.SpringBootApplication
7+
import org.springframework.boot.runApplication
8+
import org.springframework.context.annotation.Bean
9+
import org.springframework.context.annotation.Import
10+
import org.springframework.web.reactive.function.server.RouterFunction
11+
import org.springframework.web.reactive.function.server.RouterFunctions
12+
import org.springframework.web.reactive.function.server.ServerResponse
13+
import org.springframework.web.reactive.function.server.body
14+
import org.springframework.web.reactive.function.server.router
15+
16+
@Import(UsersRepository::class)
17+
@SpringBootApplication
18+
open class ReactiveDslConfig {
19+
20+
@Autowired
21+
private lateinit var usersRepository: UsersRepository
22+
23+
@Bean
24+
open fun endpoints() = router {
25+
GET("/users/{id}") {
26+
ServerResponse
27+
.ok()
28+
.body(usersRepository.findUserById(it.pathVariable("id").toLong()))
29+
}
30+
31+
POST("/create") {
32+
usersRepository.createUsers(it.bodyToMono(User::class.java))
33+
return@POST ServerResponse
34+
.ok()
35+
.build()
36+
}
37+
}
38+
}
39+
40+
fun main(vararg args: String) {
41+
runApplication<ReactiveDslConfig>(*args) {}
42+
}

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@
503503
<module>kotlin-json</module>
504504
<module>kotlin-json-2</module>
505505
<module>kotlin-dsl</module>
506+
<module>kotlin-reactive-dsl</module>
506507
<!-- <module>kotlin-ktor</module> --> <!-- not a Maven module -->
507508
<module>kotlin-lambda</module>
508509
<module>kotlin-libraries</module>
@@ -585,6 +586,8 @@
585586
<!-- <module>kotlin-js</module> --> <!-- not a Maven module -->
586587
<module>kotlin-json</module>
587588
<module>kotlin-json-2</module>
589+
<module>kotlin-dsl</module>
590+
<module>kotlin-reactive-dsl</module>
588591
<!-- <module>kotlin-ktor</module> --> <!-- not a Maven module -->
589592
<module>kotlin-lambda</module>
590593
<module>kotlin-libraries</module>

0 commit comments

Comments
 (0)