Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Acquiring Locks in Mule Flows to avoid Racing Condition

DZone's Guide to

Acquiring Locks in Mule Flows to avoid Racing Condition

This quick tutorial will introduce you to locks in Mule. See how you can create, acquire, remove, and get the status of locks in your flows.

· Integration Zone
Free Resource

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

Today, we're going to dive into some Java, using some code to acquire locks in your flow.

This can be useful when multiple threads of a single mule flow is trying to access a common shared resource (Lets say write to a file). We can aquire lock before accessing the shared resource and release the lock once the operation is done.

  1. Create a LockSynchronizer.java file under src/main/java. This will initialize a semaphore    and its three functions lock() , unlock(), and getLock().

  2. Below is the .java file under org.mule.util.locksynchronizer package.

  3. package org.mule.util.locksynchronizer;
    
    import java.util.concurrent.Semaphore;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class LockSynchronizer {
          private static final int MAX_PERMITS_NUMBER = 1;
          private static final Logger log = LogManager.getLogger(LockSynchronizer.class);
    
          private Semaphore semaphore;
    
          public void lock() {
                try {
                      getLock().acquire();
                } catch (InterruptedException e) {
                      log.error(e.getCause(), e);
                }
          }
    
          public void unlock() {
                getLock().release();
          }
    
          public Semaphore getLock() {
                if (semaphore == null) {
                      this.semaphore = new Semaphore(MAX_PERMITS_NUMBER);
                }
                return this.semaphore;
          }
    
    }

  4. Create a Spring Bean in the Mule XML file:

  5. <spring:beans>
        <spring:bean id="LockSynchronizer" name="LockSynchronizer" class="org.mule.util.locksynchronizer" scope="singleton" />
    </spring:beans>


  6. Use the below expressions to acquire and release lock respectively:

  • Acquire Lock:

  • <expression-component doc:name="Acquire lock"><![CDATA[app.registry['LockSynchronizer'].lock();]]>      </expression-component>


  • Remove Lock:

  • <expression-component doc:name="Release lock"><![CDATA[app.registry['LockSynchronizer'].unlock();]]></expression-component>


  • Get Lock Status:

<expression-component doc:name="Get lock status"><![CDATA[app.registry['LockSynchronizer'].getLock();]]></expression-component>


Thanks for reading! 

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:
mule esb ,mulesoft ,mule

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}