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.
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:
SESSION1 $ java -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.DistributedLocking
SESSION2 $ java -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.DistributedLocking
Example Source Files
Program and cache configuration files:
DistributedLocking.xml | Configures a cache with a loader and listener. |
DistributedLocking.java | Demonstrates the use of distributed locking to manage concurrent access to resources. |
Related Javadocs
- com.gemstone.gemfire.distributed.DistributedLockService