DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

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.

Ujjawal Kant user avatar by
Ujjawal Kant
·
Sep. 12, 16 · Tutorial
Like (5)
Save
Tweet
Share
7.13K Views

Join the DZone community and get the full member experience.

Join For Free

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! 

Lock (computer science) Flow (web browser)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Chaos Engineering Tutorial: Comprehensive Guide With Best Practices
  • A Gentle Introduction to Kubernetes
  • 11 Observability Tools You Should Know
  • How To Choose the Right Streaming Database

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: