简体   繁体   中英

Servlet Long Data Caching Process

I am looking for design best practice to implement the following application. Platform: RedHat Linux, Tomcat 8, Java 8. Eventually this will be moved to a JBOSS app server.

My servlet upon starting will read from 10 large files and build a data structure that will be used to answer user queries. It takes approx. 2 min to load each file for a total of 20 mins before the data structure is ready.

Currently I have this code in a ServletContextListener and the data structure is placed using setAttribute() method to make it available to the Servlet.

Problem: When I upload my war file, the servlet is not available until the set of files is loaded - 20 mins. I don't mind this but I am wondering if there is a better way to implement this.

I agree that reading time is not your problem: reading 10*30M=300M should be possible in <1 minute. Your problem is the time spent building your data-structure.

I recommend that you...

  1. Simplify your data structure in memory
  2. Optimise your file format for fast loading
  3. Avoid garbage-collections while reading files
  4. Load your files in parallel threads

In (1), make your data structure hold fewer larger objects. For example, instead of 1000 objects with (int) members, use byte[4000]. Then, for each client request you can pick the data out of your byte[4000] array. This will save lots of time, because allocating lots of small long-lifetime objects is slow. I expect that this will be your major win.

In (2), make your files store the data in blocks corresponding to the byte[4000] array etc in (1). Then you can load the data block with a single read(arByte) call. (If you want even more speed you could memory map your file, but I do not recommend this because of the extra complication in your code.)

In (3), avoid making lots of temporary Objects while reading the files. Each temporary object must be garbage collected. Garbage Collections can take a long time with high-memory-use programs like your server. Profile your server to see if GC-s are slowing it down. If they are then (for example) reuse a StringBuilder object to process each record instead of allocating a new String object for each record.

I strongly recommend (1). Your server will load faster, and paradoxically it will probably run faster too, as it will save so much memory.

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