Spring Custom Events

Spring framework provides a good way to write “Event-Driven” application! Using Spring we can code our own “custom event” or extends standard ones.

It would be nice to have annotations for events!

springsource

Objective:

  • How to develop Spring custom event, how to publish it and how to listen to it?

Environment & Tools:

  • Eclipse
  • Maven

Library:

  • Spring framework

( 1 ) Project Structure

  • LoginEvent,java : This is the custom event
  • Login.java: Bean that will publish “fire” the event
  • LoginTracker.java: Bean that will listen to the event
  • App.java: Main class that will run our sample
  • spring-config.xml: Spring XML context configuration file.

( 2 ) “Custom” Event – Publish – Listen

  • /src/main/java/com/hmkcode/event/LoginEvent.java
package com.hmkcode.event;

import java.util.Date;
import org.springframework.context.ApplicationEvent;

public class LoginEvent extends ApplicationEvent  {

  private Date date;

	public LoginEvent(Object source){
		super(source);
		this.date = new Date(getTimestamp());
	}

       //setter & getter...
}
  • src/main/java/com/hmkcode/beans/Login.java
package com.hmkcode.beans;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import com.hmkcode.event.LoginEvent;

public class Login implements ApplicationEventPublisherAware {

  private String username;
	private ApplicationEventPublisher publisher;

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
		this.publisher = publisher;
	}

	public void login(){

		//do the login.. 

		//fire the event
		LoginEvent event = new LoginEvent(this);
        publisher.publishEvent(event);
	}

      //getter & setter...
}
  • src/main/java/com/hmkcode/beans/LoginTracker.java
package com.hmkcode.beans;

import org.springframework.context.ApplicationListener;
import com.hmkcode.event.LoginEvent;

public class LoginTracker implements ApplicationListener<LoginEvent>  {

  @Override
	public void onApplicationEvent(LoginEvent event) {

		System.out.println("\n"+((Login)event.getSource()).getUsername()+" logged-in @ "+event.getDate()+"\n");

	}
}

( 3 ) Spring Configuration

  • /src/main/resources/spring-config.xml

We initiate two beans “publisher (Login) & listener (LoginTracker)”. Ther property “username” is just to show that we can access event source “publisher” properties.

<?xml version="1.0" encoding="UTF-8"?>
<beans   xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

		<bean id="login" class="com.hmkcode.beans.Login">
			<property name="username" value="hmkcode" />
		</bean>
		<bean id="loginTracker" class="com.hmkcode.beans.LoginTracker"></bean>
</beans>

( 4 ) Run it

package com.hmkcode;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hmkcode.beans.Login;

public class App 
{
    public static void main( String[] args )
    {
      ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");

    	Login login = (Login) ctx.getBean("login");
    	login.login();
    }
}

We can run this code using Maven command

>mvn exec:java

Source Code @ GitHub