Skip to content

Instantly share code, notes, and snippets.

View josefigueredo's full-sized avatar
🚀

Jose Figueredo josefigueredo

🚀
View GitHub Profile
data class Tweet(
@JsonProperty("uuid")
val uuid: String = UUID.randomUUID().toString(),
@JsonProperty("text")
val text: String,
@JsonProperty("at")
val at: ZonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"))
)
fun stream(serverRequest: ServerRequest) =
ServerResponse
.ok()
.sse()
.body(
tweetRepository
.listen()
.asPublisher()
)
fun publish(monoTweet: Mono<Tweet>) =
monoTweet
.flatMap {
redisTemplate
.convertAndSend(
TWEET_CHANNEL,
it // it: Tweet!
)
}
fun listen(): Flow<Tweet> =
redisTemplate
.listenToChannelAsFlow(
TWEET_CHANNEL
)
.map {
it.message // it: reactiveSubscription.Message<String, Tweet!>
}
@josefigueredo
josefigueredo / routes.kt
Created November 11, 2020 17:54
Routes for the example of sse
@Bean
fun routes(tweetHandler: TweetHandler) =
router {
accept(MediaType.APPLICATION_JSON)
.nest {
POST("/tweet",
tweetHandler::tweet)
}
accept(MediaType.TEXT_EVENT_STREAM)
.nest {
@josefigueredo
josefigueredo / schedulers.json
Created October 23, 2020 20:45
Graphana dashboard for Spring Reactor
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
package com.josefigueredo.examples.waysofroute.router
import com.josefigueredo.examples.waysofroute.handler.ReactiveHandler
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType.TEXT_HTML
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.router
@Component
package com.josefigueredo.examples.waysofroute.router
import com.josefigueredo.examples.waysofroute.handler.CoroutineHandler
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType.TEXT_HTML
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.coRouter
@Component
package com.josefigueredo.examples.waysofroute.router
import com.josefigueredo.examples.waysofroute.handler.ReactiveHandler
import kotlinx.coroutines.flow.Flow
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/annotated-reactive")
package com.josefigueredo.examples.waysofroute.router
import com.josefigueredo.examples.waysofroute.handler.CoroutineHandler
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*
@RestController