You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Salisbury, Richard W DLA CTR INFORMATION OPERATIONS" <Ri...@dla.mil> on 2015/03/06 22:29:09 UTC

How to force Tomcat to use the system clock?

Greetings,

We have found a need to stop and start Tomcat once in a while to allow
Tomcat to connect via HTTPS with some other servers.  We think the
restart may be synchronizing the time Tomcat uses with the server OS
system time, and we are looking for ways to prevent having to stop/start
Tomcat.

Details:
Our instance of Tomcat 6.0.36 runs on HP-UX B11.31 ia64 with JVM Version
1.7.0.08.  It hosts a custom servlet which, when invoked, connects with
a remote server via HTTPS to retrieve some data.  However, after about a
month the timestamp Tomcat sends in the SSL handshake appears to drift
enough for the remote server's time to start rejecting requests because
the timestamp is too far off (according to our partner's remote
application logs).  

We have confirmed that our server clock is set correctly and synced with
NTP, and matches the system clock on the remote server, which also uses
NTP.  So one thing we thought might be happening is that Tomcat (or the
Java that Tomcat runs on) may be keeping an internal clock, perhaps
using a separate thread as a way to speed up the retrieval of time so
that it does not have to go to the OS system clock every time it needs
the current time.  And maybe this internal clock is not synced with the
server time until Tomcat (or the JVM) is restarted.
	
If this is the case, would anyone have an idea of how to force Tomcat
(or Java) to use the server's system clock every time instead of using
its own internal clock?   We do not care about the performance hit on
this because this is a low-volume application.  Or, if we are
misunderstanding Tomcat and it actually uses the system clock every time
it needs to get the current time, is there something else we should be
looking at?
	
We have researched on the web, checked the Apache mail archives, read
the Tomcat configuration guide, looked up the Java system options, but
have not studied the Tomcat source code yet.  We did find that there is
a Java Wrapper product out there by Tanuki Software that provides an
option to use system time or a background thread, which is what caused
us to wonder if Tomcat might be doing something similar.

For more information on what the Tanuki wrapper does, here is an excerpt
we found on their website
http://wrapper.tanukisoftware.com/doc/english/prop-use-system-time.html:
"As of Wrapper version 3.1.0, a new timer mechanism was added to the
Wrapper. This new timer was made the default in Wrapper version 3.2.0.
Rather than keeping time by querying the system clock, the Wrapper
creates a background thread which enters a light weight loop and
increments an internal "tick" counter.  Internally all timekeeping has
been modified to be based on these "ticks". (If the system time is being
used, then the tick count at any particular moment is calculated from
the system time rather than from the counter.) "

Thanks in advance for any ideas that are shared. 
Richard


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to force Tomcat to use the system clock?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Chuck,

On 3/6/15 6:06 PM, Caldarale, Charles R wrote:
>> From: Christopher Schultz [mailto:chris@christopherschultz.net] 
>> Subject: Re: How to force Tomcat to use the system clock?
> 
>> Tomcat absolutely does not use any internal clock of its own. The
>> JRE almost certainly does not, either.
> 
> Don't be quite so sure about the JRE in this case.  This is HP's
> own JVM, derived from a Sun JVM specifically for HPUX on Itanium 
> hardware.  I don't believe it's being actively developed anymore,
> but I know HP threw in a fair number of tweaks for their platform.

Fair enough. I did give the OP the code and technique to test whether
the JRE respects the system time even after launching of not.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU+1RKAAoJEBzwKT+lPKRY44oP/1I2f0tE+cqhuc3sgqwEYbeu
FVKB+70rGVw2r7rqTNi8YsI7chemuIVcD217Sb5eNyzOTbi0hV8kUdOaQSyrOwnG
c9+/okzYsJdtByHiMO6R/EuYE6A3Sc1ieO3MlSxXzOZ+RCPy8lrh6PpyKIPdyL6G
rrVgHlCxKd5rxD6VvzSRqVK8bkcucOa2DwcB66cUgLHRH86mxMndTieWmFoH1nOy
llfMEuY6Vfo8okTNNE7D1P8jxcuUTvRZTh5EQMe4K60jMFhAdFrWdZQ90lpDxxQw
+mBDBM68zojNzQWxMCsd7TL3wR9IaFykAHL/Xicx1yiOEzp/Zgm4t12CNV81iXfD
7vqhIXXXE+z/7H3X0W1Xb3jqRIu8qJRnFHlSsvf1VKrxBzP5kw/xgW24ELijt2aP
xynxy+0C2CGjmhAi0aO/Sei5aWr8i6p1etNGDIT6NvED2i1CQMphLJR+xbfJOi3u
32yCV5zGT3tA08XXJfMkZLtQDDzIZGpQbQj+y4XM3xBgqZsGYF+lTDT3c9Rwph7T
rj1fymsfoxYe9Lbm+zZ9mthk8zz1VDM/ARufIl4KKsDXGuD6SjGz2l8vuEAbzvHM
3B2wnQ15iOPqtgx1uiW3XH4YVty67zs8t+ETexCDiTs6RdK8xSqVkDGr291EnePH
j0J69IV0caXEKBVS03zC
=//dI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to force Tomcat to use the system clock?

