Skip to content

Commit

Permalink
Resposta com event streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ImGabreuw committed Aug 12, 2021
1 parent da2dc18 commit 35a309a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/main/java/me/gabreuw/springwebflux/PlaylistHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import me.gabreuw.springwebflux.document.Playlist;
import me.gabreuw.springwebflux.service.PlaylistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlaylistHandler {

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/me/gabreuw/springwebflux/PlaylistRouter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package me.gabreuw.springwebflux;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;

@Configuration
public class PlaylistRouter {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package me.gabreuw.springwebflux.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.gabreuw.springwebflux.document.Playlist;
import me.gabreuw.springwebflux.service.PlaylistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import static org.springframework.http.HttpStatus.CREATED;
import java.time.Duration;

import static org.springframework.http.MediaType.TEXT_EVENT_STREAM_VALUE;

@RestController
@RequestMapping(path = "/playlists")
@Log4j2
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlaylistController {

Expand All @@ -27,9 +33,21 @@ public Mono<Playlist> findById(@PathVariable String id) {
}

@PostMapping
@ResponseStatus(code = CREATED)
public Mono<Playlist> save(@RequestBody Playlist playlist) {
return PLAYLIST_SERVICE.save(playlist);
}

@GetMapping(
path = "/events",
produces = TEXT_EVENT_STREAM_VALUE
)
public Flux<Tuple2<Long, Playlist>> findByEvents() {
Flux<Long> interval = Flux.interval(Duration.ofSeconds(5));
Flux<Playlist> events = PLAYLIST_SERVICE.findAll();

log.info("Events passou aqui");

return Flux.zip(interval, events);
}

}

0 comments on commit 35a309a

Please sign in to comment.