This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes JSP and the fly likes How to make an object strongly reachable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to make an object strongly reachable?" Watch "How to make an object strongly reachable?" New topic

How to make an object strongly reachable?

William Shubert

Joined: Mar 30, 2004
Posts: 2
I've got a JSP problem (Tomcat 5.0.19). I use a Timer object to kick off occasional actions in my JSP engine - flushing cached information, pinging my RMI peers, etc. My timers sometimes complain about "Timer already stopped" when I try to schedule a new event. A little digging shows that this can happen when a the JVM tries to garbage collect a timer. This is absurd, my timer object is in the application scope, it should never even begin to be garbage collected!
How do I make sure that the timer is always strongly reachable? I've tried using daemon and non-daemon timers, tried adding static references to the timer - nothing seems to work.
Here's the code that makes the timer:
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63547

You are correct in that an object in application scope should never be garbage collected. The map implementing the attribute list in the servlet context object will retain a reference to the object. There must be some other subtle factor at play here.

[Asking smart questions] [About Bear] [Books by Bear]
William Shubert

Joined: Mar 30, 2004
Posts: 2
Just to be 100% clear - actually, the timers are not being garbage collected, but their finalizer is being called. It is the calling of the finalizer that cancels the timers even though I am still using it.
So the real question is, why is the finalizer of an application scoped object being called? And how do I stop this from happening?
For now I have a workaround. I have created a class that extends timers, and uses a delegate "real" timer. When it gets an IllegalStateException thrown from the "real" timer, then I just create a new one and start giving all my scheduled tasks to that. It's an ugly solution, so I'd like something better, but for now it works.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Try using Quartz instead of writing your own:

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
subject: How to make an object strongly reachable?
jQuery in Action, 3rd edition