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;
@ -27,28 +26,22 @@ import rx.Observable;
* @author baeldung * @author baeldung
*/ */
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
@ -61,7 +54,7 @@ public class ClientOrchestration {
nameService.resolveTemplate("empId", id).request() nameService.resolveTemplate("empId", id).request()
.async() .async()
.get(new InvocationCallback<String>() { .get(new InvocationCallback<String>() {
@Override @Override
public void completed(String response) { public void completed(String response) {
completionTracker.countDown(); completionTracker.countDown();
@ -70,7 +63,7 @@ public class ClientOrchestration {
public void completed(String response) { public void completed(String response) {
logger.log(Level.INFO, "[InvocationCallback] The hash output {0}", response); logger.log(Level.INFO, "[InvocationCallback] The hash output {0}", response);
} }
@Override @Override
public void failed(Throwable throwable) { public void failed(Throwable throwable) {
completionTracker.countDown(); completionTracker.countDown();
@ -79,7 +72,7 @@ public class ClientOrchestration {
} }
}); });
} }
@Override @Override
public void failed(Throwable throwable) { public void failed(Throwable throwable) {
completionTracker.countDown(); completionTracker.countDown();
@ -88,7 +81,7 @@ public class ClientOrchestration {
} }
}); });
}); });
try { try {
if (!completionTracker.await(10, TimeUnit.SECONDS)) { //wait for inner requests to complete in 10 seconds if (!completionTracker.await(10, TimeUnit.SECONDS)) { //wait for inner requests to complete in 10 seconds
logger.warning("[InvocationCallback] Some requests didn't complete within the timeout"); logger.warning("[InvocationCallback] Some requests didn't complete within the timeout");
@ -97,9 +90,9 @@ public class ClientOrchestration {
failures.add(ex); failures.add(ex);
Logger.getLogger(ClientOrchestration.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ClientOrchestration.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
@Override @Override
public void failed(Throwable throwable) { public void failed(Throwable throwable) {
failures.add(throwable); failures.add(throwable);
@ -107,7 +100,7 @@ public class ClientOrchestration {
} }
}); });
} }
public void rxOrchestrate() { public void rxOrchestrate() {
logger.info("Orchestrating with a CompletionStage"); logger.info("Orchestrating with a CompletionStage");
CompletionStage<EmployeeDTO> userIdStage = userIdService.request().accept(MediaType.APPLICATION_JSON) CompletionStage<EmployeeDTO> userIdStage = userIdService.request().accept(MediaType.APPLICATION_JSON)
@ -119,7 +112,7 @@ public class ClientOrchestration {
logger.warning("[CompletionStage] An error has occurred"); logger.warning("[CompletionStage] An error has occurred");
return null; return null;
}); });
userIdStage.thenAcceptAsync(empIdDto -> { userIdStage.thenAcceptAsync(empIdDto -> {
logger.info("[CompletionStage] Got all the IDs " + empIdDto.getEmpIds()); logger.info("[CompletionStage] Got all the IDs " + empIdDto.getEmpIds());
empIdDto.getEmpIds().stream().forEach((Long id) -> { empIdDto.getEmpIds().stream().forEach((Long id) -> {
@ -128,7 +121,7 @@ public class ClientOrchestration {
.rx() .rx()
.get(String.class) .get(String.class)
.toCompletableFuture(); .toCompletableFuture();
completable.thenAccept((String userName) -> { completable.thenAccept((String userName) -> {
hashService.resolveTemplate("comboIDandName", userName + id) hashService.resolveTemplate("comboIDandName", userName + id)
.request() .request()
@ -141,24 +134,24 @@ public class ClientOrchestration {
logger.log(Level.WARNING, "[CompletionStage] Hash computation failed for {0}", id); logger.log(Level.WARNING, "[CompletionStage] Hash computation failed for {0}", id);
return null; return null;
}); });
}); });
}); });
}); });
} }
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)
@ -184,21 +176,21 @@ public class ClientOrchestration {
}) })
.subscribe(hashValue -> logger.log(Level.INFO, "[Observable] The hash output {0}", hashValue)))); .subscribe(hashValue -> logger.log(Level.INFO, "[Observable] The hash output {0}", hashValue))));
}); });
} }
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()
@ -219,7 +211,7 @@ public class ClientOrchestration {
}) })
.subscribe(hashValue -> logger.log(Level.INFO, "[Flowable] The hash output {0}", hashValue)))); .subscribe(hashValue -> logger.log(Level.INFO, "[Flowable] The hash output {0}", hashValue))));
}); });
} }
} }

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;
/** /**
@ -17,53 +20,59 @@ import org.junit.Test;
* @author baeldung * @author baeldung
*/ */
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]}";
String[] nameList = new String[]{"n/a", "Thor", "Hulk", "BlackWidow", "BlackPanther", "TheTick", "Hawkeye"}; String[] nameList = new String[]{"n/a", "Thor", "Hulk", "BlackWidow", "BlackPanther", "TheTick", "Hawkeye"};
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])));
stubFor(get(urlEqualTo("/serviceA/2/name")).willReturn(aResponse().withBody(nameList[2]))); stubFor(get(urlEqualTo("/serviceA/2/name")).willReturn(aResponse().withBody(nameList[2])));
stubFor(get(urlEqualTo("/serviceA/3/name")).willReturn(aResponse().withBody(nameList[3]))); stubFor(get(urlEqualTo("/serviceA/3/name")).willReturn(aResponse().withBody(nameList[3])));
stubFor(get(urlEqualTo("/serviceA/4/name")).willReturn(aResponse().withBody(nameList[4]))); stubFor(get(urlEqualTo("/serviceA/4/name")).willReturn(aResponse().withBody(nameList[4])));
stubFor(get(urlEqualTo("/serviceA/5/name")).willReturn(aResponse().withBody(nameList[5]))); stubFor(get(urlEqualTo("/serviceA/5/name")).willReturn(aResponse().withBody(nameList[5])));
stubFor(get(urlEqualTo("/serviceA/6/name")).willReturn(aResponse().withBody(nameList[6]))); stubFor(get(urlEqualTo("/serviceA/6/name")).willReturn(aResponse().withBody(nameList[6])));
stubFor(get(urlEqualTo("/serviceA/Thor1/hash")).willReturn(aResponse().withBody(hashResultList[0]))); stubFor(get(urlEqualTo("/serviceA/Thor1/hash")).willReturn(aResponse().withBody(hashResultList[0])));
stubFor(get(urlEqualTo("/serviceA/Hulk2/hash")).willReturn(aResponse().withBody(hashResultList[1]))); stubFor(get(urlEqualTo("/serviceA/Hulk2/hash")).willReturn(aResponse().withBody(hashResultList[1])));
stubFor(get(urlEqualTo("/serviceA/BlackWidow3/hash")).willReturn(aResponse().withBody(hashResultList[2]))); stubFor(get(urlEqualTo("/serviceA/BlackWidow3/hash")).willReturn(aResponse().withBody(hashResultList[2])));
stubFor(get(urlEqualTo("/serviceA/BlackPanther4/hash")).willReturn(aResponse().withBody(hashResultList[3]))); stubFor(get(urlEqualTo("/serviceA/BlackPanther4/hash")).willReturn(aResponse().withBody(hashResultList[3])));
stubFor(get(urlEqualTo("/serviceA/TheTick5/hash")).willReturn(aResponse().withBody(hashResultList[4]))); stubFor(get(urlEqualTo("/serviceA/TheTick5/hash")).willReturn(aResponse().withBody(hashResultList[4])));
stubFor(get(urlEqualTo("/serviceA/Hawkeye6/hash")).willReturn(aResponse().withBody(hashResultList[5]))); stubFor(get(urlEqualTo("/serviceA/Hawkeye6/hash")).willReturn(aResponse().withBody(hashResultList[5])));
} }
@Test @Test
public void hits() { public void hits() {
orchestrator.callBackOrchestrate(); orchestrator.callBackOrchestrate();
orchestrator.rxOrchestrate(); orchestrator.rxOrchestrate();
orchestrator.observableJavaOrchestrate(); orchestrator.observableJavaOrchestrate();
orchestrator.flowableJavaOrchestrate(); orchestrator.flowableJavaOrchestrate();
Assert.assertTrue(orchestrator.failures.isEmpty()); assertTrue(orchestrator.failures.isEmpty());
} }
@After
public void tearDown() {
}
} }