Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7694645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:21:58+00:00 2026-05-31T21:21:58+00:00

I have a Java servlet which calls another software (say S) over a TCP

  • 0

I have a Java servlet which calls another software (say S) over a TCP connection. This software S uses a network resource, and the output has to be retrived from a hyperlink(using wget).

Since it’s the same hyperlink I need to download my result from (irrespective of the request), it results into incorrect results few requests. I basically need to lock the use of this network resource across different processes (I believe each call from the servlet is going to create a new process).

I tried to use ReentrantLock (but I guess it only works with threads and not accross processes).

Please let me know how can this be achieved.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:21:59+00:00Added an answer on May 31, 2026 at 9:21 pm

    Here is how to do cross-process locking in Java. Adjust to your needs and add error/exception checking/handling as necessary.

    // Tester 
    try { 
      if (crossProcessLockAcquire(SomeClassInYourApp.class, 3000)) { 
        // Success - This process now has the lock. (Don't keep it too long.) 
      } 
      else { 
        // Fail (Timeout) - Another process still had the lock after 3 seconds. 
      } 
    } finally { 
      crossProcessLockRelease(); // try/finally is very important. 
    } 
    
    // Acquire - Returns success ( true/false ) 
    private static boolean crossProcessLockAcquire(final Class<?> c, final long waitMS) { 
    if (fileLock == null && c != null && waitMS > 0) { 
        try { 
            long dropDeadTime = System.currentTimeMillis() + waitMS; 
            File file = new File(lockTempDir, c.getName() + ".lock"); 
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); 
            FileChannel fileChannel = randomAccessFile.getChannel(); 
            while (System.currentTimeMillis() < dropDeadTime) { 
                fileLock = fileChannel.tryLock(); 
                if (fileLock != null) { 
                    break; 
                } 
                Thread.sleep(250); // 4 attempts/sec 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    return fileLock == null ? false : true; 
    } 
    
    // Release 
    private static void crossProcessLockRelease() { 
    if (fileLock != null) { 
        try { 
            fileLock.release(); 
            fileLock = null; 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
    } 
    
    // Some class vars and a failsafe lock release. 
    private static File lockTempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "locks"); 
    private static FileLock fileLock = null; 
    static { 
    Runtime.getRuntime().addShutdownHook(new Thread() { 
        public void run(){ 
            crossProcessLockRelease(); 
        } 
    }); 
    }     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this servlet which needs to send mail using Java Mail API, however
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
I have a Java servlet from which I need to send few notifications to
I have this java servlet that grabs information from a form, I need to
I have a Java class which uses Apache POI to generate reports in Excel.
I have the following problem. I programmed a java servlet, which responses to ajax
I have this managed bean which takes two values from database's table import java.io.Serializable;
Unix gurus! I have a Java program which passes some parameters to a Servlet
I have a servlet code which calls a ejb stateful session bean code as
I have a legacy Java servlet that is currently running in a Tomcat container.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.