Hi I am creating a JSP project, and one of my JavaBeans uses a thread that executes every hour to keep track of a Call Center's Orders for Auto Escalation. I want to be able to keep track of this thread on the server. Each time someone logs in to the site, the jsp should check that the Thread is running, if not a new instance should be created and the Thread started. What i currently have, is, a session is created with an instance of the Bean, this session is used to check that a new Instance of the Thread is not created each time the user visits the login page. However, if the session is terminated, a new Instance is created and at the end of the day i'm left with shit loads of auto escalating happening!! Is there a way of making sure that only one instance of the Thread is running at any one time. The thread should run constantly on the server and only terminate when an Exception is thrown or the sever shut down.
As much as we caution against the dangers of Singleton down in the OO, UML, etc forum, this sounds like a good application. A singleton class does two things: assures there is only one instance (or a tightly controlled number) and provides access to that instance. An easy implementation is:
As soon as any class references MyClass the static instance is created. And anybody can get access through the getInstance method.
DO visit the OO, UML, etc. forum and scan for discussions about the dangers of singleton. It's not something to be done too lightly. Cheers!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi