Application eventing using Spring Framework

An eventing mechanism is a powerful tool for decoupling different components of application modules. Application level events are also useful for maintaining state changes in an application. In this article, we will create a simple application to Spring framework based eventing mechanism to publish events and listening to the events. This article presumes the working maven installation.

Let’s start by creating a simple maven project “spring-app-event-demo” by executing the following command on your  terminal application:

1
2
3
mvn archetype:generate -DgroupId=com.wordpress.omanandj -DartifactId=spring-app-event-demo  
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Spring eventing framework works on the Base Object ApplicationEvent. Now, create an interface “IAppEventPublisher” to publish your event.

1
2
3
4
5
6
7
8
package com.wordpress.omanandj.event;
 
import org.springframework.context.ApplicationEvent;
 
 
public interface IAppEventPublisher {
    public void publish(ApplicationEvent event);
}

Now we will implement IAppEventPublisher to publish our demo events. Spring has interface ApplicationEventPublisher to publish the events and ApplicationEventPublisherAware to make the spring bean aware of eventing framework.  Create a class AppEventPublisher which implements interfaces  ApplicationEventPublisherAware and IAppEventPublisher as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.wordpress.omanandj.event;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
 
 
@Component
public class AppEventPublisher implements ApplicationEventPublisherAware, IAppEventPublisher {
 
    private ApplicationEventPublisher publisher;
 
    private static final Logger LOG = LoggerFactory.getLogger(AppEventPublisher.class);
 
    @Override
    @Autowired
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher =applicationEventPublisher;
    }
 
    public void publish(ApplicationEvent event) {
        LOG.info("publishing event {}", event);
        publisher.publishEvent(event);
    }
}

You may have noticed above, I have added annotation @Component to create class  AppEventPublisher as a spring bean.

Before creating our event listener, first, create a model class NewAppEvent for our application event.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.wordpress.omanandj.event;
 
import org.springframework.context.ApplicationEvent;
 
 
public class NewAppEvent extends ApplicationEvent{
 
 
    /**
     * Create a new ApplicationEvent.
     *
     * @param source the component that published the event (never {@code null})
     */
    public NewAppEvent(Object source) {
        super(source);
    }
}

For creating the Application event listener, you need to implement ApplicationListener<E extends ApplicationEvent>.  Create a listener AppEventListener, which will log any event it receives.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.wordpress.omanandj.event;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.Log4jLoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
 
 
@Component
public class AppEventListener implements ApplicationListener<NewAppEvent> {
 
    private static final Logger LOG = LoggerFactory.getLogger(AppEventListener.class);
 
    @Override
    public void onApplicationEvent(NewAppEvent event) {
        LOG.info("Event Occured {} " , event);
    }
}

You are almost done with your basic spring application eventing framework. Now you need to configure all the spring beans. Let us create a java class AppConfiguration to specify spring configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.wordpress.omanandj;
 
import com.wordpress.omanandj.config.AppConfiguration;
import com.wordpress.omanandj.event.IAppEventPublisher;
import com.wordpress.omanandj.event.NewAppEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
/**
 * App Event Demo
 *
 */
public class App
{
    private static final Logger LOG = LoggerFactory.getLogger(App.class);
 
    public static void main( String[] args )
    {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);
 
        IAppEventPublisher eventPublisher = context.getBean(IAppEventPublisher.class);
 
        LOG.info("Hello World for App Event using Spring");
        for(int i = 0; i < 100; ++i) {
        eventPublisher.publish(new NewAppEvent("Publishing a new App" + i));
        }
    }
}

You can download the complete source code from my git repository.

Leave a Reply

Your email address will not be published.