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();
}
}