Skip to content

Commit

Permalink
clean the sse sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Dec 18, 2017
1 parent 986b379 commit c8494c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
38 changes: 13 additions & 25 deletions sse/src/main/java/com/example/demo/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
import java.time.Duration;
import java.util.Date;
import java.util.stream.Stream;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

/**
*
* @author hantsy
*/
@RestController
Expand All @@ -32,33 +28,25 @@ public PostController(PostRepository posts) {
this.posts = posts;
}

@GetMapping(value = "")
@GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Flux<Post> all() {
return this.posts.findAll();
}

@GetMapping(value = "/stream", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Post> allStream() {
return Flux.interval(Duration.ofSeconds(1L)).flatMap((oneSecond)->this.posts.findAll());
}

@GetMapping(value = "/{id}")
public Mono<Post> get(@PathVariable(value = "id") Long id) {
return this.posts.findById(id);
@GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Post> stream() {
return Flux.interval(Duration.ofSeconds(1L)).flatMap((oneSecond) -> this.posts.findAll());
}

@GetMapping(value = "/{id}/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<PostEvent> getEvents(@PathVariable(value = "id") Long id) {

return this.posts.findById(id).flatMapMany(post -> {
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
Flux<PostEvent> postEventFlux = Flux.fromStream(Stream.generate(() -> new PostEvent(post, new Date())));
return Flux.zip(interval, postEventFlux).map(Tuple2::getT2);
});
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Post> sse() {
return Flux
.zip(Flux.interval(Duration.ofSeconds(1)), this.posts.findAll().repeat())
.map(Tuple2::getT2);
}

@PostMapping(value = "")
public Mono<Post> create(Post post) {
@PostMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<Post> create(@RequestBody Post post) {
return this.posts.save(post);
}

Expand Down
22 changes: 0 additions & 22 deletions sse/src/main/java/com/example/demo/PostEvent.java

This file was deleted.

1 change: 1 addition & 0 deletions sse/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.port=8080

0 comments on commit c8494c4

Please sign in to comment.