JSF System Event Listener

For implementing System Events(PostConstructApplicationEvent or PreDestroyApplicationEvent), we need to create one class implementing SystemEventListener interface. And same should be configured in jsf configuration file.

package com.javasafari;

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
 
public class MyAppSystemListener implements SystemEventListener{
 
  @Override
  public void processEvent(SystemEvent event) throws AbortProcessingException {
 
	if(event instanceof PostConstructApplicationEvent){
		System.out.println("PostConstructApplicationEvent");
	}
 
	if(event instanceof PreDestroyApplicationEvent){
		System.out.println("PreDestroyApplicationEvent");
	}
 
  }
 
  @Override
  public boolean isListenerForSource(Object source) {
	
	return (source instanceof Application);
 
  }	
}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

<application>
 <!-- PostConstructApplicationEvent listener which will get called after
			application is started --> 
    	<system-event-listener>
		<system-event-listener-class>
			com.mkyong.MyAppSystemListener
		</system-event-listener-class>
		<system-event-class>
			javax.faces.event.PostConstructApplicationEvent
		</system-event-class>    					
    	</system-event-listener> 	 
 <!-- PreDestroyApplicationEvent listener which will get called before
			application is going to destroy -->
    	<system-event-listener>
		<system-event-listener-class>
			com.javasafari.MyAppSystemListener
		</system-event-listener-class>
		<system-event-class>
			javax.faces.event.PreDestroyApplicationEvent
		</system-event-class>    					
    	</system-event-listener> 	 
 
    </application>

</faces-config>

Now when we start or stop application process Event method will get executed.