Distributed Locking Example

To synchronize activities on shared resources, you can use the GemFire distributed locking service. With distributed locking, you program your applications to use an arbitrary named lock that only one application can own at a time. Use this to coordinate access to resources like shared files and non-locking data regions (those that do not have global scope).

Running the Example

This example uses two instances of the same application, run concurrently, to show how distributed locking can be used to obtain exclusive rights over a resource.

Note: To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up Your Environment.

For this example, the program runs through several iterations, trying to acquire a lock on a named lock service and updating a cached data entry when it gets the lock. All activities are reported to standard out.

Start the example at approximately the same time in your two terminal sessions, to create contention for the lock:

$ java quickstart.DistributedLocking
$ java quickstart.DistributedLocking

Example Source Files

Program and cache configuration files:

Table 1. Cache configuration files, located in $SamplesDirectory/quickstart/xml
DistributedLocking.xml Configures a cache with a loader and listener.
Table 2. Java program files, located in $SamplesDirectory/quickstart/quickstart
DistributedLocking.java Demonstrates the use of distributed locking to manage concurrent access to resources.

Related Javadocs