using finalize() to persist objects

Discussion in 'Java' started by Will, Feb 7, 2005.

  1. Will

    Will Guest

    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
    Will, Feb 7, 2005
    #1
    1. Advertising

  2. Will

    Ryan Stewart Guest

    "Will" <> wrote in message
    news:...
    > 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/2.3/javadoc/javax/servlet/ServletContextListener.html
    Ryan Stewart, Feb 7, 2005
    #2
    1. Advertising

  3. Will

    Roland Guest

    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_/ /__/
    / \ /_/ / \
    Roland, Feb 7, 2005
    #3
  4. Will

    Roland Guest

    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_/ /__/
    / \ /_/ / \
    Roland, Feb 7, 2005
    #4
  5. Will

    Malte Guest

    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?
    Malte, Feb 7, 2005
    #5
  6. Will

    Ryan Stewart Guest

    "Malte" <forgetme@spam_here.nowhere.com> wrote in message
    news:cu7o1h$e7h$...
    > 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.
    Ryan Stewart, Feb 7, 2005
    #6
  7. Will

    GregSmith Guest

    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
    GregSmith, Feb 7, 2005
    #7
  8. 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
    John C. Bollinger, Feb 7, 2005
    #8
  9. If you read the specifications, you'll see that there's no guarantee
    that finalize() will ever be called.
    Darryl L. Pierce, Feb 7, 2005
    #9
  10. 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
    John C. Bollinger, Feb 7, 2005
    #10
  11. Will

    Ryan Stewart Guest

    "John C. Bollinger" <> wrote in message
    news:cu7vqm$jg8$...
    > 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).

    [...]
    > 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.
    >

    Holy crap. I wish usenet messages propogated faster. When I started reading this
    message, I thought I was reading the message I posted to this thread 2.5 hours
    earlier, they're so similar. I assume you hadn't seen mine when you posted this.
    Ryan Stewart, Feb 8, 2005
    #11
  12. Will

    Adam Maass Guest

    "Darryl L. Pierce" <> wrote:
    > If you read the specifications, you'll see that there's no guarantee
    > that finalize() will ever be called.
    >

    Well, you have to read them pretty darn closely to glean this bit of
    information. It is correct, though.

    -- Adam Maass
    Adam Maass, Feb 8, 2005
    #12
  13. Will

    Ryan Stewart Guest

    "Adam Maass" <> wrote in message
    news:...
    > "Darryl L. Pierce" <> wrote:
    >> If you read the specifications, you'll see that there's no guarantee
    >> that finalize() will ever be called.
    >>

    > Well, you have to read them pretty darn closely to glean this bit of
    > information. It is correct, though.
    >

    Actually it's perfectly reasonable and logical. If you're coming from a C++
    background, I can see how you would mistakenly take a finalizer to be similar to
    a destructor, but otherwise the way it works makes perfect sense.
    Ryan Stewart, Feb 8, 2005
    #13
  14. "Will" <> schrieb im Newsbeitrag
    news:...
    > 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


    Hello,

    You can try to add a 'shutdownHook':

    Runtime.getRuntime().addShutdownHook(new Thread("Startup
    Shutdown") {

    /**
    * Do operations on Exit.
    */
    public void run() {
    System.out.println("Storing data:");
    }

    });

    Best regards
    Thomas Buehler
    Thomas Bühler, Feb 8, 2005
    #14
  15. Will

    Will Guest

    Hi,

    Thanks to everyone for their suggestions.

    Finalize() is definitely a bad idea as I suspected.

    It sounds as though a ServletContextListener is going to be the best
    way forward for me here.

    Thanks again!

    Will
    Will, Feb 8, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z. f.

    throw exception in the finalize

    z. f., Jul 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    422
    avnrao
    Jul 5, 2004
  2. Noé R. Barranco

    Overriding finalize() method

    Noé R. Barranco, Aug 29, 2003, in forum: Java
    Replies:
    1
    Views:
    956
    Lee Fesperman
    Aug 30, 2003
  3. Joona I Palaste

    finalize() question

    Joona I Palaste, Dec 11, 2004, in forum: Java
    Replies:
    10
    Views:
    729
    Grant Wagner
    Dec 23, 2004
  4. andrey
    Replies:
    0
    Views:
    152
    andrey
    Dec 15, 2007
  5. Andrey Nikitin
    Replies:
    1
    Views:
    163
    Nobuyoshi Nakada
    Dec 16, 2007
Loading...

Share This Page