Skip to content

Commit

Permalink
Complete update
Browse files Browse the repository at this point in the history
  • Loading branch information
bygui86 committed Mar 6, 2019
1 parent f3af2a4 commit 3d70af1
Show file tree
Hide file tree
Showing 65 changed files with 2,104 additions and 492 deletions.
69 changes: 47 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

## Sub-projects

* Reactive web + security
* Reactive web socket - `ON HOLD`
* Reactive web
* Reactive security
* Reactive web client
* Reactive web socket - `[STAND BY]`
* Reactive web socket client
* Reactive MongoDB
* Reactive R2DBC - `NOT WORKING`
* Reactive functional - `IN PROGRESS`
* Reactive web client
* Reactive R2DBC - `[NOT WORKING]`
* Reactive functional

---

Expand All @@ -31,10 +32,11 @@ mvnw clean package spring-boot:run

## TODOs

* Let R2DBC work
* Add filter handler
* Let R2DBC work - `[IN PROGRESS]`
* Add validation
* Add error handling
* Add OAuth2 security
* Test Debug
* Test Fongo

---
Expand All @@ -45,16 +47,20 @@ mvnw clean package spring-boot:run

* https://spring.io/blog/2017/03/15/spring-tips-the-spring-web-flux-reactive-client
* https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html `[IN PROGRESS]`
* https://medium.com/@cheron.antoine/tuto-building-a-reactive-restful-api-with-spring-webflux-java-258fd4dbae41
* https://medium.com/@cheron.antoine/tuto-building-a-reactive-restful-api-with-spring-webflux-java-258fd4dbae41 `[TODO]`

### Reactive Web

* https://www.baeldung.com/spring-webflux
* https://www.baeldung.com/spring-5-webclient
* https://www.baeldung.com/spring-reactive-sequence-logging
* https://www.baeldung.com/spring-debugging-reactive-streams
* https://medium.com/@fede.lopez/take-reactive-programming-with-spring-to-the-infinity-and-beyond-965a4c15b26 `[TODO]`

### Reactive Web Client

* https://www.baeldung.com/spring-5-webclient
* https://spring.io/blog/2017/03/15/spring-tips-the-spring-web-flux-reactive-client

### Reactive Security

* https://www.baeldung.com/spring-security-5-reactive
Expand All @@ -67,37 +73,56 @@ mvnw clean package spring-boot:run

* https://www.baeldung.com/spring-data-mongodb-reactive

#### Fongo `[TODO]`

* https://github.com/fakemongo/fongo
* https://devops.datenkollektiv.de/testing-the-mongodb-slice-with-spring-and-fongo.html
* https://github.com/JohnathanMarkSmith/spring-fongo-demo

### Reactive R2DBC

* https://spring.io/projects/spring-data-r2dbc
* https://github.com/spring-projects/spring-data-r2dbc
* https://github.com/r2dbc/r2dbc-h2
* https://github.com/r2dbc/r2dbc-postgresql
* https://github.com/r2dbc/r2dbc-proxy - `[TODO]`
* https://github.com/r2dbc/r2dbc-client - `[TODO]`
* https://github.com/spring-projects/spring-data-examples/tree/master/r2dbc/example
* https://docs.spring.io/spring-data/r2dbc/docs/1.0.0.M1/reference/html/
* https://spring.io/blog/2018/12/12/spring-data-r2dbc-1-0-m1-released - `[IN PROGRESS]`

### Reactive Functional

* https://www.baeldung.com/spring-5-functional-web
* https://spring.io/blog/2016/09/22/new-in-spring-5-functional-web-framework
* http://sinhamohit.com/writing/spring-boot-reactive-tutorial
* https://ordina-jworks.github.io/spring/2018/09/28/SpringOne-fun-with-the-functional-web-framework.html `[IN-PROGRESS: missing handler filter function]`
* https://medium.com/@ankesh.kapil/real-time-event-streaming-using-spring-webflux-745e8885c8bd `[TODO]`
* https://medium.com/@ankesh.kapil/real-time-event-streaming-using-spring-webflux-745e8885c8bd

### Reactive Web Client
### Testing

* https://www.baeldung.com/spring-5-webclient
* https://spring.io/blog/2017/03/15/spring-tips-the-spring-web-flux-reactive-client
* https://docs.spring.io/spring-security/site/docs/current/reference/html5/#test-webflux
* https://www.baeldung.com/spring-tests

### Addons

#### Fongo `[TODO]`

* https://github.com/fakemongo/fongo
* https://devops.datenkollektiv.de/testing-the-mongodb-slice-with-spring-and-fongo.html
* https://github.com/JohnathanMarkSmith/spring-fongo-demo

### Validation `[TODO]`
#### Filter

* https://ordina-jworks.github.io/spring/2018/09/28/SpringOne-fun-with-the-functional-web-framework.html
* https://stackoverflow.com/questions/52870517/spring-reactive-get-body-jsonobject-using-serverrequest

#### Validation `[TODO]`

* https://www.baeldung.com/spring-functional-endpoints-validation

### Error handling `[TODO]`
#### Error handling `[TODO]`

