Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

order of listeners

 
Yogesh Hingmire
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that When an application shuts down listeners are called in order inversed of their apperance in the deployment descriptor.

Is this true. I am curious about "inversed" why is it so ?
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On application shutdown, listeners are notified in REVERSE order to their declarations with notifications to session listeners preceeding notifications to context listeners.
Reverse order because this allows for guaranteed ordering relationships among startup objects.
[ November 03, 2006: Message edited by: Max Fernandes ]
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guaranteed ordering relationships among startup objects


What does this mean ?
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that com.acme.MyConnectionManager and com.acme.MyLoggingModule both implement javax.servlet.ServletContextListener, and
that com.acme.MyLoggingModule additionally implements
javax.servlet.HttpSessionListener. Also, the Developer wants
com.acme.MyConnectionManager to be notified of servlet context lifecycle events before com.acme.MyLoggingModule. Here is the deployment descriptor for this application :



This is from the servlet spec pages 82,83. Can anyone tell listener notificaton sequesnce on application shut down ? there are two rules -

1. Session listeners must be notified before context listeners
so MyLoggingModule will be notified first. This also satisfies the rule
2. below
2. Listenrs are notified in the reverse order of their declaration

but if we change the declaration order as

<listener>
<listener-class>com.acme.MyLoggingModule</listener-class>
</listener>
<listener>
<listener-class>com.acme.MyConnectionManager</listenerclass>
</listener>

Then as per Rule 1, MyLoggingModule will be notified first, but this violates rule 2 - notification happens in the reverse order of declaration ( and this reverse order is MyConnectionManager getting notifiied first and then MyLoggingModule ).

Hope you guys get what i am asking
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope i get replies.
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niranjan Deshpande:
Suppose that com.acme.MyConnectionManager and com.acme.MyLoggingModule both implement javax.servlet.ServletContextListener, and
that com.acme.MyLoggingModule additionally implements
javax.servlet.HttpSessionListener. Also, the Developer wants
com.acme.MyConnectionManager to be notified of servlet context lifecycle events before com.acme.MyLoggingModule. Here is the deployment descriptor for this application :



This is from the servlet spec pages 82,83. Can anyone tell listener notificaton sequesnce on application shut down ? there are two rules -

1. Session listeners must be notified before context listeners
so MyLoggingModule will be notified first. This also satisfies the rule
2. below
2. Listenrs are notified in the reverse order of their declaration

but if we change the declaration order as

<listener>
<listener-class>com.acme.MyLoggingModule</listener-class>
</listener>
<listener>
<listener-class>com.acme.MyConnectionManager</listenerclass>
</listener>

Then as per Rule 1, MyLoggingModule will be notified first, but this violates rule 2 - notification happens in the reverse order of declaration ( and this reverse order is MyConnectionManager getting notifiied first and then MyLoggingModule ).

Hope you guys get what i am asking


Niranjan,

When the application shuts down , MyLoggingModule.sessionDestroyed() will be called first for every destroyed , followed by MyConnectionManager.contextDestroyed() then MyLoggingModule.contextDestroyed() .

I think you are smart enough that you must have known the answer , however the excitement of having found an anamoly in the specs made you overlook the obvious

HTH

[ November 05, 2006: Message edited by: Vivek Pandey ]
[ November 05, 2006: Message edited by: Vivek Pandey ]
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so Rule 2 is not true ? is it ???
i am asking serious questions,it has nothing to do with SMARtness
and how come the spec has anamoly ? i simply asked -

if we delared in this order -

MyConnectionManager
MyLoggingModule

then MyConnectionManager will be notified first ( and rule 2 is violeted )
is rule 2 concrete or a anamoly in spec ,

please explain
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh.. oh... now i get it ...

there are two rules, and both dont apply the same time, when notification order is being determined.

i.e. either of them will be used and not both..
so if declaration order is

MyConnectionManager
MyLoggingModule

MyLoggingModule is notified first ( as per rule 1 - reverse order ) and by chance this time its implementing HttpSessionListener

But in the scenario

MyLoggingModule
MyConnectionManager

MyLoggingModule will be notified first ( as per rule 2 - as it is a session listerner )

is thi OK ?
 
Yogesh Hingmire
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Niranjan has a valid point.Could any one please clear the "ordered" concept with regards to listeners.

Also, when the app gets deployed, the listeners are registered in the order in which they are written in the web.xml.

What is not clear is when it goes down,
1) The reverse of the above order is executed OR
2) The container notifies the request listeners (request destroyed), then session listeners (session listeners) and then context listeners.
and while doing this it starts notifying respective listeners in the reverse order.

I know this could be a dumb question, but have to get it clear.

Thank You
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patience Is a Virtue
(it's not the first time)
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Vivek is right.

Here the scenario is:

1.Context Listeners:
i. MyConnectionManager
ii. MyLoggingModule
2.Session Listener
i. MyLoggingModule


Container will first notify Session listeners in reverse order, so MyLoggingModule.sessionDestroyed will be the first method to be called.

Then container will notify Context listeners in reverse order, so MyLoggingModule.contextDestroyed and MyConnectionManager will be called respectively.

Here if MyLogging module is being notified first, it only mean that MyLoggingModule was notified as a session listener not as a context listener. As a context listener it will be notified again. which means both rule 1 and rule 2 were followed.

Hope I am right?
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction

for this scenario

MyLoggingModule
MyConnectionManager

Then container will notify Context listeners in reverse order, so MyLoggingModule.contextDestroyed and MyConnectionManager will be called respectively.


would be


Then container will notify Context listeners in reverse order, so MyConnectionManager.contextDestroyed and MyLoggingModule.contextDestroyed will be called respectively.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic