Milt Epstein wrote:
> [...]
> Some later messages suggest you've got this straightened out, but I
> will add a few comments just in case.
>
> These questions relate to a different issue than the previous ones.
> The previous ones related to threading. These have to do with
> persistence and the servlet life cycle. Basically, you need to be
> aware of the servlet life cycle, that servlets can be taken up and
> down multiple times, which will result in calls to the servlet init()
> and destroy() methods. In those methods, you can take care of
> persisting and restoring any necessary data. For example, with your
> counter, you can save it to a file in destroy(), and then restore it
> from that file in init() (of course, you will have to consider all the
> issues that have been discussed when writing those methods, like in
> what situations multiple instances of the servlet will exist). As to
> threads, if your servlet started up some threads while it was running,
> those threads may need to be stopped in destroy().
> [...]
Thanks for your email :-) and the folowing is just my guessing, please correct
me :-)
0 I suppose the following is the code of MyServlet class:
public class MyServlet extends HttpServlet
{
private static Object sync =new Object();
private static int counter =0;
...
//service/doGet/doPost {
...
synchronized(sync){
counter++;
sync.notifyAll();
}
...
}
1
I suppose I deploy MyServlet class in a non-Distributed Servlet container
which
implements *Servlet spec. v2.2 or v2.3*, and in my web.xml, I define two
servlet definitions: MyServletA and MyServletB
2
then I start this Servlet container, and begin to access MyServlet class,
after
a period of time-- for example , several minutes, I suppose I have already
reached the following status:
# MyServlet has already been loaded by ClassLoaderONLY -- it is *the only one
ClassLoader* uesd by *the only one JVM* in my Servlet container to
load *all custom Servlet classes*. Let us suppose I call it
*loaded_MyServlet
class* --> because I think it is different from that MyServlet class
which
is still in persistent media(for example, in hard disk)
# two instances of MyServlet class -->instanceA and instanceB have already
been made, because now I have two Servlet definitions.
# *the init() of instanceA* and *the init() of instanceB* have already both
been
invoked only one time each .
# several threads from instanceA have already been made, run and
*destroyed*
and
several threads from instanceB also have already been made, run and
*destroyed*
3 the following is my guessing about how to *save and read* counter field :
[a] if only *the destroy() of instanceA* or *the destroy() of instanceB*
has/have been invoked, in theory now I don't need to *persist counter
field*,
this is because the *loaded_MyServlet class* is still there.
[b] if both *the destroy() of instanceA* and *the destroy() of instanceB*
has/have been invoked, I don't know if I need to *persist counter
field*,
this is because I don't know if the *loaded_MyServlet class* is still
there now.
[c] if *loaded_MyServlet class* has been unloaded , now
I must *persist counter field*, otherwise I will lost the value
of counter field.
[d] I think I can put my code for *persisting counter field* in the
following two places :
* in Servlet.destroy(). But I don't know when Servlet.destroy()
will be invoked --> perhaps in 3-[a] and 3-[b] and 3-[c].
* in Object.finalize(), I think finalize() will be invoked in
3-[b] and 3-[c].
but now it means perhaps I will *save* counter field N times , and
N-1 of them is not necessary.
[e] my question is : where can I load this value from *persistent state*
and assign it to counter field?
# I think I can not put it in Servlet.init() , because now there are
two
instances-- if between *the destroy() of old instanceA is invoked--
I save counter to disk* and *the init() of new instanceA is
invoked* ,
several threads which are from instanceB are working, then counter
field will be wrong.
# I think I can not put it in *static/non-static initialization block*
of
MyServlet class with the similar reason.
# i.e. :
[#] I can save counter many times,
[#] I also can read it many times during each time before
instanceA and instanceB are made , but after instanceA
and instanceB have already been made and begin to
work, perhaps I can not read the value again.
# so where can I read the value of counter from its
persistent state if I have more than one Servlet definition?
# and I don't know if I will have a problem when I initialize
counter if I have more than one Servlet definition:
private static int counter =0;
or
private static int counter;
because I don't know if the following is possible:
instanceA has been made and begin to work,
so after several minutes, counter is already
8, but now instanceB has been made and
begin to work, now counter return to 0.
I guess now it is a little bit similar with EJB. Perhaps all the above is wrong
or we don't need to use Servlet in this way -- we can use other Java technolagy.
Please give me direction !
Thanks in advance!
Bo
Nov.05, 2000
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html