Discussion:
Singleton pattern using Servlet init() and <load-on-startup> ?
Steven Elliott
2002-03-29 18:37:08 UTC
Permalink
I have tried just about every way I know or can invent but cannot seem to
put my head around the problem of implementing a single instance of my
servlet and using <load-on-startup>.

If I put my Singleton servlet in my application directory and in the WEB.XML
file direct it to <load-on-startup> it gets instantiated and initialized
four (4) times.

For instance the following will print out "Singleton started" four times if
<load-on-startup> is in the apps WEB.XML

---------------------------< code start >---------------------------

public class Singleton extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("Singleton started");
}
}

For some reason I was under the impression that initialization and
instantiation of a servlet would only be done once and by only one
classloader. Apparently that is not correct and worse it appears that the
class is getting instantiated by different classloaders.

So because I cannot use a constructor with a servlet I am a little at a loss
of how to apply the Singleton pattern?

I have looked through the archives and over the 2.3 spec w/o much light.

If anyone has any ideas on how to get a single object instance during
startup I would really appreciate it. Lazy initialization after startup
works fine but does not meet the requirements.

Is there a listener for application startup that I can use to que from?

Thanks,

Steven


--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
Wil Doane
2002-03-29 22:04:25 UTC
Permalink
Steven-

I've focused on enforcing Singleton on the DATA MODEL ONLY by using a
minimal MVC structure... I couldn't care less how many instances of
the controller accessing my data model exist, so long as they all
access the same data structure, and so long as I synchronize the
accessor methods in the DataModel class, I'm happy.

That is to say, I'm not sure that it's necessary to force there to be
a single instance of a given servlet, so long as the data structures
accessed by that servlet enforce Singleton.

Any worker classes that want to create an instance of the data
structure represented in the simplified code below would need to call
the "getInstance()" method, since there is no public constructor.
Subsequent access would be through synchronized methods, such as
"clearData()".

An example of such a worker class would be the actual servlet being
made available via Tomcat.

public class DataModel {

/** The singleton DataModel instance. */
private static DataModel theInstance;

/** The internal data implementation. */
private DataStructure myData;

/** Private constructor used to create a single instance of myData */
private DataModel () {
this.myData = new DataStructure();
}

/**
* Get the single instance of DataModel object.
*
* @return A DataModel.
*/
public static DataModel getInstance() {
if (DataModel.theInstance == null) {
DataModel.theInstance = new DataModel ();
}
return DataModel.theInstance;
}

/** Clears the data. */
public synchronized void clearData() {
this.myData = new DataStructure();
}

}



-Wil
Post by Steven Elliott
I have tried just about every way I know or can invent but cannot seem to
put my head around the problem of implementing a single instance of my
servlet and using <load-on-startup>.
If I put my Singleton servlet in my application directory and in the WEB.XML
file direct it to <load-on-startup> it gets instantiated and initialized
four (4) times.
For instance the following will print out "Singleton started" four times if
<load-on-startup> is in the apps WEB.XML
---------------------------< code start >---------------------------
public class Singleton extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("Singleton started");
}
}
For some reason I was under the impression that initialization and
instantiation of a servlet would only be done once and by only one
classloader. Apparently that is not correct and worse it appears that the
class is getting instantiated by different classloaders.
So because I cannot use a constructor with a servlet I am a little at a loss
of how to apply the Singleton pattern?
I have looked through the archives and over the 2.3 spec w/o much light.
If anyone has any ideas on how to get a single object instance during
startup I would really appreciate it. Lazy initialization after startup
works fine but does not meet the requirements.
Is there a listener for application startup that I can use to que from?
Thanks,
Steven
--
--
William E. J. Doane ***@hawaii.edu
Department of Information & Computer Science
University of Hawaii - Manoa
1680 East West Rd
POST 309
Honolulu, HI 96822


"If you don't respect others, you're not doing it right. I try always
to let my great respect show through for people who try hard to do
the right thing. And sure enough, they do try, in almost every case.
The others, who are perhaps trying in some way I don't understand...
I Respect them too... and wish them success elsewhere."
- Ron Jeffries on XP

--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
Steven Elliott
2002-03-30 12:25:04 UTC
Permalink
Post by Wil Doane
Steven-
I've focused on enforcing Singleton on the DATA MODEL ONLY by using a
minimal MVC structure... I couldn't care less how many instances of
the controller accessing my data model exist, so long as they all
access the same data structure, and so long as I synchronize the
accessor methods in the DataModel class, I'm happy.
That is to say, I'm not sure that it's necessary to force there to be
a single instance of a given servlet, so long as the data structures
accessed by that servlet enforce Singleton.
< snip the example >

Aloha Wil.
Thanks for the reply but I don't implicitly agree that it is possible with
the code you provided to implement the Singleton pattern in a class
instantiated from a servlet invoked with <load-on-startup> in the Tomcat
container.

Why? Because I think the issue here is that more than 1 classloader is at
work. If you have a moment please check my code.

The code I used to test (sorry for the line wraps):

------------------------------< Your DataModel class code >-----------------
// Declaration of worker class DataModel
public class DataModel {

/** Ths singleton DataModel instance */
private static DataModel theInstance;

/** Internal data implementation */
private static Long myData;

/** Private constructor used to create a single instance of myData */
private DataModel() {
this.myData = new Long(System.currentTimeMillis());
}

public static Long getMyData() {
return myData;
}

/** Get the single instance of DataModel object. */
public static DataModel getInstance() {
System.out.println("DataModel is alive ="+(DataModel.theInstance
!= null));
if (DataModel.theInstance == null) {
DataModel.theInstance = new DataModel();
}
return DataModel.theInstance;
}
}
------------------------------< Servlet code >-----------------
public class Singleton extends HttpServlet {

DataModel dataModel;

public void init()
throws ServletException
{
System.out.println("Singleton initialized");
this.dataModel = DataModel.getInstance();
System.out.println("Got instance "+dataModel.getMyData());
}


public void destroy() {
System.out.println("Singleton destroyed "+dataModel.getMyData());
}

}

------------------------------< Results >-----------------
Starting service Tomcat-Standalone
Apache Tomcat/4.0.3
Singleton initialized
DataModel is alive = false
Got instance 1017488519801
Singleton initialized
DataModel is alive = false
Got instance 1017488523258
Singleton initialized
DataModel is alive = false
Got instance 1017488525010
Singleton initialized
DataModel is alive = false
Got instance 1017488526991

When I stop Tomcat I get the following:
Stopping service Tomcat-Standalone
Singleton destroyed 1017488519801
Singleton destroyed 1017488523258
Singleton destroyed 1017488526991
Singleton destroyed 1017488525010

So it would seem that either I am doing something entirely wrong or there
are four instances of DataModel each with a different DataStructure? Is it
my implementation of your singleton pattern or is it that Tomcat is somehow
instantiating 4 different instances of the DataModel and DataStructure?
Because under normal circumstances I would say that your singleton pattern
looks fine.

But even if your code (and my implementation) proved to implement a
singleton I would still have at least two other problems:

(1) a separate class outside of Tomcat does not meet my needs
(2) the apparent conflict between the 2.3 Servlet spec and Tomcat's
implementation.

(1)
My original purpose was to provide a means for my application to schedule
certain utility functions such as periodic email scheduling of database
reports, etc. These classes (TimerTasks) would be scheduled at startup from
parameters with a Timer servlet. I thought that it would be nice if these
utility classes could take advantage of the Application resouces (such as
the datasource pools). But if I instance a class w/o Context such as your
DataModel, these resources cannot be made available. Only if I use a
resource with Context within Tomcat can I get these benefits (or so it seems
to me since no one has been kind enough to reply to my other email regarding
how to pass access to JNDI Contexts from normal class files invoked from the
command line (eg. JCronTab)). But not to digress too much and I hope you
see why I need to start this up from a Servelt and preferrably one which
starts on loadup. You can also see why if 4 servlets of this type are
started what a waste of resources not to mention my client's irritation at
receiving 4 different versions of the same report.

(2)
The second problem is that in the 2.3 Servlet spec it strongly indicates
that only one instance of a Servlet will be invoked EXCEPT in the case where
that Servlet implements the SingleThreadModel. In load conditions, the
Container can invoke more instances of classes implementing the
SingleThreadModel interface.

So, I would say that either my interpretation of the 2.3 Specs is incorrect
or Tomcat is instantiating (and keeping alive!!!) more than one instance of
a <load-on-startup> Servlet. Possibly even more serious is that fact that
each servlet is getting initialized with different values.

In any case if you see problems of my implementation of your code or want me
to try something else please let me know and I'll get it done. In the
meantime I'll wait and hope to hear something from Craig or another of the
TC developers shedding some light on this problem before filing a Bug
report.

Mahalo...

Steven

BTW I can only find in the logs (Application and Catalina logs) one
declaration of the Singleton.class being started!!! How weird.


--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
Derek Stedman
2002-03-30 15:41:54 UTC
Permalink
In my meanderings around the web I found that AJP is an acronym for
Apache JServ Protocol - this got me wondering -

Does anyone know if WARP is an acronym? and if so what does it stand for..

--Derek Stedman


