Spring Boot Web Flux with JOOQ for interfacing with DB and Kotlin coroutines to make blocking JDBC calls run asynchronously. Now with rsockets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.1 KiB

  1. package com.example.demo.controller
  2. import com.example.demo.model.User
  3. import com.example.demo.service.UserService
  4. import kotlinx.coroutines.*
  5. import kotlinx.coroutines.flow.Flow
  6. import kotlinx.coroutines.flow.toList
  7. import org.springframework.beans.factory.annotation.Autowired
  8. import org.springframework.web.bind.annotation.GetMapping
  9. import org.springframework.web.bind.annotation.PathVariable
  10. import org.springframework.web.bind.annotation.RestController
  11. import reactor.core.publisher.Flux
  12. import reactor.core.publisher.Mono
  13. import org.springframework.context.annotation.Lazy
  14. import org.springframework.messaging.handler.annotation.DestinationVariable
  15. import org.springframework.messaging.handler.annotation.MessageMapping
  16. import org.springframework.web.bind.annotation.RequestMapping
  17. @RestController
  18. @Lazy
  19. @RequestMapping("/api")
  20. class HomeRestController(@Autowired @Lazy private val userService: UserService) {
  21. @GetMapping("/")
  22. fun index(): Mono<String> {
  23. return Mono.just("foo")
  24. }
  25. @GetMapping("/data")
  26. fun dataHandler(): Mono<MyData> {
  27. return Mono.just(MyData(1, "hello2"))
  28. }
  29. @GetMapping("/data2")
  30. suspend fun dataHandler2(): MyData = withContext(Dispatchers.IO) {
  31. delay(10_000)
  32. MyData(1, "hello3")
  33. }
  34. @GetMapping("/users")
  35. fun users(): Flux<User> {
  36. return userService.users()
  37. }
  38. @GetMapping("/users2")
  39. suspend fun users2(): Flow<User> {
  40. return userService.users2()
  41. }
  42. @GetMapping("/users3")
  43. suspend fun users3() = coroutineScope {
  44. val fun1 = async {
  45. userService.users2().toList()
  46. }
  47. Pair(fun1.await(), 1)
  48. }
  49. @GetMapping("/messages/{userName}")
  50. suspend fun messages(@PathVariable userName: String) = coroutineScope {
  51. userService.getUserMessages(userName)
  52. }
  53. @MessageMapping("messages.findAll")
  54. suspend fun all() = coroutineScope {
  55. userService.getAllMessages()
  56. }
  57. @MessageMapping("users.{name}")
  58. suspend fun getUser(@DestinationVariable name: String) = coroutineScope {
  59. userService.getUserByName(name)
  60. }
  61. }
  62. data class MyData(val id: Int, val name2: String)