0

Currently I'm working on performance tuneup for the client/server application. It has been currently deployed Jetty 9. Here, the client is connecting to the HttpServlet in the server application using HttpURLConnection.

And client sends a java-serializable object as the request and receives a JSON String from the HttpServlet. After receives the JSON string, we are parsing it and do what we want to do. This response JSON is 4MB-5MB weight and request java-serializable object is relatively light weight.

Main problem is it take much time (15-20s) to download the JSON String over http. I made the JSON parser streaming with Jackson, so that it can work on the downloaded part without waiting for the complete JSON to be downloaded. But this doesn't give good results when it take much time to download.

Options that I looked for are. - Increase the bandwidth - Reduce the JSON size as mush as possible (I'm working on this too)

Additionally I heard that Netty (with NIO) will be a good solution for this. Honestly, I'm new to Netty. I have heard it is a NIO client server framework and not a HttpServlet container like Jetty. And it has boosted up major applications like Twitter with flying colours (ref : Twitter Search is Now 3x Faster). And I heard Netty will help you to handle huge number of concurrent connections (eg : 16000 threads et). So, basically, I have doubt how much will it be suitable for my situation.

Can anybody have an idea on this or any other implementation to boost it up?

Here's the code:

Client.java

URL                                         url                         = new URL(http://myhost.com/Connectors/url2Service);
HttpURLConnection                           servletConnection           = (HttpURLConnection) url.openConnection();

servletConnection.setDoInput(true);  
servletConnection.setDoOutput(true);  
servletConnection.setUseCaches (false);  
servletConnection.setDefaultUseCaches (false);  
servletConnection.setRequestMethod("POST");
servletConnection.setRequestProperty("content-type","application/x-java-serialized-object");
servletConnection.setRequestProperty("Accept","application/json");
servletConnection.setRequestProperty("Accept-Encoding","gzip,deflate");
servletConnection.setRequestProperty("user-agent","Mozilla(MSIE)");
servletConnection.setConnectTimeout(30000);
servletConnection.setReadTimeout(60000);
servletConnection.connect();


String                                      encodingHeader              = servletConnection.getHeaderField("Content-Encoding");

String                                      contentType                 = servletConnection.getHeaderField("content-type");


if(contentType.equals("application/json")){

InputStream inputStream = servletConnection.getInputStream();


if(encodingHeader != null && encodingHeader.toLowerCase().indexOf("gzip") != -1){

        gzipis          = new GZIPInputStream(inputStream);

}else{

        iSR             = new InputStreamReader(inputStream);
        br              = new BufferedReader(iSR);

}


ResposeObject   resposeObject                   = new JsonString2ResposeObject().parse(gzipis); // previously we passed complete String here


}

Server - url2Service Servlet

public class url2Service extends HttpServlet{

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            ObjectInputStream       ois = new ObjectInputStream(request.getInputStream());
            RequestObject requestObject = (RequestObject) ois.readObject();

            response.setContentType("application/json");

            String jsonResponseString = new ServerApplication().doMoreWithRequest(requestObject);

            OutputStreamWriter wr = null;

            if (null != aEncoding && aEncoding.toLowerCase().indexOf("gzip") != -1) {
                gzipos = new GZIPOutputStream(response.getOutputStream());
                wr = new OutputStreamWriter(gzipos);                    
                response.addHeader("Content-Encoding", "gzip,deflate");                 
            } else {
                wr = new OutputStreamWriter(response.getOutputStream());
            }

            wr.write(jsonString);
            wr.flush();



    }

}
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • Have you considered using Jetty's NIO? It has [NIO Connector](http://download.eclipse.org/jetty/7.6.17.v20150415/apidocs/org/eclipse/jetty/server/nio/SelectChannelConnector.html) which may be sufficient (never worked with it however). – user3707125 Jun 07 '15 at 05:25
  • No actually. In forums someone (http://stackoverflow.com/questions/8639625/use-jetty-or-netty) doubt it and has suggested for Netty. But I'll check that too. – ironwood Jun 07 '15 at 05:53
  • The thing is that Netty is a very different kind of beast - it is not that straightforward how to switch to it. If you have a decent amount of existing code, or you don't have much free time, then I would suggest to avoid this migration unless it is really necessary. – user3707125 Jun 07 '15 at 05:57
  • Hmmm... I got your point! Code is decent (more or less) and yes I'm having a fight with the time also. But the hunger for speed is also very big.Thank you user3707125. – ironwood Jun 07 '15 at 06:13

0 Answers0