Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Listeners

 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers

Consider this code:

<listener>
<listener-class>MySessionListener</listener-class>
</listener>
<listener>
<listener-class>MyContextListener</listener-class>
</listener>

The order in which its getting called is first ContextListener then SessionListener. But I read in one of the mock exam that listeners are called in the order they are declared in the DD i.e, first SessionListener then ContextListener. Please let me know which one is correct.

Thanks
Srividhya
 
Paulo Marcio Brandi Rezende
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have two (or more) listeners listening to the same thing, the order in the DD cares, but in your example you have two listeners listening different things.

MySessionListener is listening sessions initializations and destructions.

MyContextListener is listening context initialization and destruction (they happens 'almost' once, they can happen twice if you restart the server or the app).

Of course, a context initialization will happen before any session initialization, and the sessions destructions will happen before the context destruction.

But, I really don't if the container call the session destructions events before shutdown. It is really a very good question?
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul

I think the container calls the session destruction before the context destroyed. I have checked that. Anyway thanks for your reply.

Srividhya
 
Bala'J'i Rags
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srividya,

So does it mean that the container executes in the order given in the DD?
 
Paulo Marcio Brandi Rezende
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container will always destroy the session before destroy the context.

Even you declare in the DD a listener for context before a listener for session.

Then, in this case, the session's listener will be called before the context's listener.

The order cares if you declare many listeners that extends ServletContextListener and many listeners that extends HttpSessionListener.

The container will execute before the session's listeners, in the order of DD declaration.

Then container will execute the context's listeners, in the order of DD declaration.
[ August 06, 2008: Message edited by: Paulo Rezende ]
 
Paulo Marcio Brandi Rezende
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, the listerners just listen

They don't change the order which the container works.
[ August 06, 2008: Message edited by: Paulo Rezende ]
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and first the contextlistener(construction) will be called and then sessionlistener(construction) and then again sessionlistener(destruction) and then contextlistener(destruction)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic