aspose file tools*
The moose likes Servlets and the fly likes Java time with servlet in-accurate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java time with servlet in-accurate" Watch "Java time with servlet in-accurate" New topic
Author

Java time with servlet in-accurate

phillip nash
Greenhorn

Joined: Feb 08, 2011
Posts: 21
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

Joined: Mar 09, 2012
Posts: 6
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

Joined: Feb 08, 2011
Posts: 21
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

Joined: Mar 09, 2012
Posts: 6
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
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

What are you doing? making some sort of Time Server?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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.
 
Consider Paul's rocket mass heater.
 
subject: Java time with servlet in-accurate