• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tomcat Servlet Context Listener reflection error

 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So have a class that inherits ServletContextListener. I overrode the context methods, and they call start and stop methods in my class, which start or stop the thread. I registered the class as a listener in web.xml and everything works fine.

Now, however, I'm adding a properties file in order to configure some things without having to recompile. So I created a private constructor which initializes the properties file. Now I'm getting a reflection error saying that it can't access private methods. If I change the constructor to public it works fine.

Any suggestions on how to get this to work? The class is set up as a singleton since I only want one instance of the thread running at the same time. I forgot to make the constructor private previously.

TIA.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35704
407
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bai,
You can have a singleton that handles everything you want to have only one copy. They key is it has to be a singleton of another class. Just have your public constructor call OtherClass.getInstance() to get a copy. That way your singleton is a singleton and your listener is not.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just don't worry about it and let the container handle things. What problem are you really trying to solve?
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Bai,
You can have a singleton that handles everything you want to have only one copy. They key is it has to be a singleton of another class. Just have your public constructor call OtherClass.getInstance() to get a copy. That way your singleton is a singleton and your listener is not.


That would work. Just seems kind of hokey.

Bear Bibeault wrote:Or just don't worry about it and let the container handle things. What problem are you really trying to solve?


What do you mean, let the container handle things?

Basically, I just need a background process to run while tomcat is running. Right now I have the following setup.



And then the start and stop methods start and stop the thread that runs my process. Does that make sense? Or do you have a suggestion for a better way?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!