Sam
Sam

Reputation: 565

URLConnection getInputStream()/openStream() is slow for the first time

I am executing the following line, as soon as the Tomcat server(5.5.28) is started..

URL  url = new URL("http://localhost:80/servlets/ConnectionCheckServlet");
URLConnection uc = url.openConnection();
long t=System.currentTimeMillis();
//InputStream content = (InputStream)uc.getInputStream();
InputStream content = (InputStream)url.openStream();
System.out.println("Time diff: "+(System.currentTimeMillis()-t));

The Timediff in millisecs got printed as Time diff: 5141 (> 5 Seconds)

For the subsequent call on the same servlet or for a different servlet, the time diff is actually less than a second.

Will the first time delay, some thing to do with Tomcat intialization??

Upvotes: 1

Views: 1566

Answers (1)

František Žiačik
František Žiačik

Reputation: 7612

I guess it could be due to compilation and/or caching of the servlet which could occur the first time accessed.

Upvotes: 2

Related Questions