-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RESTEasy client example for chapter 4
- Loading branch information
1 parent
82c880d
commit b2d8067
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
chapter4/resteasy-client/src/main/java/ejm/chapter4/resteasyclient/MessageResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
chapter4/resteasy-client/src/main/java/ejm/chapter4/resteasyclient/TimeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |