I have a Java web app.
I can specify the order of calling
init() method on servlets by the parameter in
But, how can I specify the order of calling
Actually, what I need to do is just shutdown log4j in the end. But in advance I want to know if there is some rules for calling
I had a look into the Servlet 3.0 spec. It does not define any rules on the order in which the
destroy methods have to be called. Thus, it is not specified and you should not rely on any vendor specific behaviour. A second reason for not releasing resources shared across Servlets is, that a given Servlet can be destroyed at any time – if the container chooses to do so. See section 2.3.4 of Servlet 3.0 specification:
2.3.4 End of Service
The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between.
When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down
Let’s say you have 3 Servlets – A, B, and C. If A and B rely on resources managed by C, it may happen that the container decides to temporarily disable C by calling its
destroy method. So A and B won’t have access to those resources any more. I must admit, I’ve never seen this behaviour in reality.
Use a ServletContextListener. It is guaranteed to be initialized and destroyed only once.