简体   繁体   中英

How to design the class & it's method

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 eg 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 eg 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 eg `context/common_directory/... to context/directory_1, context/directory_2'

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

Two advices:

  1. Do not name the class same to already existing class in JDK.

  2. Do not chain method calls this way, create one-purpose methods and then put them together in one method clearly showing your intension.

    class FileProcessor /*FileUtil whatever, but not FileWriter */ {

    public synchronized writeFile(String contextPath) { // create a directory & new file under context

    copyFiles(contextPath); compressDir(contextPath); // to compress the file
    }

    // copies files to /contextPath/directory/... from existingDirectory;
    private void copyFiles(String path_to_directory){ }

    // compress the newly created directory private void compressDir(String Directory_path) { }

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 (ie you are not maintaining its state that too in a thread safe fashion) then you have performance/accuracy issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM