簡體   English   中英

如何通過 Spring Data Mongo 中的 @Tailable 查詢向 SSE 公開數據

[英]How to expose data to SSE from a @Tailable query in Spring Data Mongo

在閱讀了 Spring 數據 MongoDB 的@Tailable的文檔后,我認為將它用於消息通知很好。


@SpringBootApplication
class ServerApplication {

    @Bean
    fun runner(template: ReactiveMongoTemplate) = CommandLineRunner {
        println("running CommandLineRunner...")
        template.executeCommand("{\"convertToCapped\": \"messages\", size: 100000}");
    }

    fun main(args: Array<String>) {
        runApplication<ServerApplication>(*args)
    }

}

---------


@RestController()
@RequestMapping(value = ["messages"])
@CrossOrigin(origins = ["http://localhost:4200"])
class MessageController(private val messages: MessageRepository) {

    @PostMapping
    fun hello(p: String) =
            this.messages.save(Message(body = p, sentAt = Instant.now())).log().then()

    @GetMapping(produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
    fun messageStream(): Flux<Message> = this.messages.getMessagesBy().log()
}


-----------



interface MessageRepository : ReactiveMongoRepository<Message, String> {
    @Tailable
    fun getMessagesBy(): Flux<Message>
}


------------


@Document(collection = "messages")
data class Message(@Id var id: String? = null, var body: String, var sentAt: Instant = Instant.now())

如何實施?

我自己做的,檢查我的解決方案

我自己解決了這個問題,檢查示例代碼

此外,在 medium 上發表了一篇文章來演示如何使用 Angular 編寫的 SPA 客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM