Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletContextListener calling method from another class  RSS feed

 
phillip nash
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

I've got a ServletContextListener set up and working correctly. When the servlet shuts down I need it to call a method within another class to shut down a thread pool.
Is it possible for a ServletContextListener to do this?

Thanks
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By servlet shuts down do you mean container shutdown? If so call the method in the
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phillip nash wrote:
When the servlet shuts down I need it to call a method within another class to shut down a thread pool.
Thanks


Do you mean when the context shuts down? ServletContextListener has a contextDestroyed() method that will notify the listener that the context is about to be destroyed, but by the time that notification is sent, all servlets and filters have already been destroyed. Without more information, I'm not sure if that's what you are looking for.
 
phillip nash
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:By servlet shuts down do you mean container shutdown? If so call the method in the


Basically when the servlet shuts down and executes contextDestroyed(). Within that method I'd like to refer to an object that was created when my servlet first started up.
I tried to implement ServletContextListener within my servlet class, but I was getting errors (I think it's because I'm creating a new servlet object).
 
phillip nash
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, I could move some of my objects into the class which implements ServletContextListener, but this means in my servlet code I'll need to refer back to them in order to use their
methods. Doing it this way I can easily shut down my thread pool. I'm not too sure if this is even possible.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phillip nash wrote:Basically when the servlet shuts down and executes contextDestroyed().

As has already been pointed out, servlets do not "shut down". Are you asking about when the web app is being shut down? Until you can accurately define what you are trying to do, help will be difficult to offer.
 
phillip nash
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
phillip nash wrote:Basically when the servlet shuts down and executes contextDestroyed().

As has already been pointed out, servlets do not "shut down". Are you asking about when the web app is being shut down? Until you can accurately define what you are trying to do, help will be difficult to offer.


Yes. Sorry. When I shutdown GlassFish I need to call a method that's in another class to shutdown a thread pool.
I don't know if I can refer to the class within contextDestroyed() that contains the method I need to call. I currently looking at getServletContext().getAttribute() to see if I can refer to the class
which contains the code for shutting down the thread pool.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to give us more information about the purpose and nature of this class and what role it plays in the web app.

Just like any other Java code, you can call methods in any object that you have a reference to. But without more contact we'll have a hard time offering advice.
 
phillip nash
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class in which I want to refer to deals with launching 4 threads to communicate with these sensor boxes I'm playing around with. If I restart my webservice (not GlassFish) I get various errors relating to my thread pool. So, in order to sort this out, when I restart my webservice I need a way to shutdown the thread pool so it can restart again when I launch the service.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still having a little trouble understanding exactly what you are doing, but how about creating the object that contains the thread pool shutdown method within the contextInitialized() method of the listener and then call the method within contextDestroyed()?

Maybe if you post the code of the method in question and tell us how this object is currently being created it might clear things up a bit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!