File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java time with servlet in-accurate

 
phillip nash
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm having trouble with obtaining the current second from my servlet.



As you can see, the code looks totally fine. But, actually, the problem is when I'm running it in my browser (Chrome) the seconds are correct. However, at times it actually skips a second, which is messing up parts of my application.
When I use System.nanoSeconds() I've noticed every time I get the current time (using an auto refresher - refresh every second) a few of the milli digits are actually increasing every seconds the auto refresh kicks in. Eventually, the servlet skips a second.

Any tips please? The code you see is the only code that's actually running.
 
Oli Wurster
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you debugged the servlet? Is there a new GET request every 1 second? I think that it could be a network or client problem. The auto refresher probably is some kind of client script that refreshes the page every interval. It could also be a problem on the auto refresher.
 
phillip nash
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oli Wurster wrote:Have you debugged the servlet? Is there a new GET request every 1 second? I think that it could be a network or client problem. The auto refresher probably is some kind of client script that refreshes the page every interval. It could also be a problem on the auto refresher.


Thanks for the reply. There's a get request every second. This is running on my localhost machine (until deployment).
Do you think there can be a performance hit when running GlassFish on a localhost machine? Rather than a dedicated
server? Personally, I think its because my dev-machine. Its kinda slow and crap to run GlassFish
 
Oli Wurster
Greenhorn
Posts: 6
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I can't say anything about performance on Glassfish. But I don't think it is the server. I rather think it is a timing problem. Imagine the following: The client requests to the server every second (with a precision of some micro-seconds). Then there is the transport of the request (TCP/IP) to the server, that takes some microseconds on localhost. Now if two requests are send by the client, the first arrives just before a certain time, for example on 16:00:00.999, the next request shoud be on 16:00:01.999, but is just a few micro seconds later. then the response will be 16:00:02.004 (ore something). This makes it look like the server time jumps sometime. This is just a theory. Try to write the exact times to the console or the logs on Glassfish.

You could sync the time every minute (or every 10 minutes) and use the JavaScript Date object on the Page it self. This way you would have less network traffic. If the app will be running over a network (instead of localhost only) There will be a bigger problem with the time the request and response use.

I hope that helps.
 
Jayesh A Lalwani
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you doing? making some sort of Time Server?
 
Paul Clapham
Sheriff
Pie
Posts: 20185
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phillip nash wrote:There's a get request every second.


Have you confirmed that this is happening precisely once per second? And how precisely is it happening? And have you confirmed that there are no network delays which reduce the precision of that measurement? And no delays in your server either?

You suggest that there's a problem in your application if a certain piece of code doesn't run exactly once per second. I wouldn't choose sending a GET request from a client system to fulfil that requirement -- but perhaps I didn't understand your requirement.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic