Thursday, August 26, 2010

Weblogic Listeners



J2EE standard ServletContextListener has following two methods:


1- public void contextInitialized(ServletContextEvent event)
2- public void contextDestroyed(ServletContextEvent event)


And you need to have web application (may by dummy) where you put following snippet in web.xml

 <listener>
   <listener-class>
   ServletContextListener
   </listener-class>
  </listener>


When you need more application events you can use weblogic.application.ApplicationLifecycleListener by extending your listener class from it. It provides:


1- public void preStart(ApplicationLifecycleEvent event)
2- public void postStart(ApplicationLifecycleEvent event)
3- public void preStop(ApplicationLifecycleEvent event)
4- public void postStop(ApplicationLifecycleEvent event)


Add an entry in weblogic-application.xml



<wls:listener>
  <wls:listener-class>ApplicationListener</wls:listener-class>
</wls:listener>


For server related events you can implement following interface:

com.bea.wlcp.wlng.core.wls.module.WlsStatusListener

It has a method which you can implement to listen for events:


public void handleWlsStatusChange(WlsStatus wlsStatus);

Example events are


WlsStatus.RESUMING;
WlsStatus.RUNNING;
WlsStatus.STANDBY


You can add/remove your listener as:


WlsListener.addWLSStatusListener
WlsListener.removeWLSStatusListener


Sometimes you need to check whether weblogic server is running or not e.g. if you are calling a webservice at application startup which is also deployed in the same server, you can use:


WlsListener.isServerRunning()



No comments: