You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Steven Elliott <tt...@mac.com> on 2002/03/29 19:37:08 UTC

Singleton pattern using Servlet init() and ?

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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: WARP acronym?

Posted by Jacob Kjome <ho...@visi.com>.
Probably "Web Application Archive Protocol" since .war files are Web 
Application Archives

Jake

At 10:41 AM 3/30/2002 -0500, you wrote:

>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:   <ma...@jakarta.apache.org>
>For additional commands: <ma...@jakarta.apache.org>
>Troubles with the list: <ma...@jakarta.apache.org>

tomcat with ssl

Posted by ak...@yahata.com.
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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: WARP acronym?

Posted by Nikola Milutinovic <Ni...@ev.co.yu>.
> 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????? Protocol.

Nix.

WARP acronym?

Posted by Derek Stedman <ds...@onlinewebdesign.com>.
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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Singleton pattern using Servlet init() and ?

Posted by Steven Elliott <tt...@mac.com>.
On 29/03/02 22:04, "Wil Doane" <wd...@hawaii.edu> wrote:

> 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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Singleton pattern using Servlet init() and ?

Posted by Wil Doane <wd...@hawaii.edu>.
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

>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:   <ma...@jakarta.apache.org>
>For additional commands: <ma...@jakarta.apache.org>
>Troubles with the list: <ma...@jakarta.apache.org>


-- 

William E. J. Doane                                   wdoane@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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>