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 6896517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:03:13+00:00 2026-05-27T07:03:13+00:00

Multiple clients send request to write a file and expecting a response either success

  • 0

Multiple clients send request to write a file and expecting a response either success or fail. I would like to describe concisly the work done at server side.

  1. handle the request by servlet class and invoke another class to proceed the further.
  2. FileWriter class is invoked and this class performs follwing the file writing process.
    a). create directory under context and write a *.txt file inside directory
    b). copy some files from context’s existing directory to newly created directory.
    c). compress (*.zip) this directory

    class FileWriter {

    public synchronized writeFile(String contextPath) {
          creates a directory & new file under context
    
          copyFiles(path_to_directory);
    }
    
    private void copyFiles(String path_to_directory){
       copies files to /contextPath/directory/... from existingDirectory;  
    
     compressDir( Directory_path ); // to compress the file
     }
    
    private void compressDir(String Directory_path) {
           compress the newly created directory
     }
    

    }

As you can see above in the class that there is one method is synchronized and two methods are private. only synchrnized method is invoking from servlet class others method are invoking inside from the method.
so is this a good / standard way to handling the multiple client request ?

or should i invoke each method directly from servlet class. please correct me and suggest a better way to implement the class.

@Edit : req1 comes and create directory & file e.g.
context/directory_1/file_1.txt

in the mean time req2 comes and checks that directory_1 is existing already so it creates directory_2 e.g. context/directory_2/file_1.txt.

now the second step is to copy the file from context to newly created directory. Let me tell you directory_1 has nothing to do with directory_2
all the newly created directory copies the file from a common_directory e.g. `context/common_directory/… to context/directory_1, context/directory_2′

and third step is to compress the directory : e.g. directory_1.zip, directory_2.zip

  • 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-27T07:03:13+00:00Added an answer on May 27, 2026 at 7:03 am

    Looking at the above code, if you calling writeFile from the servlet, your servlet ends up as a single threaded application.

    If two are working on two separate directories and separate files and you guaranty that there is no overlap, you should call both methods directly and ditch synchronized. Looks like this is what your situation is. So you can use below approach:

    Servlet Code
    
    {
      ....
      String uniqDir = createUniqDir();
      copyFiles(uniqDir);
      compressDir(uniqDir);
    }
    

    Now the whole idea is to create uniq dir name. Now there are many approaches to create uniq dir name. I ll use one which is based on time-stamp.

    String createUniqDir() {
       // Use SimpleDateFormat or just millis from Date 
       // We just trying to be as uniq as possible.
      String timeStampStr;
      Date now = new Date();
      timeStampStr = "" + now.getTime(); // If using EPOC
      // This soln if you wana use SimpleDateFormat
      // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
      // timeStampStr = sdf.format(dt);
      int counter = 1;
    
      String dirToCreateStr = "some_prefix-" + timeStampStr;
      File dirToCreate = new File(dirToCreate);
    
      while(!dirToCreate.mkdir()) {
        dirToCreateStr = "some_prefix-" + timeStampStr + "-" + counter;
        file = new File(dirToCreate);
        counter++;
      }
      return dirToCreateStr;
    }
    

    Since we are using mkdir and it is atomic and only return true if it is able to create a uniq dir. This soln is optimized as requesting colliding during a millisecond are way less and we dont need any synchronization overhead.

    You can use some counter too for creating uniq name. But if your counter always starts from the beginning (i.e. you are not maintaining its state that too in a thread safe fashion) then you have performance/accuracy issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the spec: Multiple clients using a WPF winforms application on their local
I am working on an application with multiple clients and a server running various
We have a WCF service with multiple clients to schedule operations amongst clients. It
I have a server that connects to multiple clients using TCP/IP connections, using C
In a database-centric application that is designed for multiple clients, I've always thought it
I am working with an application that is used by multiple clients. Each client
I have a WCF service that I am calling from multiple clients. I need
I am writing a message transfer program between multiple clients and server. I want
If I have multiple Oracle clients installed on a given workstation (i.e. Oracle 9
http://www.programmersheaven.com/2/FAQ-ADONET Disconnected architecture is not well suited for desktop clients with multiple updates but

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.