Add files via upload

This commit is contained in:
k0l0ssus 2018-08-10 08:24:46 -04:00 committed by GitHub
parent 2b53d29985
commit 2b229332b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 73 deletions

View File

@ -40,7 +40,7 @@
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId> <artifactId>jersey-media-json-jackson</artifactId>
<version>2.22</version> <version>2.25</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId> <groupId>com.fasterxml.jackson.jaxrs</groupId>

View File

@ -1,7 +1,6 @@
package com.baeldung.samples.jerseyrx; package com.baeldung.samples.jerseyrx;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -29,26 +28,20 @@ import rx.Observable;
public class ClientOrchestration { public class ClientOrchestration {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
WebTarget userIdService = client.target("http://localhost:8080/serviceA/id"); WebTarget userIdService = client.target("http://localhost:8080/serviceA/id");
WebTarget nameService = client.target("http://localhost:8080/serviceA/{empId}/name"); WebTarget nameService = client.target("http://localhost:8080/serviceA/{empId}/name");
WebTarget hashService = client.target("http://localhost:8080/serviceA/{comboIDandName}/hash"); WebTarget hashService = client.target("http://localhost:8080/serviceA/{comboIDandName}/hash");
LinkedList<Throwable> failures = new LinkedList<>(); LinkedList<Throwable> failures = new LinkedList<>();
Logger logger = Logger.getLogger("ClientOrchestrator"); Logger logger = Logger.getLogger("ClientOrchestrator");
public static void main(String[] args) {
ClientOrchestration orchestrator = new ClientOrchestration();
orchestrator.callBackOrchestrate();
orchestrator.rxOrchestrate();
orchestrator.observableJavaOrchestrate();
orchestrator.flowableJavaOrchestrate();
}
public void callBackOrchestrate() { public void callBackOrchestrate() {
logger.info("Orchestrating with the pyramid of doom"); logger.info("Orchestrating with the pyramid of doom");
userIdService.request().accept(MediaType.APPLICATION_JSON) userIdService.request()
.accept(MediaType.APPLICATION_JSON)
.async() .async()
.get(new InvocationCallback<EmployeeDTO>() { .get(new InvocationCallback<EmployeeDTO>() {
@Override @Override
@ -152,13 +145,13 @@ public class ClientOrchestration {
public void observableJavaOrchestrate() { public void observableJavaOrchestrate() {
logger.info("Orchestrating with Observables"); logger.info("Orchestrating with Observables");
Observable<EmployeeDTO> userIdObservable = userIdService.register(RxObservableInvokerProvider.class).request() Observable<EmployeeDTO> observableUserIdService = userIdService.register(RxObservableInvokerProvider.class).request()
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)
.rx(RxObservableInvoker.class) .rx(RxObservableInvoker.class)
.get(new GenericType<EmployeeDTO>() { .get(new GenericType<EmployeeDTO>() {
}); }).asObservable();
userIdObservable.subscribe((EmployeeDTO empIdList) -> { observableUserIdService.subscribe((EmployeeDTO empIdList) -> {
logger.info("[Observable] Got all the IDs " + empIdList.getEmpIds()); logger.info("[Observable] Got all the IDs " + empIdList.getEmpIds());
Observable.from(empIdList.getEmpIds()).subscribe(id Observable.from(empIdList.getEmpIds()).subscribe(id
-> nameService.register(RxObservableInvokerProvider.class) -> nameService.register(RxObservableInvokerProvider.class)
@ -171,8 +164,7 @@ public class ClientOrchestration {
failures.add(throwable); failures.add(throwable);
logger.log(Level.WARNING, " [Observable] An error has occurred in the username request step {0}", throwable.getMessage()); logger.log(Level.WARNING, " [Observable] An error has occurred in the username request step {0}", throwable.getMessage());
}) })
.subscribe(userName -> hashService .subscribe(userName -> hashService.register(RxObservableInvokerProvider.class)
.register(RxObservableInvokerProvider.class)
.resolveTemplate("comboIDandName", userName + id) .resolveTemplate("comboIDandName", userName + id)
.request() .request()
.rx(RxObservableInvoker.class) .rx(RxObservableInvoker.class)
@ -189,16 +181,16 @@ public class ClientOrchestration {
public void flowableJavaOrchestrate() { public void flowableJavaOrchestrate() {
logger.info("Orchestrating with Flowable"); Flowable<EmployeeDTO> userIdFlowable = userIdService.register(RxFlowableInvokerProvider.class)
Flowable<EmployeeDTO> userIdObservable = userIdService.register(RxFlowableInvokerProvider.class)
.request() .request()
.rx(RxFlowableInvoker.class) .rx(RxFlowableInvoker.class)
.get(new GenericType<EmployeeDTO>() { .get(new GenericType<EmployeeDTO>() {
}); });
Disposable subscribe = userIdObservable.subscribe((EmployeeDTO dto) -> { userIdFlowable.subscribe((EmployeeDTO dto) -> {
logger.info("Orchestrating with Flowable");
List<Long> listOfIds = dto.getEmpIds(); List<Long> listOfIds = dto.getEmpIds();
Observable.from(listOfIds).map(id Flowable.just(listOfIds).subscribe(id
-> nameService.register(RxFlowableInvokerProvider.class) -> nameService.register(RxFlowableInvokerProvider.class)
.resolveTemplate("empId", id) .resolveTemplate("empId", id)
.request() .request()

View File

@ -1,6 +1,7 @@
package com.baeldung.samples.jerseyrx; package com.baeldung.samples.jerseyrx;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* *
@ -18,4 +19,36 @@ public class EmployeeDTO {
this.empIds = empIds; this.empIds = empIds;
} }
@Override
public int hashCode() {
int hash = 5;
hash = 59 * hash + Objects.hashCode(this.empIds);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final EmployeeDTO other = (EmployeeDTO) obj;
if (!Objects.equals(this.empIds, other.empIds)) {
return false;
}
return true;
}
@Override
public String toString() {
return "EmployeeDTO{" + "empIds=" + empIds + '}';
}
} }

View File

@ -1,15 +1,18 @@
package com.baeldung.samples.jerseyrx; package com.baeldung.samples.jerseyrx;
import com.github.tomakehurst.wiremock.WireMockServer;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import junit.framework.Assert; import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.After; import java.util.LinkedList;
import java.util.logging.Logger;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import static junit.framework.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
/** /**
@ -18,6 +21,16 @@ import org.junit.Test;
*/ */
public class ClientOrchestrationTest { public class ClientOrchestrationTest {
Client client = ClientBuilder.newClient();
WebTarget userIdService = client.target("http://localhost:8080/serviceA/id");
WebTarget nameService = client.target("http://localhost:8080/serviceA/{empId}/name");
WebTarget hashService = client.target("http://localhost:8080/serviceA/{comboIDandName}/hash");
LinkedList<Throwable> failures = new LinkedList<>();
Logger logger = Logger.getLogger("ClientOrchestrator");
ClientOrchestration orchestrator = new ClientOrchestration(); ClientOrchestration orchestrator = new ClientOrchestration();
String jsonIdList = "{\"empIds\":[1,2,3,4,5,6]}"; String jsonIdList = "{\"empIds\":[1,2,3,4,5,6]}";
@ -26,12 +39,12 @@ public class ClientOrchestrationTest {
String[] hashResultList = new String[]{"roht1", "kluh2", "WodiwKcalb3", "RehtnapKclab4", "kciteht5", "eyekwah6"}; String[] hashResultList = new String[]{"roht1", "kluh2", "WodiwKcalb3", "RehtnapKclab4", "kciteht5", "eyekwah6"};
WireMockServer wireMockServer = new WireMockServer(); @Rule
public WireMockRule wireMockServer = new WireMockRule();
@Before @Before
public void setup() { public void setup() {
wireMockServer.start();
configureFor("localhost", 8080);
stubFor(get(urlEqualTo("/serviceA/id")).willReturn(aResponse().withBody(jsonIdList).withHeader("Content-Type", "application/json"))); stubFor(get(urlEqualTo("/serviceA/id")).willReturn(aResponse().withBody(jsonIdList).withHeader("Content-Type", "application/json")));
stubFor(get(urlEqualTo("/serviceA/1/name")).willReturn(aResponse().withBody(nameList[1]))); stubFor(get(urlEqualTo("/serviceA/1/name")).willReturn(aResponse().withBody(nameList[1])));
@ -58,12 +71,8 @@ public class ClientOrchestrationTest {
orchestrator.observableJavaOrchestrate(); orchestrator.observableJavaOrchestrate();
orchestrator.flowableJavaOrchestrate(); orchestrator.flowableJavaOrchestrate();
Assert.assertTrue(orchestrator.failures.isEmpty()); assertTrue(orchestrator.failures.isEmpty());
} }
@After
public void tearDown() {
}
} }