Added rsocket test code
This commit is contained in:
parent
3e669b704d
commit
3c0692394d
@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RestController
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import org.springframework.context.annotation.Lazy
|
||||
import org.springframework.messaging.handler.annotation.DestinationVariable
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping
|
||||
|
||||
|
||||
@RestController
|
||||
@ -56,6 +58,16 @@ class HomeController(@Autowired @Lazy private val userService: UserService) {
|
||||
suspend fun messages(@PathVariable userName: String) = coroutineScope {
|
||||
userService.getUserMessages(userName)
|
||||
}
|
||||
|
||||
@MessageMapping("messages.findAll")
|
||||
suspend fun all() = coroutineScope {
|
||||
userService.getAllMessages()
|
||||
}
|
||||
|
||||
@MessageMapping("users.{name}")
|
||||
suspend fun getUser(@DestinationVariable name: String) = coroutineScope {
|
||||
userService.getUserByName(name)
|
||||
}
|
||||
}
|
||||
|
||||
data class MyData(val id: Int, val name2: String)
|
@ -8,37 +8,49 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
import org.jooq.Condition
|
||||
import org.jooq.DSLContext
|
||||
import org.jooq.Record
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.context.annotation.Lazy
|
||||
import org.springframework.stereotype.Service
|
||||
import reactor.core.publisher.Flux
|
||||
import java.util.*
|
||||
|
||||
|
||||
@Service
|
||||
@Lazy
|
||||
class UserService(@Autowired @Lazy private val dsl: DSLContext) {
|
||||
|
||||
fun users(): Flux<User> {
|
||||
return Flux
|
||||
.from(dsl
|
||||
.select()
|
||||
.from(Tables.USERS))
|
||||
.map {
|
||||
User(it.get(USERS.ID), it.get(USERS.NAME))
|
||||
}
|
||||
}
|
||||
fun users(): Flux<User> = Flux
|
||||
.from(dsl
|
||||
.select()
|
||||
.from(Tables.USERS))
|
||||
.map(Query::toUser)
|
||||
|
||||
|
||||
suspend fun users2(): Flow<User> {
|
||||
return dsl
|
||||
.select()
|
||||
.from(USERS)
|
||||
.fetch()
|
||||
.map { userRecord ->
|
||||
User(userRecord.get(USERS.ID), userRecord.get(USERS.NAME))
|
||||
}
|
||||
.map(Query::toUser)
|
||||
.asFlow()
|
||||
}
|
||||
|
||||
suspend fun getUserByName(userName: String): Optional<User> = dsl
|
||||
.select(USERS.ID, USERS.NAME)
|
||||
.from(USERS)
|
||||
.where(USERS.NAME.eq(userName))
|
||||
.limit(1)
|
||||
.fetchOptional()
|
||||
.map(Query::toUser)
|
||||
|
||||
|
||||
suspend fun getAllMessages(): Flow<String> = dsl
|
||||
.select(MESSAGES.MESSAGE).from(MESSAGES)
|
||||
.fetch()
|
||||
.map { it.get(MESSAGES.MESSAGE) }
|
||||
.asFlow()
|
||||
|
||||
suspend fun getUserMessages(userName: String): Flow<String> = dsl
|
||||
.select(MESSAGES.MESSAGE).from(MESSAGES, USERS)
|
||||
.where(MESSAGES.USER_ID.eq(USERS.ID), USERS.NAME.eq(userName))
|
||||
@ -56,5 +68,6 @@ class UserService(@Autowired @Lazy private val dsl: DSLContext) {
|
||||
private object Query {
|
||||
val messages = listOf(MESSAGES.MESSAGE)
|
||||
val complexClause: Condition = MESSAGES.MESSAGE.eq("")
|
||||
fun toUser(r: Record) = User(r.get(USERS.ID), r.get(USERS.NAME))
|
||||
}
|
||||
}
|
||||
|
@ -5,3 +5,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/test_db
|
||||
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.username=test_user
|
||||
spring.datasource.password=password
|
||||
|
||||
spring.rsocket.server.port=7000
|
||||
spring.rsocket.server.transport=tcp
|
Loading…
Reference in New Issue
Block a user