Posted by Hassan Schroeder <ha...@gmail.com>.
On Fri, Mar 6, 2015 at 3:06 PM, Caldarale, Charles R
<Ch...@unisys.com> wrote:

> Don't be quite so sure about the JRE in this case.  This is HP's own JVM, derived from a Sun JVM specifically for HP UX on Itanium hardware.  I don't believe it's being actively developed anymore, but I know HP threw in a fair number of tweaks for their platform.

Write Once, Debug Everywhere   :-)

-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How to force Tomcat to use the system clock?

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Christopher Schultz [mailto:chris@christopherschultz.net] 
> Subject: Re: How to force Tomcat to use the system clock?

> Tomcat absolutely does not use any internal clock of its own. The JRE
> almost certainly does not, either.

Don't be quite so sure about the JRE in this case.  This is HP's own JVM, derived from a Sun JVM specifically for HPUX on Itanium hardware.  I don't believe it's being actively developed anymore, but I know HP threw in a fair number of tweaks for their platform.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.

 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to force Tomcat to use the system clock?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Richard,

On 3/6/15 4:29 PM, Salisbury, Richard W DLA CTR INFORMATION OPERATIONS
wrote:
> We have found a need to stop and start Tomcat once in a while to
> allow Tomcat to connect via HTTPS with some other servers.  We
> think the restart may be synchronizing the time Tomcat uses with
> the server OS system time, and we are looking for ways to prevent
> having to stop/start Tomcat.
> 
> Details: Our instance of Tomcat 6.0.36 runs on HP-UX B11.31 ia64
> with JVM Version 1.7.0.08.  It hosts a custom servlet which, when
> invoked, connects with a remote server via HTTPS to retrieve some
> data.  However, after about a month the timestamp Tomcat sends in
> the SSL handshake appears to drift enough for the remote server's
> time to start rejecting requests because the timestamp is too far
> off (according to our partner's remote application logs).

I was about to suggest that every six months or so, you have to bounce
Tomcat. But you are saying it's roughly a month. Let me tell you why I
thought it might be every six months:

Your JRE is quite old (if it really is 1.7.0_08), and so its got
outdated time zone definitions, especially DST corrections.

Oracle created a timezone-update tool because the process was getting
to be a royal pain, especially with users paranoid about upgrading the
JRE without 18 years of testing.
http://www.oracle.com/technetwork/java/javase/tzupdater-readme-136440.html

You can see the changelog here:
http://www.oracle.com/technetwork/java/javase/tzdata-versions-138805.html

You'll notice that there are a whole bunch of updates to the time
zones between ~2012 when 1.7.0_08 was released and now.

Are you sure that Java's timestamps don't look right? Or are you just
finding that SSL handshakes aren't working and blaming the "Java
system time" (whatever definition that might have) for it?

I like Jason Pyeron's suggestion that you create a simple JSP that
will give you the current time according to Java. I think you'll find
that Java's system time tracks your OS's system time exactly.

We have servers running for months with no such drift, and we also use
NTP (because hardware clock drift is in fact a real problem). We also
run on both bare metal and virtualized hardware and have never had
such problems.

> We have confirmed that our server clock is set correctly and synced
> with NTP, and matches the system clock on the remote server, which
> also uses NTP.  So one thing we thought might be happening is that
> Tomcat (or the Java that Tomcat runs on) may be keeping an internal
> clock, perhaps using a separate thread as a way to speed up the
> retrieval of time so that it does not have to go to the OS system
> clock every time it needs the current time.  And maybe this
> internal clock is not synced with the server time until Tomcat (or
> the JVM) is restarted.

Tomcat absolutely does not use any internal clock of its own. The JRE
almost certainly does not, either. It should all come down the system
time.

Before I posted, I checked by running this Java program:
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;

public class CurrentTime {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        while(true) {
            System.out.println(df.format(new Date()));
            Thread.currentThread().sleep(1000);
        }
    }
}

This just prints the current time every second, forever.

In another terminal, I performed these commands while the above
program was running:

$ sudo date -s '2015-03-06 17:52:00'
Fri Mar  6 17:52:00 EST 2015
(wait a few seconds)
$ sudo date -s '2015-03-06 17:50:00'
Fri Mar  6 17:50:00 EST 2015

This is the output from the Java program:

2015-03-06 17:49:48
2015-03-06 17:49:49
2015-03-06 17:49:50
2015-03-06 17:49:51
2015-03-06 17:49:52
2015-03-06 17:49:53
2015-03-06 17:49:54
2015-03-06 17:49:55
2015-03-06 17:49:56
2015-03-06 17:52:00
2015-03-06 17:52:01
2015-03-06 17:52:02
2015-03-06 17:52:03
2015-03-06 17:52:04
2015-03-06 17:52:05
2015-03-06 17:52:06
2015-03-06 17:52:07
2015-03-06 17:52:08
2015-03-06 17:52:09

As you can see, the Java process picked-up the clock-adjustment
immediately.

What's interesting is that the output stalls after I reset the time
back to it's correct time (17:50:00). I suspect that Thread.sleep()
doesn't just set a timeout in milliseconds, but schedules a wake-up at
a particular time in the future.

At 17:52:09 (after the clock was pushed-up relative to the proper
time), Thread.sleep() schedules a sleep until 17:52:10. At 17:52:09, I
changed the time back to 17:50:00, and so it will take 2:10 for the
Thread.sleep to return.

I ran this again, fiddled-around with the system time and waited.
Here's the output:

2015-03-06 17:52:04
2015-03-06 17:52:05
2015-03-06 17:52:06
2015-03-06 17:52:07
2015-03-06 17:52:08
2015-03-06 17:52:09
2015-03-06 17:52:10
2015-03-06 17:52:11
2015-03-06 17:52:12
2015-03-06 17:55:00
2015-03-06 17:55:01
2015-03-06 17:55:02
2015-03-06 17:55:03
2015-03-06 17:55:04
2015-03-06 17:55:05
2015-03-06 17:55:06
2015-03-06 17:55:07
2015-03-06 17:55:08
2015-03-06 17:55:09
(Here there was a 3-minute pause)
2015-03-06 17:55:10
2015-03-06 17:55:11
2015-03-06 17:55:12
2015-03-06 17:55:13

Honestly, I wasn't expecting that at all... I figured
Thread.sleep(1000) meant "wake me up in 1000ms" instead of "wake me up
at 17:55:10". Interesting.

Anyhow, Java tracks the system time, both forward and backward, in
real-time.

I think your problem is elsewhere.

> We did find that there is a Java Wrapper product out there by
> Tanuki Software that provides an option to use system time or a
> background thread, which is what caused us to wonder if Tomcat
> might be doing something similar.

I think this has to do with scheduling in general, and not about
adjusting Java's system time (which isn't actually a thing) in any
way. I think Tanuki's tool is a red herring, here.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJU+jEuAAoJEBzwKT+lPKRYaOIP/0QC7fQZ88mqcy6u80GUK7EB
Y55qUpB2i6Xx3X9y4uvl6TZMfB4lszreFvaGVpULjcbfgtfa0QqxRHtfTX0u5anG
cCxtIcWBKQt88FK0UiJ2jHDu56dG6RLoYPkazj3lwMT6GuJ/D1L5R6PuqwVJjGxy
j/OhCHhFV2sdqX5TT+TZmg7DKcMZrb6RnDwuVhZ9mkdOQ1KhsdJ+dF49l5pEpymd
gLVTfNO6NkM2w1nopMfOpuVlhwRkLl8ZIfR7roCgfqq4Gk8L25SaBY6kIWheOosA
d6Wj00cWZbvnSMtOI011803/drcm/DQ2hNKEfiPUVKjBTLxX4j2L1v7pH1bJRtE6
rs+x7oVyb+vYNKXSI/pblMQvGVI6+nKpjLg5aaNPyzXFUmhoy6g7/WtlNDYdICOZ
0mv+OpbgVQUG84corX9R0D5ceZkNoPW1xOwasPFsnEn3JNUwrvsk0U6kxZ9A97Uf
33bOhXdmiZGaJM+ELmeo3tnv2gohaMmf9XD/Bhm/xDcv4wjEpO0hmm4WvLH7arH4
ZCrX2LStIqT0JvLLxoURC9sptzIKG9NbXBzxeKQDn01RB4xIcx315cO+2oN4krG/
INvQbeBnEwXVbi+sYoQ8Yi+Cql3yk34h6b5PEiGcWhQYZ38FcE6jIgE7C0rmgaOh
jVuH+uGuA5uH2nATUKIB
=YEhT
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: [tomcat-users] How to force Tomcat to use the system clock?

Posted by Jason Pyeron <jp...@pdinc.us>.
> -----Original Message-----
> From: Salisbury, Richard W DLA CTR INFORMATION OPERATIONS 
> Sent: Friday, March 06, 2015 16:29
> 
> Greetings,
> 
> We have found a need to stop and start Tomcat once in a while to allow

How long is a "while"?

> Tomcat to connect via HTTPS with some other servers.  We think the
> restart may be synchronizing the time Tomcat uses with the server OS
> system time, and we are looking for ways to prevent having to 
> stop/start
> Tomcat.

I want to start with, I am very skeptical to your observations. 

> 
> Details:
> Our instance of Tomcat 6.0.36 runs on HP-UX B11.31 ia64 with 
> JVM Version

JVM vendor?

> 1.7.0.08.  It hosts a custom servlet which, when invoked, 
> connects with
> a remote server via HTTPS to retrieve some data.  However, 

The webapp make an outbound HTTPS connection or a client makes a HTTPS connection to the webapp?

> after about a
> month the timestamp Tomcat sends in the SSL handshake appears to drift
> enough for the remote server's time to start rejecting 
> requests because
> the timestamp is too far off (according to our partner's remote
> application logs).  

How far off?

> 
> We have confirmed that our server clock is set correctly and 
> synced with
> NTP, and matches the system clock on the remote server, which 
> also uses
> NTP.  So one thing we thought might be happening is that 

What are the NTP stats? In otherwords, how stable is the system clock.

> Tomcat (or the
> Java that Tomcat runs on) may be keeping an internal clock, perhaps

It all boils down to: 

public static native long currentTimeMillis();

Even new java.util.Date() ->

    public Date() {
        this(System.currentTimeMillis());
    }

That means all the "time" source in Java, and Tomcat come from the OS.

> using a separate thread as a way to speed up the retrieval of time so
> that it does not have to go to the OS system clock every time it needs
> the current time.  And maybe this internal clock is not 
> synced with the
> server time until Tomcat (or the JVM) is restarted.
> 	
> If this is the case, would anyone have an idea of how to force Tomcat
> (or Java) to use the server's system clock every time instead of using
> its own internal clock?   We do not care about the performance hit on
> this because this is a low-volume application.  Or, if we are
> misunderstanding Tomcat and it actually uses the system clock 
> every time
> it needs to get the current time, is there something else we should be
> looking at?

Lets start withm can you add a simple JSP to your web app?

<%=System.currentTimeMillis()%>

Then periodically:

date && date -d "@$(curl -s http://127.0.0.1:8080/date.jsp)" && date

> 	
> We have researched on the web, checked the Apache mail archives, read
> the Tomcat configuration guide, looked up the Java system options, but
> have not studied the Tomcat source code yet.  We did find 
> that there is
> a Java Wrapper product out there by Tanuki Software that provides an
> option to use system time or a background thread, which is what caused
> us to wonder if Tomcat might be doing something similar.
> 
> For more information on what the Tanuki wrapper does, here is 
> an excerpt
> we found on their website
> http://wrapper.tanukisoftware.com/doc/english/prop-use-system-
> time.html:
> "As of Wrapper version 3.1.0, a new timer mechanism was added to the
> Wrapper. This new timer was made the default in Wrapper version 3.2.0.
> Rather than keeping time by querying the system clock, the Wrapper
> creates a background thread which enters a light weight loop and
> increments an internal "tick" counter.  Internally all timekeeping has
> been modified to be based on these "ticks". (If the system 
> time is being
> used, then the tick count at any particular moment is calculated from
> the system time rather than from the counter.) "
> 
> Thanks in advance for any ideas that are shared. 
> Richard

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-                                                               -
- Jason Pyeron                      PD Inc. http://www.pdinc.us -
- Principal Consultant              10 West 24th Street #100    -
- +1 (443) 269-1555 x333            Baltimore, Maryland 21218   -
-                                                               -
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
This message is copyright PD Inc, subject to license 20080407P00.