aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes order of listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "order of listeners" Watch "order of listeners" New topic
Author

order of listeners

Yogesh Hingmire
Ranch Hand

Joined: Dec 06, 2005
Posts: 61
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

Joined: Jun 28, 2003
Posts: 548

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 ]

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
guaranteed ordering relationships among startup objects


What does this mean ?


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Oct 16, 2005
Posts: 1277
hope i get replies.
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245
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 ]

Vikrant Pandit
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Dec 06, 2005
Posts: 61
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Patience Is a Virtue
(it's not the first time)


[My Blog]
All roads lead to JavaRanch
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
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?


Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: order of listeners