【问题标题】:How to expose data to SSE from a @Tailable query in Spring Data Mongo如何从 Spring Data Mongo 中的 @Tailable 查询向 SSE 公开数据
【发布时间】:2020-11-01 15:12:59
【问题描述】:

看了Spring Data 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())

如何实现?

我自己做的,查看我的solution

【问题讨论】:

    标签: spring mongodb spring-webflux spring-data-mongodb reactive


    【解决方案1】:

    我自己解决了这个问题,请查看sample codes

    另外,published a post 在 medium 上演示如何使用 Angular 编写的 SPA 客户端。

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 2022-01-17
      • 2012-07-21
      • 2013-05-20
      • 1970-01-01
      • 2020-07-04
      • 2020-04-29
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多