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.

49 lines
1.4 KiB

4 years ago
  1. package com.example.demo.service
  2. import com.example.demo.model.User
  3. import com.example.demo.model.test_db.Tables
  4. import com.example.demo.model.test_db.Tables.MESSAGES
  5. import com.example.demo.model.test_db.tables.Users.USERS
  6. import kotlinx.coroutines.flow.Flow
  7. import kotlinx.coroutines.flow.asFlow
  8. import org.jooq.DSLContext
  9. import org.springframework.beans.factory.annotation.Autowired
  10. import org.springframework.context.annotation.Lazy
  11. import org.springframework.stereotype.Service
  12. import reactor.core.publisher.Flux
  13. @Service
  14. @Lazy
  15. class UserService(@Autowired @Lazy private val dsl: DSLContext) {
  16. fun users(): Flux<User> {
  17. return Flux
  18. .from(dsl
  19. .select()
  20. .from(Tables.USERS))
  21. .map {
  22. User(it.get(USERS.ID), it.get(USERS.NAME))
  23. }
  24. }
  25. suspend fun users2(): Flow<User> {
  26. return dsl
  27. .select()
  28. .from(USERS)
  29. .fetch()
  30. .map { userRecord ->
  31. User(userRecord.get(USERS.ID), userRecord.get(USERS.NAME))
  32. }
  33. .asFlow()
  34. }
  35. suspend fun getUserMessages(userName: String): Flow<String> = dsl
  36. .select(MESSAGES.MESSAGE).from(MESSAGES, USERS)
  37. .where(MESSAGES.USER_ID.eq(USERS.ID), USERS.NAME.eq(userName))
  38. .fetch()
  39. .map { it.get(MESSAGES.MESSAGE) }
  40. .asFlow()
  41. }