This will guarantee that you load a new class from disk every time:
ClassLoader loader = new MyCustomClassLoader();
Class c = loader.loadClass("MyWorkerClass");
Runnable r = (Runnable)c.newInstance();
r.run();
Note that you *cannot* refer to the MyWorkerClass within the code--you need
to use an interface or abstract base class that MyWorkerClass extends, or
this whole scheme fails miserably.
Each time a new ClassLoader is used, a class is checked to see if it has
been loaded into that ClassLoader *OR ANY OF ITS PARENTS*, which includes
the "root" ClassLoader. This means that MyWorkerClass can't be found on the
CLASSPATH, or it'll get picked up by the root loader and never released.
When the ClassLoader is gc'ed, any classes it loaded also get unloaded at
the same time.
This is a VERY terse (and very tired) explanation of how you can do this
code-on-the-fly thing.
<SHAMELESS PLUG>
I cover the same concept in my forthcoming book, "Server-Side Java", from
Manning Press. See Chapter Two, "ClassLoaders". Bill, if you can't wait the
couple of months or so for the book to hit the streets, email me privately.
</SHAMELESS PLUG>
Ted Neward
Patterns/C++/Java/CORBA/EJB/COM-DCOM spoken here
http://www.javageeks.com/~tneward
"I don't even speak for myself; my wife won't let me." --Me
-----Original Message-----
From: Steven J. Owens <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Monday, June 28, 1999 10:26 AM
Subject: Re: Can one servlet unload another?
>I lost track of the attributions (I *think* Bill O'Keefe was the original
>poster), but essentially:
>
>>>On Thu, 24 Jun 1999, James Duncan Davidson wrote:
>>>>> I looked through the servlet spec, and didn't see any way to do
>>>>> this, but I just want to know for sure. Bascially, I'm looking
>>>>> for a way to reload a servlet without shutting down the web
>>>>> server. I don't want to use the dynamic reloading facility,
>>>>> since I don't
>>
>> >I think te original poster wants to load on demand a new version of the
>> >servlet's class - a method like:
>> >
>> > ServletContext.markObsolete(Servlet myservlet);
>> >
>> >in order to inform the context to reload (through class loader)
>> >when a new request comes.
>> >This opposed with expensive dynamic reloading provided by some web
servers .
>> >
>> >There is no such method in JSDK.
>> >Cezar.
>>
>> Yes, this is basically what I wanted to do. Since there doesn't
>> appear to be such a method, do you have any idea how this could
>> be implemented? Or, it this basically something that would have
>> to be added to each servlet engine? Thanks,
>
> Depending on why you want to do this, couldn't you achieve much
>of the same effect by having the servlet instantiate and keep around a
>separate class to do all of the dirty work, then having some way of
>telling the servlet to throw away that class and reinstantiate it?
>
> I'm not sure how the library loading works in this case. I.e.
>given the following series of events:
>
> myservlet.class
> dirtywork.class <-- version 1
>
> start up servletengine
> servletengine loads in myservlet.class
> servletengine loads in dirtywork.class
> browser requests myservlet
> servletengine invokes myservlet.init
> myservlet.init calls myservlet.setupdirtywork
> mysevlet.setupdirtywork sets up dirtywork object
>
> overwrite dirtwork.class with new version
>
> poke myservlet.setupdirtywork somehow to get it to:
> throw away old dirtywork object
> reinstantiate dirtywork
>
> Does the servlet engine reload the class from disk or does it go
>with some cached version of dirtywork.class that's already in memory?
>
>
>Steven J. Owens
>[EMAIL PROTECTED]
>[EMAIL PROTECTED]
>
>___________________________________________________________________________
>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
___________________________________________________________________________
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