Added Simple Reactive Event Server and Client Example
This commit is contained in:
parent
974637a273
commit
b8c78d9a8e
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.reactive;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ReactiveEventClient implements CommandLineRunner {
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("server.port","8090");
|
||||
SpringApplication.run(ReactiveEventClient.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
WebClient webclient = WebClient.builder()
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
.baseUrl("http://localhost:8080/events")
|
||||
.build();
|
||||
|
||||
webclient.get().uri("/stream/").header(HttpHeaders.CONTENT_TYPE,MediaType.TEXT_HTML.toString())
|
||||
.retrieve().bodyToFlux(String.class)
|
||||
.subscribe(System.out::println);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.reactive.controller;
|
||||
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/events")
|
||||
public class ReactiveEventServer {
|
||||
|
||||
@GetMapping(path = "/stream/" , produces=MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<String> eventStream() {
|
||||
return Flux.interval(Duration.ofSeconds(1)).map(tick -> {return "Event:" + tick;});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue