Distributed Locking Example

Distributed Locking Example

Use the distributed locking service to synchronize activities on shared resources.

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 Running the Examples.

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 -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.DistributedLocking
$ java -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.DistributedLocking

Example Source Files

Program and cache configuration files:

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

Related Javadocs

  • com.gemstone.gemfire.distributed.DistributedLockService