Contact Us 1-800-596-4880

Distributed Locking

Mule runtime engine version 3.8 reached its End of Life on November 16, 2021. For more information, contact your Customer Success Manager to determine how to migrate to the latest Mule version.

Mule provides the ability to create locks for synchronizing access to resources within Mule components. To manage concurrent access to resources within a server cluster, Mule provides a lock factory which custom components – such as custom transformers and DevKit-built components – can access programmatically.

Synchronizing your code with locks provided by Mule enables you to synchronize access to shared resources in a clustered environment. Thus, any locks you create with Mule’s lock factory work seamlessly on deployment models which use a single server or a cluster of servers. With the locks in place, you can deploy code in a single mule server or a Mule cluster without causing any issues with resource synchronization. Further, Mule’s locking system offers a simple API to access shared locks.

Creating a Lock

  1. To create a lock, you first need to gain access to a LockFactory. To access the LockFactory provided by Mule, use a reference to the MuleContext `of your application. Mule can inject a `MuleContext instance on every custom component that you use by making the components implement MuleContextAware. In the example code below, Mule automatically injects MuleContext into MyCustomComponent by calling the setMuleContext method. You can store the context in a variable for later use.

    public class MyCustomComponent implements MuleContextAware
    {
        private MuleContext muleContext;
        public void setMuleContext(MuleContext context)
        {
            this.muleContext = context;
        }
    }
  2. With access to MuleContext, you can access the LockFactory to start creating locks. Refer to code sample below. Use an identifier for each lock you create. This identifier allows you to access the same lock from different components without having to explicitly share the same lock instance between components.

    public class MyCustomComponent implements MessageProcessor
    {
        public MuleEvent process(MuleEvent event)
        {
            Lock lock = muleContext.getLockFactory().createLock("lockId");
            lock.lock();
            try
            {
               //critical section
            }
            finally
            {
               lock.unlock();
            }
        }
    }

Sharing a Lock Between Components

When creating locks for components, use the same lock ID for each component. Because both components create the lock using the same ID, they obtain access to the same lock. The code sample below describes two custom components which create locks using the same lock ID, sharedResourceId.

public class MyCustomComponentA implements MessageProcessor
{
    public MuleEvent process(MuleEvent event)
    {
        Lock lock = muleContext.getLockFactory().createLock("sharedResourceId");
        ...
    }
}
public class MyCustomComponentB implements MessageProcessor
{
    public MuleEvent process(MuleEvent event)
    {
        Lock lock = muleContext.getLockFactory().createLock("sharedResourceId");
        ...
    }
}

LockFactory creates java.util.concurrent.Lock but does not support the method newCondition(). Using newCondition() causes Mule to throw an UnsupportedOperationException. Mule does support other methods of interfacing with a lock, such as lockInterruptibly() and tryLock().

See Also