You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Ivano Luberti <lu...@archicoop.it.INVALID> on 2024/12/27 16:48:47 UTC

contextDestoryed events order of calling

Hi everyone

Using Tomcat 9.5

I have two questions:

2) The docs 
(https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/ServletContextListener.html) 
state that

> All servlets and filters have been destroyed before any 
> ServletContextListeners are notified of context destruction.
>
Now in my code I have a servlet class (MyServletClass) that implements 
ServletContextListener and  contextDestroyed

and another not servlet class  (say MyClass) that implements those as well.

Using breakpoints I see that MyClass.contextDestroyed called BEFORE 
MyServletClass.contextDestroyed

and for what I understand it should be the other way around.

2)  I cannot find documentation on this matter.

a) if I have more than one class (neither servlet) implementing 
ServletContextListener interface in which order the contextDestroyed 
method is called?

b) if I have more than one  servlet class implementing 
ServletContextListener interface in which order the contextDestroyed 
method is called?


TIA



-- 

Archimede Informatica tratta i dati personali in conformità a quanto
stabilito dal Regolamento UE n. 2016/679 (GDPR) e dal D. Lgs. 30 giugno 
2003 n. 196
per come modificato dal D.Lgs. 10 agosto 2018 n. 101.
Informativa completa 
<http://www.archicoop.it/fileadmin/pdf/InformativaTrattamentoDatiPersonali.pdf>

Il contenuto di questo messaggio e dei suoi eventuali allegati è 
riservato. Nel caso in cui Lei non sia il destinatario, La preghiamo di 
contattare telefonicamente o via e-mail il mittente ai recapiti sopra 
indicati e di cancellare il messaggio e gli eventuali allegati dal Suo 
sistema senza farne copia o diffonderli. Le opinioni espresse sono 
quelle dell'autore e non rappresentano necessariamente quelle della Società.
This message and any attachment are confidential.If you are not the 
intended recipient, please telephone or email the sender and delete this 
message and any attachment from your system. If you are not the intended 
recipient you must not copy this message or attachment or disclose the 
contents to any other person. Any opinions presented are solely those of 
the author and do not necessarily represent those of the Company.

dott. Ivano Mario Luberti

Archimede Informatica società cooperativa a r. l.
Via Gereschi 36, 56127 Pisa

tel.: +39 050/580959

web: www.archicoop.it
linkedin: www.linkedin.com/in/ivanoluberti
facebook: www.facebook.com/archimedeinformaticapisa/

Re: contextDestoryed events order of calling

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Ivano,

On 12/27/24 11:48 AM, Ivano Luberti wrote:
> Hi everyone
> 
> Using Tomcat 9.5

Doesn't exist.

> I have two questions:
> 
> 2) The docs (https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/ 
> servlet/ServletContextListener.html) state that
> 
>> All servlets and filters have been destroyed before any 
>> ServletContextListeners are notified of context destruction.
>>
> Now in my code I have a servlet class (MyServletClass) that implements 
> ServletContextListener and  contextDestroyed
> 
> and another not servlet class  (say MyClass) that implements those as well.

You have a Servlet that implements ServletContextListener, and also 
includes a contextDestroyed(ServletContextEvent) method? Okay.

> Using breakpoints I see that MyClass.contextDestroyed called BEFORE 
> MyServletClass.contextDestroyed
> 
> and for what I understand it should be the other way around.

For what reason? If they are both implementing ServletContextListener, 
they should both be called. But there is no guarantee of the order in 
which they are called.

The fact that one of them is a Servlet is not relevant.

> 2)  I cannot find documentation on this matter.

The Servlet Specification is what you are looking for. The one relevant 
for all supported Tomcat 9 versions is Servlet 4.0 which can be found here:
https://javaee.github.io/servlet-spec/downloads/servlet-4.0/servlet-4_0_FINAL.pdf

> a) if I have more than one class (neither servlet) implementing 
> ServletContextListener interface in which order the contextDestroyed 
> method is called?

How are you registering your event listeners? If you register them in 
WEB-INF/web.xml then they will be invoked in the order in which they 
appear in web.xml. Initialization notifications will be sent in top-down 
order and destruction notifications will be sent in bottom-up order.

If you are using @Annotations, then I don't believe an order can be 
specified because there is no guaranteed order of .jar-file processing 
and/or .class-file processing when searching for annotations.

> b) if I have more than one  servlet class implementing 
> ServletContextListener interface in which order the contextDestroyed 
> method is called?

There is no Servlet.contextDestroyed method defined, so it will never be 
called by the container (Tomcat). If you happen to have a class which 
implements both interfaces, then the container will invoke the right 
methods at the right times, but don't think that because you have a 
Servlet that is also a ServletContextListener that Tomcat will call 
contextDestroyed on it earlier than any other ServletContextLsistenrt 
just because it's a Servlet.

 From Tomcat's perspective, it's just a ServletContextListener like any 
other. I would bet that you get two separate instances of that class in 
memory, as well: one to serve as the Servlet and another to serve as the 
ServletContextListener. Forcing them to be the same object might be 
difficult to accomplish.

-chris


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org