--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
Nikola Milutinovic
2002-04-01 05:30:02 UTC
Permalink
Post by Derek Stedman
In my meanderings around the web I found that AJP is an acronym for
Apache JServ Protocol - this got me wondering -
Does anyone know if WARP is an acronym? and if so what does it stand for..
Web Application R????
a***@yahata.com
2002-04-01 05:46:26 UTC
Permalink
I am configuring Tomcat with ssl.

my system is;

jakarta-tomcat-4.0.1
jsse-1_0_2-gl
j2sdk-1_3_1_03


I put jcert.jar jnet.jar jsse.jar in $JAVA_HOME/jre/lib/ext.
My apache is OK with ssl, and also Tomcat without ssl.

When I take away <--- and --> from text below,

<!--
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8443" minProcessors="5" maxProcessors="75"
enableLookups="false"
acceptCount="10" debug="0" scheme="https" secure="true">
<Factory className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
</Connector>
-->

Tomcat seems ok to bootup, But can not connect from web browser, just
keeping
timeout.

Wnen I coment out the text below;

<!--
<Factory className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
-->

Tomcat works, but not with SSL.

Please someone help me?

Akihiro


--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
Jacob Kjome
2002-03-30 20:25:06 UTC
Permalink
Probably "Web Application Archive Protocol" since .war files are Web
Application Archives

Jake
Post by Derek Stedman
In my meanderings around the web I found that AJP is an acronym for
Apache JServ Protocol - this got me wondering -
Does anyone know if WARP is an acronym? and if so what does it stand for..
--Derek Stedman
--
Lawlor, Frank
2002-04-03 03:38:42 UTC
Permalink
How are you trying to connect?
What is the URL you are using?
Is there anything in the logs?
I assume you followed all the
directions in the How-to?

Frank Lawlor
Athens Group, Inc.
(512) 345-0600 x151
Athens Group, an employee-owned consulting firm integrating technology
strategy and software solutions.
-----Original Message-----
Sent: Sunday, March 31, 2002 11:46 PM
To: Tomcat Users List
Subject: tomcat with ssl
I am configuring Tomcat with ssl.
my system is;
jakarta-tomcat-4.0.1
jsse-1_0_2-gl
j2sdk-1_3_1_03
I put jcert.jar jnet.jar jsse.jar in $JAVA_HOME/jre/lib/ext.
My apache is OK with ssl, and also Tomcat without ssl.
When I take away <--- and --> from text below,
<!--
<Connector
className="org.apache.catalina.connector.http.HttpConnector"
port="8443" minProcessors="5" maxProcessors="75"
enableLookups="false"
acceptCount="10" debug="0" scheme="https"
secure="true">
<Factory
className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
</Connector>
-->
Tomcat seems ok to bootup, But can not connect from web browser, just
keeping
timeout.
Wnen I coment out the text below;
<!--
<Factory className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
-->
Tomcat works, but not with SSL.
Please someone help me?
Akihiro
--
--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
T***@csiro.au
2002-04-03 03:54:27 UTC
Permalink
I have noticed that there is no SSL keystore specified in the factory...
that will kill the SSL on startup, as it will try to laod SSL certificates
it wants to use...

regards
TMC


--
Tomasz M. Ciolek
Systems Administrator - CSIRO Entomology
Phone: 02-62464391 * Fax: 02-62464000
-----Original Message-----
Sent: Wednesday, 03 April 2002 13:39
To: 'Tomcat Users List'
Subject: RE: tomcat with ssl
How are you trying to connect?
What is the URL you are using?
Is there anything in the logs?
I assume you followed all the
directions in the How-to?
Frank Lawlor
Athens Group, Inc.
(512) 345-0600 x151
Athens Group, an employee-owned consulting firm integrating technology
strategy and software solutions.
-----Original Message-----
Sent: Sunday, March 31, 2002 11:46 PM
To: Tomcat Users List
Subject: tomcat with ssl
I am configuring Tomcat with ssl.
my system is;
jakarta-tomcat-4.0.1
jsse-1_0_2-gl
j2sdk-1_3_1_03
I put jcert.jar jnet.jar jsse.jar in $JAVA_HOME/jre/lib/ext.
My apache is OK with ssl, and also Tomcat without ssl.
When I take away <--- and --> from text below,
<!--
<Connector
className="org.apache.catalina.connector.http.HttpConnector"
port="8443" minProcessors="5" maxProcessors="75"
enableLookups="false"
acceptCount="10" debug="0" scheme="https"
secure="true">
<Factory
className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
</Connector>
-->
Tomcat seems ok to bootup, But can not connect from web
browser, just
keeping
timeout.
Wnen I coment out the text below;
<!--
<Factory className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
-->
Tomcat works, but not with SSL.
Please someone help me?
Akihiro
--
For
--
To
--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
Loading...