By default, the framework tries to acquire a lock from the client lock manager for a resource before processing a request. If the request is a GET request, only a READ lock is acquired; otherwise a WRITE lock is acquired. By default, the lock key is the request URI for the root resource of the request. For example, if the request was /orders/o12345/commerceItems, the framework locks on order /orders/o12345. You can create your own lock key provider component for custom locking behavior using the @Endpoint annotation lockType attribute by setting it to READ, WRITE or NONE. By default, the lock uses the current request URI string as the lock key.

You can turn global locking on or off by using the enabled property of the RestLockService to make associated autolock properties mark all operations as requiring locks. When autolocking is enabled, those endpoints that do not have a lockType annotation are assigned a lock type based on their HTTP method. The RestLockService is enabled by default.

The @Endpoint annotation uses the lockType property to define the lock type as read or write. The RestLockService uses the autolock property that assigns endpoints with no lockType defined to a lock type based on their HTPP method.

The LockKeyProvider allows you to provide the key used in locking by implementing the getLockKey method. For example:

public interface LockKeyProvider {
/**
* Get the lock key
* @param pRestContextTypically RestContext for the current request
* @return the lock key
*/
public Serializable getLockKey(RestContext pRestContext);
}

The following is an example of implementing a lock mechanism using your own lock key. Root resources should implement an interface such as this when locking is required during read/write operations on the resource itself or any of its sub-resources. The following example shows how you determine what keys are used as locks. Note that by default the LockKeyProvider uses the current request URI string as the lock key:

@Path("/SomeResources")
public class SomeResourceRestResource implements LockKeyProvider {
...

    @Path("/{id}")
    @GET
    @Endpoint(lockType="READ")
    public RepresentationModel defaultGet(@PathParam("id") String pId){
        ...
    }

    @Override
    public Serializable getLockKey(RestContext pRestContext) {
        // This implementation will use the ID of the resource
        // present in the request path param as the Lock Key
        // Note - in this particular example all requests to
        // Endpoints that require locking must set this path param
        MultivaluedMap<String, String> info =
            pRestContext.getRequestContext().getUriInfo().getPathParameters();
        List<String> idList = info.get("id");
        String id = idList.get(0);
        return (Serializable) id;
    }
}

Copyright © 1997, 2017 Oracle and/or its affiliates. All rights reserved. Legal Notices