JSF 2 SystemEventListener Example

The System Events are normally specified in the JavaServer Faces Specification 2.2 under “System Events” Subject. This concept expands on the idea of lifecycle PhaseEvents. With the PhaseEvents, it is possible to have an application scoped PhaseListeners that are given the opportunity to act on the system before and after each phase in the lifecycle. System events provide a much more fine-grained insight into the system, allowing application or component scoped listeners to be notified of a verity of kinds of events.

There are several ways to declare interest in a particular kind of event.

  • Call Application.subscribeToEvent () to add an application scoped listener.
  • Call UIComponent.subscribeToEvent () to add a component scoped listener.
  • Use <f:event> tag to declare a component scoped listener.
  • Use @ListenerFor or @ListenersFor annotation. The scope of the listener is determined by the code that processes the annotation. (This mechanism can be useful for component developers).
  • Use <system-event-listener> element in an application configuration resource to add an application scoped listener.

At this tutorial, you are going to examine the using of <system-event-listener> in an application configuration to add application scoped listener.

Also Read:

1. Managed Bean


package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

public class IndexBean {
	private String message;

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

2. The View


<html xmlns="http://www.w3.org/1999/xhtml"
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 System Events - Application Source Example</h2>
			<h2>This is a JSF View</h2>

3. SystemEventListener


package net.javabeat.jsf.listener;

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class SystemEventsListener implements SystemEventListener{

	public void processEvent(SystemEvent event) throws AbortProcessingException {
		// The handling of event
		System.out.println("System Event Being Processed :: "+event.toString());


	public boolean isListenerForSource(Object source) {
		if(source instanceof Application){
			// This listener for this kind of source
			return true;
		// This listener isn't for this kind of source
		return false;

4. Faces Configuration File


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

5. JSF 2 SystemEventListener Demo

The below snapshots show you the way that used for listening the SystemEventListener.

JSF 2 System Event Example 1 SystemEventListener

JSF 2 System Event Example 2

  • By removing the application from the server, you are fired the PreDestroyApplicationEvent.

JSF 2 System Event Example 3

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!