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.Flux
|
||||||
import reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
import org.springframework.context.annotation.Lazy
|
import org.springframework.context.annotation.Lazy
|
||||||
|
import org.springframework.messaging.handler.annotation.DestinationVariable
|
||||||
|
import org.springframework.messaging.handler.annotation.MessageMapping
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@ -56,6 +58,16 @@ class HomeController(@Autowired @Lazy private val userService: UserService) {
|
|||||||
suspend fun messages(@PathVariable userName: String) = coroutineScope {
|
suspend fun messages(@PathVariable userName: String) = coroutineScope {
|
||||||
userService.getUserMessages(userName)
|
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)
|
data class MyData(val id: Int, val name2: String)
|
@ -8,37 +8,49 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
import kotlinx.coroutines.flow.asFlow
|
import kotlinx.coroutines.flow.asFlow
|
||||||
import org.jooq.Condition
|
import org.jooq.Condition
|
||||||
import org.jooq.DSLContext
|
import org.jooq.DSLContext
|
||||||
|
import org.jooq.Record
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.context.annotation.Lazy
|
import org.springframework.context.annotation.Lazy
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import reactor.core.publisher.Flux
|
import reactor.core.publisher.Flux
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Lazy
|
@Lazy
|
||||||
class UserService(@Autowired @Lazy private val dsl: DSLContext) {
|
class UserService(@Autowired @Lazy private val dsl: DSLContext) {
|
||||||
|
|
||||||
fun users(): Flux<User> {
|
fun users(): Flux<User> = Flux
|
||||||
return Flux
|
.from(dsl
|
||||||
.from(dsl
|
.select()
|
||||||
.select()
|
.from(Tables.USERS))
|
||||||
.from(Tables.USERS))
|
.map(Query::toUser)
|
||||||
.map {
|
|
||||||
User(it.get(USERS.ID), it.get(USERS.NAME))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun users2(): Flow<User> {
|
suspend fun users2(): Flow<User> {
|
||||||
return dsl
|
return dsl
|
||||||
.select()
|
.select()
|
||||||
.from(USERS)
|
.from(USERS)
|
||||||
.fetch()
|
.fetch()
|
||||||
.map { userRecord ->
|
.map(Query::toUser)
|
||||||
User(userRecord.get(USERS.ID), userRecord.get(USERS.NAME))
|
|
||||||
}
|
|
||||||
.asFlow()
|
.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
|
suspend fun getUserMessages(userName: String): Flow<String> = dsl
|
||||||
.select(MESSAGES.MESSAGE).from(MESSAGES, USERS)
|
.select(MESSAGES.MESSAGE).from(MESSAGES, USERS)
|
||||||
.where(MESSAGES.USER_ID.eq(USERS.ID), USERS.NAME.eq(userName))
|
.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 {
|
private object Query {
|
||||||
val messages = listOf(MESSAGES.MESSAGE)
|
val messages = listOf(MESSAGES.MESSAGE)
|
||||||
val complexClause: Condition = MESSAGES.MESSAGE.eq("")
|
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.driverClassName=com.mysql.cj.jdbc.Driver
|
||||||
spring.datasource.username=test_user
|
spring.datasource.username=test_user
|
||||||
spring.datasource.password=password
|
spring.datasource.password=password
|
||||||
|
|
||||||
|
spring.rsocket.server.port=7000
|
||||||
|
spring.rsocket.server.transport=tcp
|
Loading…
Reference in New Issue
Block a user