abh.swar@gmail.com: Code check-in for article on Spring WebFlux
1. EmailWebClient is the client that subscribes to the data from WebFlux server 2. EmailGenerator generates one email per second randomly 3. EmailHandler and EmailRouter deal with handling of request of the subscriber 4. Email is the POJO for data transmitted by the server
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.reactive.client;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.Disposable;
|
||||
|
||||
public class EmailWebClient {
|
||||
|
||||
public Disposable getEmails() {
|
||||
WebClient webClient = WebClient.builder()
|
||||
.baseUrl("http://localhost:8080")
|
||||
.build();
|
||||
|
||||
return webClient.get()
|
||||
.uri("/emails")
|
||||
.accept(MediaType.APPLICATION_STREAM_JSON)
|
||||
.exchange()
|
||||
.subscribe(email -> System.out.println("Email: " + email));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EmailWebClient vehiclesWebClient = new EmailWebClient();
|
||||
Disposable disposable = vehiclesWebClient.getEmails();
|
||||
try {
|
||||
// sleep for 5 minutes
|
||||
Thread.sleep(300000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
disposable.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.reactive.generator;
|
||||
|
||||
import com.baeldung.reactive.model.Email;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
public class EmailGenerator {
|
||||
private static Map<String, Email> emailMap = new HashMap<String, Email>() {
|
||||
{
|
||||
put("emailBob", new Email("Hello", "Hello Bob"));
|
||||
put("emailRob", new Email("Hello1", "Hello Rob"));
|
||||
put("emailJeff", new Email("Hello2", "Hello Jeff"));
|
||||
put("emailTom", new Email("Hello3", "Hello Tom"));
|
||||
put("emailBobby", new Email("Hello4", "Hello Bobby"));
|
||||
}
|
||||
};
|
||||
|
||||
public Flux<Email> fetchQuoteStream() {
|
||||
return Flux.interval(Duration.ofSeconds(1))
|
||||
.onBackpressureDrop()
|
||||
.map(this::generateEmail)
|
||||
.flatMapIterable(emails -> emails);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create an email for all tickers
|
||||
*/
|
||||
private List<Email> generateEmail(long interval) {
|
||||
Object[] emailKeys = emailMap.keySet()
|
||||
.toArray();
|
||||
List<Email> emailList = new ArrayList<>();
|
||||
|
||||
// select random email message from emailMap
|
||||
emailList.add(emailMap.get(emailKeys[new Random().nextInt(emailKeys.length)]));
|
||||
return emailList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.reactive.handler;
|
||||
|
||||
import com.baeldung.reactive.generator.EmailGenerator;
|
||||
import com.baeldung.reactive.model.Email;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
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;
|
||||
|
||||
@Component
|
||||
public class EmailHandler {
|
||||
|
||||
@Autowired EmailGenerator emailGenerator;
|
||||
|
||||
public Mono<ServerResponse> getEmails(ServerRequest serverRequest) {
|
||||
return ServerResponse.ok()
|
||||
.contentType(MediaType.APPLICATION_STREAM_JSON)
|
||||
.body(emailGenerator.fetchQuoteStream(), Email.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.reactive.model;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
|
||||
public class Email {
|
||||
@NotBlank
|
||||
private String title;
|
||||
|
||||
@NotBlank
|
||||
private String message;
|
||||
|
||||
@NotNull
|
||||
private Date createdDate = new Date();
|
||||
|
||||
public Email(@NotBlank String title, @NotBlank String message) {
|
||||
this.title = title;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Date getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
public void setCreatedDate(Date createdDate) {
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.reactive.router;
|
||||
|
||||
import com.baeldung.reactive.handler.EmailHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class EmailRouter {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> route(EmailHandler handler) {
|
||||
return RouterFunctions.route(RequestPredicates
|
||||
.GET("/emails")
|
||||
.and(RequestPredicates.accept(MediaType.APPLICATION_STREAM_JSON)),
|
||||
handler::getEmails);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user