Web application events
In servlet 2.3 spec, web application events are introduced that give you greater degree of control over your web application. The two important application events are:
- Application startup and shutdown
- Session creation and invalidation
As their names suggest, application startup event occurs when your web application is first loaded and started by the Servlet container and application shutdown event occurs when the web application is shutdown.
Session creation event occurs every time a new session is created on the server and similarly session invalidation event occurs every time a session is invalidated. To make use of these web application events and to do something useful you’ll have to create and make use of special “listener” classes.
Listener
These are simple Java classes which implement one of the two following interfaces:
- javax.servlet.ServletContextListener
- javax.servlet.http.HttpSessionListener
If you want your class to listen for application startup and shutdown events then implement ServletContextListener interface. If you want your class to listen for session creation and invalidation events then implement HttpSessionListener interface.
ServletContextListener - This interface contains two methods :
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);
HttpSessionListener - This interface contains two methods also:
public void sessionCreated(HttpSessionEvent se);
public void sessionDestroyed(HttpSessionEvent se);
Example of usage – use HttpSessionListener to count how many active session
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;public class SessionCounter implements HttpSessionListener {
private static int activeSessions = 0;
/* Session Creation Event */
 public void sessionCreated(HttpSessionEvent se) {
 activeSessions++;
 }
/* Session Invalidation Event */
 public void sessionDestroyed(HttpSessionEvent se) {
 if(activeSessions > 0)
 activeSessions--;
 }public static int getActiveSessions() {Register listener to the web application
 return activeSessions;
 }
}
<!-- Web.xml -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd“><web-app><!– Listeners –>
 <listener>
  <listener-class>
  com.stardeveloper.web.listener.SessionCounter
  </listener-class>
 </listener>
 <listener>
  <listener-class>
  com.stardeveloper.web.listener.ApplicationWatch
  </listener-class>
 </listener></web-app>
Spring Events
Spring provides a simple mechanism for sending and receiving events between beans. To receive an event, a bean implements ApplicationListener, which has a single method:
public void onApplicationEvent(ApplicationEvent event);
To publish events to listeners you call the publishEvent() method the ApplicationContext. This will publish the same event to every listener in the context. Event listeners receive events synchronously. This means the publishEvent()Â method blocks until all listeners have finished processing the event. it is possible to supply an alternate event publishing strategy via a ApplicationEventMulticaster implementation. Furthermore, when a listener receives an event it operates inside the transaction context of the publisher, if a transaction context is available.
You can be both listener or publisher. If it is a publisher, it needs to have access to the ApplicationContext. This means that beans will have to made aware of the container that they are running in. You can create your own custom event via extends ApplicationEvent class. In addition to events that are published by other beans, the Spring container itself publishes a handful of events during the course of an application’s lifetime. These application events include:
- ContextClosedEvent - publish when the application context is closed
- ContextRefreshEvent - publish when the application context is initialized or refreshed
- RequestHandledEvent - publish when a request is handled
Put them together
Look at how acegi publish session creation/destroy events to the bean(s) listening on this. It has HttpSessionEventPublisher class that implements HttpSessionListener. So, web container will trigger its sessionCreated and sessoinDestroy methods when session is created an destroyed. Within these methods, the publisher will use ApplicationContext to publish its own HttpSessionCreatedEvent and HttpSessionDestroyedEvent to all the spring bean(s) listening on these. (code)
Reference
http://java.sys-con.com/read/171482_1.htm (Acegi)
http://www.acegisecurity.org/articles.html (Acegi)






































(4.75 out of 5)
No Comment Received
Sorry the comment area are closed for non registered users