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.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today