Blog Image

Example On Event Processing  

Here we are going to explore about spring provided Event Processing to know how we can work with Event Processing in our projects.

Lets see the example:
Technologies used :-
1) java (1.8).
2) maven (3.5.2).
3) Spring (4.0.0).

Project Structure

Execute below command for creating maven project.
mvn archetype:generate -DgroupId=com.sjg
-DartifactId=example-on-event-processing
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Below is the maven project structure with packages and java src files.

Classes
ReloadEvent.java
package com.sjg.beans;

import org.springframework.context.ApplicationEvent;

public class ReloadEvent extends ApplicationEvent {
    private String tableName;

    public ReloadEvent(Object source, String tableName) {
        super(source);
        this.tableName = tableName;
    }

    public String getTableName() {
        return tableName;
    }

}
ReloadEventListener.java
package com.sjg.beans;

import org.springframework.context.ApplicationListener;

public class ReloadEventListener implements ApplicationListener<ReloadEvent> {
    private CacheManager cacheManager;

    public void onApplicationEvent(ReloadEvent event) {
        cacheManager.reload(event.getTableName());

    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

}
EditStateController.java
package com.sjg.beans;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class EditStateController implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher publisher;

    public void editState(String stateCode, String stateName) {
        System.out.println("Updating State into database...");
        ReloadEvent reloadEvent = new ReloadEvent(this, "tblStates");
        publisher.publishEvent(reloadEvent);
    }

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

    }

}
CacheManager.java
package com.sjg.beans;

public class CacheManager {
    public void reload(String tableName) {
        System.out.println("Reloading " + tableName + " data.....");
    }
}
application-context.xml
<?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.xsd">;

    <bean id="editStateController" class="com.sjg.beans.EditStateController" />
    <bean id="cacheManager" class="com.sjg.beans.CacheManager" />
    <bean class="com.sjg.beans.ReloadEventListener">
        <property name="cacheManager" ref="cacheManager" />
    </bean>

</beans>

pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sjg</groupId>
  <artifactId>example-on-event-processing</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>example-on-event-processing</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.srimanjavagroup.com</url>;

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
    </dependencies>
</project>
Test.java
package com.sjg.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sjg.beans.EditStateController;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/sjg/common/application-context.xml");
        EditStateController esc = context.getBean("editStateController", EditStateController.class);
        esc.editState("NY", "New York");
    }
   
}

Please find attached the example_on_event_processing.zip as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image
Sriman

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.