开发者

How can I make a java servlet outputting a lot of data more efficient?

I have a java servlet that pulls information out of a JSON object. The JSON object consists of search results from indexed data (elastic search). This works well if there are few results, but when there are many it significantly slows down. If the number of results is over 50,000 this will sometime crash the browser. What could I do to make going through a large number of results more efficient so that it doesn't crash the browser.

Here is my code:

//Result Count
int i = 1;

for (SearchHit sh : response.getHits().hits()) {
    out.println("Result " + i + " <br>");

    out.println("&nbsp;&nbsp;&nbsp;&nbsp;" + sh.getSource().get("@message").toString() + " <br>");

    HashMap hm = (HashMap) sh.getSource().get("@fields");

    Iterator it = hm.entrySet().iterator();

    while (it.hasNext()) {

        Map.Entry pairs = (Map.Entry)it.next();
        out.println("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + pairs.getKey() + " = " + pairs.getValue().toString().replace("[", "").replace("]", "") + " <br>");
    }
    i++;
 开发者_JAVA技巧   out.println(" <br>");
}


It is not clear from your question where the real bottleneck is:

  • It could be the rate at which your JSP is able to pull data from the JSON object.

  • It could be the rate at which the JSP can format the data and write it.

  • It could be the network transfer rate.

  • It could be the rate at which the browser is able to read and render the response.

The solution will depend on where the bottleneck actually is, so you need to figure this out by profiling your server-side and monitoring what is happening in the browser. The solution could involve changing the HTML you are generating so that it is smaller or it renders faster, or changing the way you are generating it; e.g. simplifying the JSON. (In your case, the latter may not be an option since you are getting the JSON from an external service.)

The other way to address this is to restructure the UI so that you don't try to send 50,000 results in one page. Either do some server-side work to reduce / distil the results, use some kind of result set paging, or do some clever (and efficient) stuff in the user's browser to present the results dynamically. (The last option would involve sending the results to the browser as JSON, and would entail a lot of browser-side coding to render the data, brower independence issues, etc!)

A web UI that expects the user to wade through a page containing 50,000 results is (IMO) pretty much unusable.


If you place a watch on your browser, either through HTTP watch or a browser plugin (such as chrome's), you will notice that the browser may be locked in some activities while processing data.

For example, when using HTML tables to present data, the browser waits for the entire group of data required to render the table to first arrive before starting to render the whole table. This is one of the arguments HTML TABLE vs HTML DIV. Therefore, you may want to look at whether you have been using TABLE for rendering such data and consider the switch to DIV.

Here is an example and guide of using DIV to render grid like structures.

There may be also other factors. Therefore, place a watch on your browser activity and see what the bottleneck is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