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()



Wednesday, August 25, 2010

Log4j in J2EE

1- Create log4j.properties with following example contents. Important part is bold, i.e. you are creating a logger which will act as root of all of your classes. In this case all you classes reside in com.abc.xyz package or its sub-packages.


log4j.logger.com.abc.xyz=DEBUG, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d : [%-5p] - %m%n



2- Put log4j.properties in EarContent/APP-INF/classes or WebContent/WEB-INF/classes.


3- Any where in your startup code e.g. ServletContext or Servlet or EJB, configure log4j.

PropertyConfigurator.configure(this.getClass().getClassLoader().getResource("log4j.properties"));