Earthly Powers
- All
- Fast Infoset
- General
- Java
- REST
EHCache using Jersey
Ehcache is a general purpose distributed caching framework and server written in Java. Greg Luck has reported in a blog entry that the newly released Ehcache 0.3 now supports a RESTful interface using Jersey. Very nice! Greg provides more details on the interface in the blog entry.
Greg also goes into details on how to use this from the client-side. Using the Jersey client API makes it very easy to use the service, for example creating or updating a new cache entry:
String xmlDocument = "...";Client c = Client.create(); WebResource r = c.resource("http://localhost:8080/ehcache/rest/sampleCache2/2"); r.type("application/xml").put(xmlDocument);
Anything other than a 2xx response will result in an UniformInterfaceException being thrown. This makes it easier to manage errors returned from services.
Retrieving the cached entry as a DOM object can be performed as follows:
Node n = r.accept("application/xml").get(DOMSource.class).getNode();
Again, anything other than a 2xx response will result in an exception being thrown. Alternatively it is possible to check more details of the response:
ClientRespone cr = r.get(ClientResponse.class);
if (cr.getStatus() < 300 &&
cr.getType().isCompatibleWith(MediaType.APPLICATION_XML_TYPE)) {
Node n = cr.getEntity(DOMSource.class).getNode();
}
Notice that in the above two cases the WebResource instance r is reused.
Caching can be performed on the client side too, so i am wondering if the Jersey client API could reuse Ehcache for a caching solution, embedded (file or in-memory) or networked. Jersey client filters could written and installed to check the cache first.
Posted at 11:32AM Aug 06, 2008 by Paul Sandoz in Fast Infoset | Comments[0]
