-
Notifications
You must be signed in to change notification settings - Fork 3
/
PersonReactiveApplication.kt
48 lines (39 loc) · 1.38 KB
/
PersonReactiveApplication.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cz.zubal.person
import io.codearte.jfairy.Fairy
import io.codearte.jfairy.producer.person.Person
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import java.time.Duration
@SpringBootApplication
class PersonReactiveApplication
fun main(args: Array<String>) {
runApplication<PersonReactiveApplication>(*args)
}
@RestController
@RequestMapping("/person")
class PersonController {
private val scheduler = Schedulers.newParallel(RestController::class.java.simpleName)
var fairy : Fairy = Fairy.create()
@GetMapping
fun flux(@RequestParam("number") number : Long) : Flux<Person> {
return mono()
.repeat()
.take(number)
}
private fun mono() : Mono<Person> {
return Mono
.fromCallable(this::generatePerson)
.delayElement(Duration.ofMillis(1000))
.subscribeOn(scheduler)
}
private fun generatePerson() : Person {
return fairy.person()
}
}