Skip to content

Commit

Permalink
RESTEasy client example for chapter 4
Browse files Browse the repository at this point in the history
  • Loading branch information
kenfinnigan committed Sep 6, 2016
1 parent 82c880d commit b2d8067
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
6 changes: 6 additions & 0 deletions chapter4/resteasy-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.14.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ejm.chapter4.resteasyclient;

import javax.enterprise.concurrent.ManagedExecutorService;
import javax.naming.InitialContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

/**
* @author Ken Finnigan
*/
@Path("/")
public class MessageResource {

private String timeUrl = "http://localhost:8081/";

@GET
@Path("/sync2sync")
public String getMessageSync2Sync() throws Exception {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(this.timeUrl);

TimeService timeService = target.proxy(TimeService.class);
String time = timeService.getTime();

if (time != null) {
return message(time);
} else {
return "Time service unavailable at " + this.timeUrl;
}
}

@GET
@Path("/async2sync")
public void getMessageAsync2Sync(@Suspended final AsyncResponse asyncResponse) throws Exception {
executorService().execute(() -> {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(this.timeUrl);

TimeService timeService = target.proxy(TimeService.class);
String time = timeService.getTime();

if (time != null) {
asyncResponse.resume(message(time));
} else {
asyncResponse.resume("Time service unavailable at " + this.timeUrl);
}
});
}

@GET
@Path("/async2async")
public void getMessageAsync2Async(@Suspended final AsyncResponse asyncResponse) throws Exception {
executorService().execute(() -> {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(this.timeUrl);
target.request(MediaType.TEXT_PLAIN)
.async()
.get(new InvocationCallback<String>() {
@Override
public void completed(String result) {
asyncResponse.resume(message(result));
}

@Override
public void failed(Throwable throwable) {
asyncResponse.resume(throwable);
}
});
});
}

private String message(String time) {
return "The date and time at " + this.timeUrl + " is " + time;
}

private ManagedExecutorService executorService() throws Exception {
InitialContext ctx = new InitialContext();
return (ManagedExecutorService) ctx.lookup("java:jboss/ee/concurrency/executor/default");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ejm.chapter4.resteasyclient;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
* @author Ken Finnigan
*/
@Path("/")
public interface TimeService {

@GET
@Produces(MediaType.TEXT_PLAIN)
String getTime();
}

0 comments on commit b2d8067

Please sign in to comment.