Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of calling destroy() method on servlets/filters

I have a Java web app.

I can specify the order of calling init() method on servlets by the parameter in web.xml:

<load-on-startup>1</load-on-startup>

But, how can I specify the order of calling destroy() methods?

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 destroy() method.

like image 436
bugs_ Avatar asked Jul 29 '11 14:07

bugs_


People also ask

When destroy () method of a filter is called?

Q 23 - When destroy method of filter gets called? A - The destroy method is called only once at the end of the life cycle of a filter.

When the destroy () method will be called?

Q 11 - When destroy() method of servlet gets called? A - The destroy() method is called only once at the end of the life cycle of a servlet.

What will be the order of filter execution when the servlet is invoked?

First, it receives ServletRequest and ServletResponse object pairs for incoming requests. Depending on the counter, it then either passes them down the chain by invoking the next doFilter() method on the FilterChain object, or rejects them by generating its own response.

What is the sequence of method execution in filter init doFilter destroy?

Every filter must implement the three methods in the Filter interface: init(), doFilter(), and destroy(). When the Container decides to instantiate a filter, the init() method is your chance to do any set-up tasks before the filter is called.


1 Answers

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.

Recommendation:

Use a ServletContextListener. It is guaranteed to be initialized and destroyed only once.

like image 163
home Avatar answered Oct 15 '22 11:10

home