* https://objectpartners.com/2017/11/16/spring-webflux-functional-endpoints/

#### Debug `[TODO]`

* https://www.baeldung.com/spring-debugging-reactive-streams

#### OAuth2 `[TODO]`

* https://www.baeldung.com/spring-oauth-login-webflux
* https://docs.spring.io/spring-security/site/docs/current/reference/html/webclient.html
4 changes: 4 additions & 0 deletions spring-reactive-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.nio.charset.Charset;
import java.util.Random;

import static java.nio.charset.StandardCharsets.UTF_8;


/**
* @author Matteo Baiguini
Expand All @@ -22,26 +30,13 @@
@AllArgsConstructor
@Getter(AccessLevel.PROTECTED)
@Component
@Profile("web-func")
public class EmployeeWebClient {

final String URI_ROOT = "/employees";

WebClient webClient;

@Scheduled(initialDelay = 5000, fixedRate = 1000)
public void update() {

String id = "1";

log.info("update employee id {}", id);

getWebClient()
.put()
.uri(URI_ROOT)
.body(BodyInserters.fromObject(Employee.builder().id(id).name("Matteo Baiguini").build()))
;
}

@Scheduled(initialDelay = 1000, fixedRate = 3000)
public void getById() {

Expand Down Expand Up @@ -70,6 +65,25 @@ public void getAll() {
;
}

@Scheduled(initialDelay = 5000, fixedRate = 10000)
public void update() {

final Employee employee = Employee.builder().id("1").name("Matteo Baiguini").build();
log.info("scheduled update employee {}...", employee);

getWebClient()
.put()
.uri(URI_ROOT)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("Authorization", "Basic " + Base64Utils
.encodeToString(("admin:secret").getBytes(UTF_8)))
.body(Mono.just(employee), Employee.class)
.retrieve()
.bodyToMono(Employee.class)
.subscribe(this::logInfoEmployee)
;
}

private String generateRandomId() {

return Integer.toString(new Random().nextInt(10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand All @@ -25,6 +26,7 @@
@AllArgsConstructor
@Getter(AccessLevel.PROTECTED)
@Component
@Profile("web-func")
public class EventWebClient {

final String URI_ROOT = "/events";
Expand Down Expand Up @@ -59,7 +61,9 @@ public void subscribeFlux() {
getWebClient()
.get()
.uri(URI_ROOT)
.accept(MediaType.TEXT_EVENT_STREAM)
// WARNING: adding "accept" header is not working, investigate better about the reason!
// .accept(MediaType.TEXT_EVENT_STREAM)
// .accept(MediaType.APPLICATION_STREAM_JSON)
.retrieve()
.bodyToFlux(Event.class)
.subscribe(this::logInfoEvent)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.rabbit.samples.springreactiveclient.clients;

import com.rabbit.samples.springreactiveclient.domain.Employee;
import com.rabbit.samples.springreactiveclient.domain.Event;
import com.rabbit.samples.springreactiveclient.domain.R2dbcEmployee;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.Random;

import static java.nio.charset.StandardCharsets.UTF_8;


/**
* @author Matteo Baiguini
* [email protected]
* 05 Mar 2019
*/
@Slf4j
@AllArgsConstructor
@Getter(AccessLevel.PROTECTED)
@Component
@Profile("r2dbc")
public class R2dbcWebClient {

final String URI_ROOT = "/employees";

WebClient webClient;

@PostConstruct
public void postConstruct() {

insert(1, "Matteo Baiguini");
// insert(2, "John Doe");
// insert(3, "Jane Doe");
//
// getById(1);
//
// getAll();
}

public void insert(final Integer id, final String name) {

final R2dbcEmployee employee = R2dbcEmployee.builder().id(id).name(name).build();
log.info("scheduled insert employee {}", employee);

getWebClient()
.post()
.uri(URI_ROOT)
.body(Mono.just(employee), R2dbcEmployee.class)
.retrieve()
.bodyToMono(R2dbcEmployee.class)
.subscribe(this::logInfoEmployee)
;
}

public void getById(final Integer id) {

log.info("scheduled get by id...");

getWebClient()
.get()
.uri(URI_ROOT + "/{id}", id)
.retrieve()
.bodyToMono(R2dbcEmployee.class)
.subscribe(this::logInfoEmployee)
;
}

public void getAll() {

log.info("scheduled get all...");

getWebClient()
.get()
.uri(URI_ROOT)
.retrieve()
.bodyToFlux(R2dbcEmployee.class)
.subscribe(this::logInfoEmployee)
;
}

private void logInfoEmployee(final R2dbcEmployee employee) {

log.info(employee.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.FieldDefaults;


Expand All @@ -13,27 +15,17 @@
* [email protected]
* 19 Feb 2019
*/
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@FieldDefaults(level = AccessLevel.PRIVATE)
@EqualsAndHashCode
@ToString
public class Employee {

String id;

String name;

@Override
public String toString() {

return new StringBuilder()
.append("{\"id\":")
.append(getId())
.append(",\"name\":")
.append(getName())
.append("}")
.toString();
}

}
Loading

0 comments on commit 3d70af1

Please sign in to comment.