Tomcat and classloaders

E

Erik Berglund

Hi
I do not know if this is correct forum but I could not find any "free"
tomcat forum right now. So:

I have a problem using Tomcat (version 4.1.30) in an environment where
we have configured it with about 10 different hosts (used together
with virtual hosts in Apache webserver ). Theses hosts are different
web applications used by different countries with different time
zones.

The problem is when setting the default java.util.TimeZone in one web
application it will also affect another web application under a
completely different host.

I guess this is beacause core java classes are shared among all
defined hosts and just loaded once. Is this correct ? Is it in any way
possible to configure Tomcat so that each host can have its own
default TimeZone?

Any idea how to solve this would be helpful

/Erik
 
S

Sudsy

Erik Berglund wrote:
The problem is when setting the default java.util.TimeZone in one web
application it will also affect another web application under a
completely different host.

From the javadocs:
"Typically, you get a TimeZone using getDefault which creates a
TimeZone based on the time zone where the program is running."
----------------------------
I guess this is beacause core java classes are shared among all
defined hosts and just loaded once. Is this correct ? Is it in any way
possible to configure Tomcat so that each host can have its own
default TimeZone?

You're talking about the tail wagging the dog. What you SHOULD be
investigating in i18n (internationalization). Get the timezone from
the browser and display strings such as data/time or currency in the
format appropriate for the user timezone.
It's even easier if you use a framework like Struts. Elements such
as bean:write will automagically use a localized format for Date
strings.
 
B

Babu Kalakrishnan

Erik said:
I do not know if this is correct forum but I could not find any "free"
tomcat forum right now. So:

I have a problem using Tomcat (version 4.1.30) in an environment where
we have configured it with about 10 different hosts (used together
with virtual hosts in Apache webserver ). Theses hosts are different
web applications used by different countries with different time
zones.

The problem is when setting the default java.util.TimeZone in one web
application it will also affect another web application under a
completely different host.

I guess this is beacause core java classes are shared among all
defined hosts and just loaded once. Is this correct ? Is it in any way
possible to configure Tomcat so that each host can have its own
default TimeZone?

I would say depending on Objects that use the Default Locale / Timezone values
isn't a very robust design anyway for an application meant to cater to varying
Locales. As far as I can think, most (all?) classes that need Locale / Timezone
information for proper functioning (like character I/O, Calendars etc) have
constructors that allow you to specify a specific non-default value for these
and applications such as yours must use those and must never be dependent on
setting the system defaults.

BK
 
E

Erik Berglund

Babu Kalakrishnan said:
I would say depending on Objects that use the Default Locale / Timezone values
isn't a very robust design anyway for an application meant to cater to varying
Locales. As far as I can think, most (all?) classes that need Locale / Timezone
information for proper functioning (like character I/O, Calendars etc) have
constructors that allow you to specify a specific non-default value for these
and applications such as yours must use those and must never be dependent on
setting the system defaults.

BK

Thanks for your hint.
But that do not solve my problem right now. To clarify: each
application does not deal with different time zones. For example:
Lithuania has its own application hosted on one host and Sweden has
its own application hosted on another host. Why do they not get
different default time zones ? Is it not possible to have each host
running in its own process (virtual machine) ? Earlier we used
WebSphere to host all these countrie's applications (but that was not
as quick as Tomcat and of course more expensive) and I do not remember
having this problem.

Regards
Erik
 
B

Babu Kalakrishnan

Erik said:
But that do not solve my problem right now. To clarify: each
application does not deal with different time zones. For example:
Lithuania has its own application hosted on one host and Sweden has
its own application hosted on another host. Why do they not get
different default time zones ? Is it not possible to have each host
running in its own process (virtual machine) ? Earlier we used
WebSphere to host all these countrie's applications (but that was not
as quick as Tomcat and of course more expensive) and I do not remember
having this problem.

By "host" do you mean different physical computers or a different
webapp running under the same Tomcat instance ?

If it is the former, I would expect them to be running under different
JVMs and you should be able to setup each one by calling the
TimeZone.setDefault(TimeZone) method (I'm not sure if there is a
system property that defines this which would make it easier to
provide the value on the commandline).

If they are different webapps running under the same Tomcat instance,
I'm not very sure if Tomcat provides a way for core library classes to
be loaded from a different classloader. As far as I can recall, only
external libraries found in the WEB-INF/lib directory or classes found
in the WEB-INF/classes directory are loaded by the Context classloader
in preference to an already existing class in the parent (system)
classloader.

In such a scenario, the only workaround that I can see is to run
multiple instances of Tomcat (and bind them to different AJP connector
ports if they are on the same physical machine) so that each process
runs under its own private JVM (might be pretty resource intensive if
done on a single machine)

BK
 
E

Erik Berglund

Babu Kalakrishnan said:
By "host" do you mean different physical computers or a different
webapp running under the same Tomcat instance ?

Under the same Tomcat instance but under different virtual hosts
If it is the former, I would expect them to be running under different
JVMs and you should be able to setup each one by calling the
TimeZone.setDefault(TimeZone) method (I'm not sure if there is a
system property that defines this which would make it easier to
provide the value on the commandline).

If they are different webapps running under the same Tomcat instance,
I'm not very sure if Tomcat provides a way for core library classes to
be loaded from a different classloader. As far as I can recall, only
external libraries found in the WEB-INF/lib directory or classes found
in the WEB-INF/classes directory are loaded by the Context classloader
in preference to an already existing class in the parent (system)
classloader.

In such a scenario, the only workaround that I can see is to run
multiple instances of Tomcat (and bind them to different AJP connector
ports if they are on the same physical machine) so that each process
runs under its own private JVM (might be pretty resource intensive if
done on a single machine)

Yepp, this works great ! Just defining a CATALINA_BASE for each user
(country) then each country has its own configuration file :) I had
to fiddle around with the ports for the connectors and the server port
as well to prevent bringing down wrong instance when stopping. Now it
looks like this:
One Apache installation configured with a bunch of virtual hosts. Each
virtual host connects to its own tomcat instance by using different JK
"workers".
Now it is even possible to take down tomcat completely for one country
only. This is specially good since we have had problems replacing WAR
files which forced us to restart tomcat completely which all countries
then suffered from.
What I also noticed was that our xalan.jar which we had in shared/lib
was not loaded anymore. So that needed to be copied as well to
CATALINA_BASE
Well I am not a Tomcat expert but this works. Thanks !


/Erik
 
B

Babu Kalakrishnan

Erik said:
Yepp, this works great ! Just defining a CATALINA_BASE for each user
(country) then each country has its own configuration file :) I had
to fiddle around with the ports for the connectors and the server port
as well to prevent bringing down wrong instance when stopping.

Glad that you managed to solve your immediate problem. But it might
still be worthwhile to modify your codebase as well wherein you don't
rely on the default TimeZone property and construct class instances with
explicit Timezones specified. That approach would be much cleaner and
would allow you to run multiple webapps under a single Tomcat instance.

BK
 
A

Albretch

I think there are a number of issues that relate to your architecture
in general, other than the 'Time zones' and 'virtual hosts'
(relatively innocuous) issues you had

1._ Time zones in a server: I wondered what would be the technical or
cultural meaning of it in an open-to-the-Net server running 24/7
anyway. All servers should use the same time zone preferably, UGT. I
think, even internally, OSs spend time doing these translations, and
everyone should hit your server from different time zones anyway,
right?
Not only that, but you should be glad you had a clear 'error' you
could notice right away. There are a number of relatively subtle
issues that relate to your flawed architecture, too. The next
‘natural/consequent' step to using ‘Time zones' in a server would be
using ‘day light savings' (I am not bush-it-ting you I have seen this
out there), and say you use versioning or an application that
internally/silently uses it (like Hibernate) or some kind of
'optimistic'-concurrency one.
When these applications compare time differences they might go crazy
with their negative values they get (many of them just care about time
differences) and even throw errors and/or Exceptions that would be
hard to debug/understand without the right mindset.
Java gets its System.currentTimeMillis and java.util.Date data right
off the OS . . . so by now you could see my point

2._ Classloaders: and their relationship with the CATALINA_BASE
setting for each 'virtually' hosted server.
Web developers should –always- (do not trust different versions of
the same app server) test the priority of the loading sequences just
by placing the same class in different directories, with different
markers (like the actual directory it is loaded from) and asking the
class what its classloader was (check out java.lang.Class) you could
have seen what your prob was ‘clear and loud'

3._ Your code: I could see you rolling up your sleeves :) Why would
anyone conceive/code an open-to-the-world server application wired to
a 'time zone'? Specially if java's libraries are so dirt easy to
i18n'ed and l10n'ed?

AM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top