Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Have a query regarding application shut down during context initialization

 
Indrayani Godbole
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a query , I have a created a web applicaiton and have put a listener which implements ServletContextListener which gets executed on application startup.
I am checking some flag value inside the listeners' contextInitialized method.
if the flag value is false, then i want to shut down the application that is i should no longer be able to access the application's URL.

My query is : is it possible to do so, like shut down the application during the context initialization? I searched a lot but could not get any solution.
Can not use system.exit(0) since it will stop the application container itself.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try throwing a javax.servlet.UnavailableException

Bill
 
Indrayani Godbole
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:I would try throwing a javax.servlet.UnavailableException

Bill


but if i am throwing the exception then i have to catch it somewhere and all these things i am doing during context initialization only, then how it will stop the already started application ? somehow i am not able to understand how should i throw the exception in order to stop the application, can you give me an example for the same?
 
Tim Holloway
Bartender
Posts: 18419
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you throw an exception and your application doesn't catch it, then the container (server) will catch it and deal with it.

However, a ServletUnavailableException wouldn't really shut down the entire webapp. There's nothing within the J2EE webapp API set that I know of that will do that, in fact.

There are some alternatives, however. One of them is to have your listener grab one of the control APIs and issue a stop command. I think JSR-177 is one of them. For Tomcat, there's also a set of ReST-style commands that you can invoke via the HttpConnection interface.

An alternative, however, is to keep the servlet up but effectively offline. You can do this by constructing a ServletFilter that looks at the "on/off" indicator, and if the app is turned "off", returns a "404 Page Not Found" response. Which is what webapp servers would do for an invalid URL. Including URLs for a webapp that had been stopped.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indrayani Godbole wrote:
William Brogden wrote:I would try throwing a javax.servlet.UnavailableException

Bill


but if i am throwing the exception then i have to catch it somewhere and all these things i am doing during context initialization only, then how it will stop the already started application ? somehow i am not able to understand how should i throw the exception in order to stop the application, can you give me an example for the same?


No, the container will catch it and mark that URL as unavailable - thats why this exception exists, to inform the container. As I recall that means future requests will get a 404 error. The underlying filter or servlet will not see any more requests.

Bill
 
Tim Holloway
Bartender
Posts: 18419
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:
Indrayani Godbole wrote:
William Brogden wrote:I would try throwing a javax.servlet.UnavailableException

Bill


but if i am throwing the exception then i have to catch it somewhere and all these things i am doing during context initialization only, then how it will stop the already started application ? somehow i am not able to understand how should i throw the exception in order to stop the application, can you give me an example for the same?


No, the container will catch it and mark that URL as unavailable - thats why this exception exists, to inform the container. As I recall that means future requests will get a 404 error. The underlying filter or servlet will not see any more requests.

Bill


I had to RTFM on that one, since it sounded like something implementation-dependent. But the javadocs do plainly state that the servlet can be marked as unavailable either permanently, or for some definite period of time. I did note however, that if you change your mind later, there didn't seem to be a reference to any officially-blessed mechanism for bringing the servlet back from the dead short of restarting the webapp server.

Rather than marking "that URL" unavailable, one can reasonably expect that all URL patterns mapped to that servlet will come up unavailable. Unless, maybe they manage to map some other servlet which would ordinarily be hidden by the servlet that gets exposed when the original servlet is taken offline.

Overall, however, I'd go with the servlet Filter approach, since the net effect would be pretty much the same, and that way all servlets, JSPs, and other resources would be switchable from a single control point.

I said servlet listener, but I've been dealing with too many listeners lately. It's a servlet Filter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!