Java Reference
In-Depth Information
rent ETag value for it from the hash code of the object (this isn't the best way to create the
EntityTag , but for simplicity's sake, let's keep it that way). We then call
Request.evaluatePreconditions() , passing in the up-to-date tag. If the tags match, we
reset the client's cache expiration by sending a new Cache-Control header and return. If the
tags don't match, we build a Response with the new, current version of the ETag and Cus-
tomer .
Concurrency
Now that we have a good idea of how to boost the performance of our JAX-RS services us-
ing HTTP caching, we need to look at how to scale applications that update resources on our
server. The way RESTful updates work is that the client fetches a representation of a re-
source through a GET request. It then modifies the representation locally and PUTs or
POSTs the modified representation back to the server. This is all fine and dandy if there is
only one client at a time modifying the resource, but what if the resource is being modified
concurrently? Because the client is working with a snapshot, this data could become stale if
another client modifies the resource while the snapshot is being processed.
The HTTP specification has a solution to this problem through the use of conditional PUTs
or POSTs. This technique is very similar to how cache revalidation and conditional GETs
work. The client first starts out by fetching the resource. For example, let's say our client
wants to update a customer in a RESTful customer directory. It would first start off by sub-
mitting GET /customers/123 to pull down the current representation of the specific custom-
er it wants to update. The response might look something like this:
HTTP
HTTP / 1.1 200 OOK
Content-Type : application/xml
Cache-Control : max-age=1000
ETag: "3141271342554322343200"
Last-Modified: Tue, 15 May 2013 09:56 EST
<customer id="123">...</customer>
In order to do a conditional update, we need either an ETag or Last-Modified header. This
information tells the server which snapshot version we have modified when we perform our
update. It is sent along within the If-Match or If-Unmodified-Since header when we do
our PUT or POST request. The If-Match header is initialized with the ETag value of the
snapshot. The If-Unmodified-Since header is initialized with the value of Last-Modified
header. So, our update request might look like this:
Search WWH ::




Custom Search