axis-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Javier Gonzalez <jagon...@gmail.com>
Subject Re: Application scope objects that retain data
Date Thu, 04 Aug 2005 14:09:53 GMT
To put it simply, it's a class of wich exactly one instance exists. In
such class you keep your 'persistent' string. It's usually achieved by
making the constructor private (so nobody but the class itself can
call  new SingletonClass(...)) and a static method to access a static
field that holds the one instance. Something like:

public final class MySingleton { // final to avoid multiple instances
by subclassing

 private static MySingleton _instance = new MySingleton();

 private MySingleton(){
   // ... //
 }
 
 public static MySingleton getInstance(){
    return _instance;
 }

 // your useful methods go here
}

that's the general idea. You can get more info here:

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

Or here: (complete, free as in beer, html book on java & patterns)
http://www.pythoncriticalmass.com/downloads/TIPatterns-0.9.zip

On 8/4/05, J.W.F. Thirion <derik@molo.co.za> wrote:
> Thanks for your advice. I did try moving the temp="" part to the top (static
> initializer) and the same thing happens.
> 
> PS: What do you mean by "following the Singleton pattern". I am more from a
> C/C++ background so the terminology is a bit strange to me.
> 
> Thanks for the help!
> Derik
> 
> -----Original Message-----
> From: James Wilson [mailto:wilsonj@ociweb.com]
> Sent: 04 August 2005 15:15
> To: axis-user@ws.apache.org
> Subject: Re: Application scope objects that retain data
> 
> 
> This is interesting.  I can't speak to the Axis internals.  But what this
> shows
> me is that Axis is creating more than one service implementation class.
> Thus
> there is more than one instance of your Test class instanciated even though
> they will live to service many requests.  This is what I would expect for
> Axis
> to support multiple user-request-threads coming in from the Servlet
> container.
> 
> If you move the 'temp=""' line out of the constructor and into a static
> initializer (static String  temp="";) you will get the effect you are
> looking
> for.  But to scale the example, you might want to implement a class
> following
> the Singleton pattern to maintain the application state.
> 
> hope it helps,
> james
> 
> Quoting "J.W.F. Thirion" <derik@molo.co.za>:
> 
> > Hi everyone,
> >
> > I appologise if this has been asked already, but I would like to know the
> > following: Is it possible to have a Java class, e.g. Test in my Test.java,
> > to keep the value of its static variables between calls to the service.
> E.g.
> > I have a static variable temp, to which I just add string data and on each
> > call to getValue the value returned should be the value I passed to the
> > function concatenated to previous String values. So on the first call, if
> I
> > pass "Hello", I would get back "Initial, Hello" and on the second call, if
> I
> > pass "there", I would get back "Initial, Hello there", and so on. The
> object
> > must thus never go out of scope. I assumed that I needed to set the scope
> to
> > application, but that didn't work (I just got back the string "Initial,
> XXX"
> > where XXX is what I passed to getValue - thus the constructor was called
> on
> > each call). Here is my deploy.wsdd file:
> >
> > <deployment xmlns="http://xml.apache.org/axis/wsdd/"
> > xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
> >     <service name="Test" provider="java:RPC">
> >         <parameter name="scope"          value="application" />
> >         <parameter name="className"      value="Test" />
> >         <parameter name="allowedMethods" value="*" />
> >     </service>
> > </deployment>
> >
> > and here is my class:
> >
> > public class Test {
> >     static boolean bInitialised;
> >     static String  temp;
> >
> >     public Test() {
> >       bInitialised = false;
> >       temp = "";
> >     }
> >
> >     public static synchronized boolean isInitialised() {
> >         return bInitialised;
> >     }
> >
> >     public static synchronized void initialise() {
> >               temp = "Initial, ";
> >               bInitialised = true;
> >     }
> >
> >     public String getValue(String input) {
> >         if (!Test.isInitialised()) {
> >               Test.initialise();
> >         }
> >
> >         temp = temp + input;
> >         return temp;
> >     }
> > }
> >
> > and I have deployed the service with the following:
> >
> > java org.apache.axis.client.AdminClient deploy.wsdd
> >
> > Any help would be greatly appreciated.
> >
> > Kind regards,
> > J.W.F. Thirion (Dérik)
> > E-mail: derik@molo.co.za
> >
> >
> >
> 
> 
> 


-- 
Javier Gonzalez Nicolini

Mime
View raw message