토비의 봄 TV 14회 스프링 리액티브 프로그래밍 (10) Flux의 특징과 활용방법IT/Spring Framework2018. 2. 4. 00:06
Table of Contents
(시청일 : 20180114)
■ 예제 1
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<Event> hello(@PathVariable long id) {
return Mono.just(new Event(id, "event" + id));
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/event/123
{"id":123,"value":"event123"}
■ 예제 2
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<Event> hello(@PathVariable long id) {
return Mono.just(new Event(id, "event" + id));
}
@GetMapping("/events")
Flux<Event> events() {
return Flux.just(new Event(1L, "event1"), new Event(2L, "event2"));
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
[{"id":1,"value":"event1"},{"id":2,"value":"event2"}]
■ 예제 3
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping("/events")
Flux<Event> events() {
return Flux.just(new Event(1L, "event1"), new Event(2L, "event2"));
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/event/1
[{"id":1,"value":"event1"},{"id":2,"value":"event2"}]
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
[{"id":1,"value":"event1"},{"id":2,"value":"event2"}]
■ 예제 3
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping(value="/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Event> events() {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Flux.fromIterable(list);
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
data:{"id":1,"value":"event1"}
data:{"id":2,"value":"event2"}
<Flux>
<Flux.fromIterable()>
■ 예제 4
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping(value="/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Event> events() {
return Flux
.fromStream(Stream.generate(() -> new Event(System.currentTimeMillis(), "value")))
.delayElements(Duration.ofSeconds(1))
.take(10);
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
data:{"id":1516531806083,"value":"value"}
data:{"id":1516531806192,"value":"value"}
data:{"id":1516531806194,"value":"value"}
data:{"id":1516531806195,"value":"value"}
data:{"id":1516531806210,"value":"value"}
data:{"id":1516531806213,"value":"value"}
data:{"id":1516531806215,"value":"value"}
data:{"id":1516531806216,"value":"value"}
data:{"id":1516531806217,"value":"value"}
data:{"id":1516531806219,"value":"value"}
<Flux.take()>
■ 예제 5
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping(value="/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Event> events() {
return Flux
.<Event>generate(sink -> sink.next(new Event(System.currentTimeMillis(), "value")))
.delayElements(Duration.ofSeconds(1))
.take(10);
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
data:{"id":1516533395000,"value":"value"}
data:{"id":1516533396018,"value":"value"}
data:{"id":1516533397019,"value":"value"}
data:{"id":1516533398021,"value":"value"}
data:{"id":1516533399023,"value":"value"}
data:{"id":1516533400023,"value":"value"}
data:{"id":1516533401024,"value":"value"}
data:{"id":1516533402025,"value":"value"}
data:{"id":1516533403026,"value":"value"}
data:{"id":1516533404027,"value":"value"}
■ 예제 6
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping(value="/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Event> events() {
return Flux
.<Event, Long>generate(()->1L, (id, sink) -> {
sink.next(new Event(id, "value" + id));
return id+1;
})
.delayElements(Duration.ofSeconds(1))
.take(10);
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
data:{"id":1,"value":"value1"}
data:{"id":2,"value":"value2"}
data:{"id":3,"value":"value3"}
data:{"id":4,"value":"value4"}
data:{"id":5,"value":"value5"}
data:{"id":6,"value":"value6"}
data:{"id":7,"value":"value7"}
data:{"id":8,"value":"value8"}
data:{"id":9,"value":"value9"}
data:{"id":10,"value":"value10"}
■ 예제 7
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping(value="/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Event> events() {
Flux<Event> es = Flux
.<Event, Long>generate(()->1L, (id, sink) -> {
sink.next(new Event(id, "value" + id));
return id+1;
});
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
return Flux.zip(es, interval).map(tu->tu.getT1()).take(10);
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
data:{"id":1,"value":"value1"}
data:{"id":2,"value":"value2"}
data:{"id":3,"value":"value3"}
data:{"id":4,"value":"value4"}
data:{"id":5,"value":"value5"}
data:{"id":6,"value":"value6"}
data:{"id":7,"value":"value7"}
data:{"id":8,"value":"value8"}
data:{"id":9,"value":"value9"}
data:{"id":10,"value":"value10"}
■ 예제 8
package toby.tobytv014;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
@SpringBootApplication
@Slf4j
@RestController
public class Tobytv014Application {
@GetMapping("/event/{id}")
Mono<List<Event>> hello(@PathVariable long id) {
List<Event> list = Arrays.asList(new Event(1L, "event1"), new Event(2L, "event2"));
return Mono.just(list);
}
@GetMapping(value="/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Event> events() {
Flux<String> es = Flux.generate(sink -> sink.next("Value"));
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
return Flux.zip(es, interval).map(tu -> new Event(tu.getT2()+1, tu.getT1())).take(10);
}
public static void main(String[] args) {
SpringApplication.run(Tobytv014Application.class, args);
}
@Data @AllArgsConstructor
public static class Event {
long id;
String value;
}
}
<Terminal>
C:\Users\Soohyeon\Desktop\curl_7_53_1_openssl_nghttp2_x64>curl localhost:8080/events
data:{"id":1,"value":"Value"}
data:{"id":2,"value":"Value"}
data:{"id":3,"value":"Value"}
data:{"id":4,"value":"Value"}
data:{"id":5,"value":"Value"}
data:{"id":6,"value":"Value"}
data:{"id":7,"value":"Value"}
data:{"id":8,"value":"Value"}
data:{"id":9,"value":"Value"}
data:{"id":10,"value":"Value"}
<Flux.generate()>
<Flux.interval()>
<Flux.zip()>
'IT > Spring Framework' 카테고리의 다른 글
ORM, JPA, Hibernate란? (0) | 2018.02.14 |
---|---|
서블릿(Servlet)과 JSP(Java Sever Pages) (0) | 2018.02.14 |
토비의 봄 TV 13회 스프링 리액티브 프로그래밍 (9) Mono의 동작방식과 block() (0) | 2018.02.03 |
토비의 봄 TV 12회 스프링 리액티브 프로그래밍 (8) WebFlux (0) | 2018.02.03 |
토비의 봄 TV 11회 스프링 리액티브 프로그래밍 (7) CompletableFuture (0) | 2018.02.03 |
@DEAN :: Dean Story
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!