Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > using finalize() to persist objects

Reply
Thread Tools

using finalize() to persist objects

 
 
Will
Guest
Posts: n/a
 
      02-07-2005
Hi,

I have a webapp which uses an in-memory queue to hold requests for
processing.

I'm trying to come up with a way to persist the contents of the queue
to disk when the webapp is gracefully shutdown.

I know that the JVM can call the finalize() method on an object just
before garbage collection, however, when I put some serialization code
into the finalize() method of the queue, it never seems to get called.

To me, using finalize() in this fashion seems quite clunky anyway.

Would anybody be able to suggest a better way of doing this? I just
need to ensure that the queue is persisted to disk when the app is
shutdown.

Many thanks in advance,

Will
 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      02-07-2005
"Will" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi,
>
> I have a webapp which uses an in-memory queue to hold requests for
> processing.
>
> I'm trying to come up with a way to persist the contents of the queue
> to disk when the webapp is gracefully shutdown.
>
> I know that the JVM can call the finalize() method on an object just
> before garbage collection, however, when I put some serialization code
> into the finalize() method of the queue, it never seems to get called.
>
> To me, using finalize() in this fashion seems quite clunky anyway.
>
> Would anybody be able to suggest a better way of doing this? I just
> need to ensure that the queue is persisted to disk when the app is
> shutdown.
>

There is no guarantee that any object's finalizer will ever be called since
there's no guarantee that any object will ever be garbage collected. A finalizer
is never the appropriate place for business logic of any kind, even if it's just
persistence. You're looking for a ServletContextListener, most likely:
http://java.sun.com/products/servlet...tListener.html


 
Reply With Quote
 
 
 
 
Roland
Guest
Posts: n/a
 
      02-07-2005
On 7-2-2005 13:24, Will wrote:

> Hi,
>
> I have a webapp which uses an in-memory queue to hold requests for
> processing.
>
> I'm trying to come up with a way to persist the contents of the queue
> to disk when the webapp is gracefully shutdown.
>
> I know that the JVM can call the finalize() method on an object just
> before garbage collection, however, when I put some serialization code
> into the finalize() method of the queue, it never seems to get called.
>
> To me, using finalize() in this fashion seems quite clunky anyway.
>
> Would anybody be able to suggest a better way of doing this? I just
> need to ensure that the queue is persisted to disk when the app is
> shutdown.
>
> Many thanks in advance,
>
> Will

You could add a shutdown hook:
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)>,
but read it's Javadoc carefully: a shutdown hook will only run when the
program exits normally, or when the virtual machine is terminated in
response to a user interrupt (CTRL-C).
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      02-07-2005
On 7-2-2005 13:38, Roland wrote:

> On 7-2-2005 13:24, Will wrote:
>
>> Hi,
>>
>> I have a webapp which uses an in-memory queue to hold requests for
>> processing.
>>
>> I'm trying to come up with a way to persist the contents of the queue
>> to disk when the webapp is gracefully shutdown.
>>
>> I know that the JVM can call the finalize() method on an object just
>> before garbage collection, however, when I put some serialization code
>> into the finalize() method of the queue, it never seems to get called.
>>
>> To me, using finalize() in this fashion seems quite clunky anyway.
>>
>> Would anybody be able to suggest a better way of doing this? I just
>> need to ensure that the queue is persisted to disk when the app is
>> shutdown.
>>
>> Many thanks in advance,
>>
>> Will

>
> You could add a shutdown hook:
> <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)>,
> but read it's Javadoc carefully: a shutdown hook will only run when the
> program exits normally, or when the virtual machine is terminated in
> response to a user interrupt (CTRL-C).

OK, i didn't read carefully enough :-[ .
You were talking about a web app. A shutdown hook is not gonna help you
there.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
Malte
Guest
Posts: n/a
 
      02-07-2005
Will wrote:
> Hi,
>
> I have a webapp which uses an in-memory queue to hold requests for
> processing.
>
> I'm trying to come up with a way to persist the contents of the queue
> to disk when the webapp is gracefully shutdown.
>
> I know that the JVM can call the finalize() method on an object just
> before garbage collection, however, when I put some serialization code
> into the finalize() method of the queue, it never seems to get called.
>
> To me, using finalize() in this fashion seems quite clunky anyway.
>
> Would anybody be able to suggest a better way of doing this? I just
> need to ensure that the queue is persisted to disk when the app is
> shutdown.
>
> Many thanks in advance,
>
> Will

Just guessing here, but does not a Servlet/JSP have a destroy() method
you could use?
 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      02-07-2005
"Malte" <forgetme@spam_here.nowhere.com> wrote in message
news:cu7o1h$e7h$(E-Mail Removed)...
> Will wrote:
>> Hi,
>>
>> I have a webapp which uses an in-memory queue to hold requests for
>> processing.
>>
>> I'm trying to come up with a way to persist the contents of the queue
>> to disk when the webapp is gracefully shutdown.
>>
>> I know that the JVM can call the finalize() method on an object just
>> before garbage collection, however, when I put some serialization code
>> into the finalize() method of the queue, it never seems to get called.
>>
>> To me, using finalize() in this fashion seems quite clunky anyway.
>>
>> Would anybody be able to suggest a better way of doing this? I just
>> need to ensure that the queue is persisted to disk when the app is
>> shutdown.
>>
>> Many thanks in advance,
>>
>> Will

> Just guessing here, but does not a Servlet/JSP have a destroy() method you
> could use?


Yes, but that method may be called at any time, not just when the app is shut
down.


 
Reply With Quote
 
GregSmith
Guest
Posts: n/a
 
      02-07-2005
The Java Servler Whitepaper
(http://java.sun.com/products/servlet/whitepaper.html) Says...

"<i>Requests are processed until the servlet is explicitly shut down by
the web server, by calling the destroy method. The servlet's class may
then become eligible for garbage collection. </i>"

So, you should be able to serialize in the destroy() method.

Greg

Will wrote:
> Hi,
>
> I have a webapp which uses an in-memory queue to hold requests for
> processing.
>
> I'm trying to come up with a way to persist the contents of the queue
> to disk when the webapp is gracefully shutdown.
>
> I know that the JVM can call the finalize() method on an object just
> before garbage collection, however, when I put some serialization

code
> into the finalize() method of the queue, it never seems to get

called.
>
> To me, using finalize() in this fashion seems quite clunky anyway.
>
> Would anybody be able to suggest a better way of doing this? I just
> need to ensure that the queue is persisted to disk when the app is
> shutdown.
>
> Many thanks in advance,
>
> Will


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      02-07-2005
Will wrote:

> Hi,
>
> I have a webapp which uses an in-memory queue to hold requests for
> processing.
>
> I'm trying to come up with a way to persist the contents of the queue
> to disk when the webapp is gracefully shutdown.
>
> I know that the JVM can call the finalize() method on an object just
> before garbage collection, however, when I put some serialization code
> into the finalize() method of the queue, it never seems to get called.


No object's finalize() method is ever guaranteed to be called, because
no object is guaranteed to ever be garbage collected. In particular,
objects are unlikely to be garbage collected at application shutdown
(where in your case, the relevant "application" is the servlet container).

> To me, using finalize() in this fashion seems quite clunky anyway.


It is. Plus, overriding Object.finalize() (even trivially) on any class
makes the GC work much harder to collect instances of that class.

> Would anybody be able to suggest a better way of doing this? I just
> need to ensure that the queue is persisted to disk when the app is
> shutdown.


Create an appropriate ServletContextListener implementation and register
it in your web.xml. Yours is squarely among the kinds of problems that
ServletContextListeners are intended to solve.

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Darryl L. Pierce
Guest
Posts: n/a
 
      02-07-2005
If you read the specifications, you'll see that there's no guarantee
that finalize() will ever be called.

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      02-07-2005
GregSmith wrote:

> The Java Servler Whitepaper
> (http://java.sun.com/products/servlet/whitepaper.html) Says...
>
> "<i>Requests are processed until the servlet is explicitly shut down by
> the web server, by calling the destroy method. The servlet's class may
> then become eligible for garbage collection. </i>"
>
> So, you should be able to serialize in the destroy() method.


No, he can't. The servlet container is free at any time to destroy() a
servlet instance and replace it with a new instance of the same servlet
class. It could even delay creating the new instance until a request is
received that maps to that servlet. In the case of a servlet that
implements SingleThreadModel, the container is even free to maintain
multiple instances concurrently, in which case it would be completely
incorrect to serialize in the servlet's destroy() method.

The servlet API defines a means for a web application to receive
notification when it is being shut down (the ServletContextListener; see
my other response). If shutdown notification is what is desired then
that API should be used, not some hack that may (or may not) be found to
work under the initial development conditions.


--
John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the most cost effective way to persist page level objects in server Ryan Liu ASP .Net 4 01-20-2009 02:56 AM
Session objects do not persist on different web pages... why that happens ? Newcomsas ASP .Net 1 11-04-2008 01:41 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Re: Persist Python objects to a MySQL database? dique Python 0 06-24-2003 10:20 AM
Re: Persist Python objects to a MySQL database? Miki Tebeka Python 0 06-24-2003 05:28 AM



Advertisments