mercoledì 23 maggio 2012

Intercepting Java Web Server Startup And ShootDown with servlet and JVM hook

In this sample you may quite easy intercept events of Web Server starting completed and shut down  done implementing a Servlet.
When init() metod is invoked on servlet you may suppose that web server has completed start procedure (usually gui are latest module deployed and moreover are deployed only if all dependent modules are correctly deployed).
To intercept shutdown event you may use JVM shootdown hook (Runtime.getRuntime stands for JVM in sample) so when shutdown signal is sent to JVM your thread starts.

After coding servlet don't miss to configure your web.xml file.

Here web.xml fragment
     <servlet>
        <servlet-name>StartAndStopListener</servlet-name>
        <servlet-class>com.italtel.snodo.util.servlet.StartAndStopListener</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>StartAndStopListener</servlet-name>
        <url-pattern>/StartAndStopListener</url-pattern>
    </servlet-mapping>
   
   
And here servlet code



import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServlet;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.italtel.systmng.applog.api.ApplogInterface;



public class StartAndStopListener  extends HttpServlet {
   
   
    /**
     *
     */
    private static final long serialVersionUID = -4370442950501287760L;

    /*
     * Logger di classe
     */
    private Logger logger = LogManager.getLogger(this.getClass());
    private ApplogInterface applogger;
    private Thread myShutDownInstance;
   
    public static final String APPLICATION_NAME = "CM_NOFUN_JBOSS_BE";
    public static final String EVENT_NAME_START = "CM_GUI_STARTUP";
    public static final String EVENT_NAME_STOP  = "CM_GUI_SHUTDOWN";
    public static final String EVENT_TYPE_START = "CLEARED" ;
    public static final String EVENT_TYPE_STOP  = "P_WARNING" ;


   


    @Override
    public void init() {
        applogger = new ApplogInterface();
        myShutDownInstance = new MyShutdown(applogger);
        registerJVMStop();
        Date date = new Date ();
        String appLogDateString = getDateAppLog(date);
        logger.info("CONF.WAR STARTING EVENT INTERCEPTED");
        this.applogger.sendMsg(APPLICATION_NAME, EVENT_NAME_START , EVENT_TYPE_START, appLogDateString, "CM Gui Started");
        logger.info("CONF.WAR STARTING EVENT INTERCEPTED");
    }

   

   
    private void registerJVMStop()  {
        Runtime.getRuntime().addShutdownHook(this.myShutDownInstance);

    }


   
    public static String getDateAppLog(Date date){
        String ret;
        String dateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        ret = sdf.format(date);
        return ret;
    }
   
}


class MyShutdown extends Thread {
    private Logger logger = LogManager.getLogger(this.getClass());
   
    private ApplogInterface applogger;
   
    public MyShutdown(ApplogInterface applogger) {
        super();
        this.applogger = applogger;
        logger.info("MyShutDown thread created");
    }
   
    public void run() {
        try {
            logger.info("JVM STOPPING EVENT INTERCEPTED");
            Date date = new Date ();
            String appLogDateString = StartAndStopListener.getDateAppLog(date);
            this.applogger.sendMsg(StartAndStopListener.APPLICATION_NAME, StartAndStopListener.EVENT_NAME_STOP ,StartAndStopListener.EVENT_TYPE_STOP, appLogDateString, "CM  ShutDown Executed");

        } catch (Exception ee) {
            ee.printStackTrace();
        }   
    }
}

Nessun commento:

Posta un commento