Spring Event Handling/Message exchanging
The below example gives an overview of how the events in Spring is handled using annotations and declarative approach.
In Spring, we can handle both custom events and Spring Events.
For custom events, we need to write both publisher, listener and event classes and pass the event to publisher.publish(ApplicationEvent event) method. Whereas, Spring events (eg.Context events) are published by Spring. We need to write listeners to handle those.
Publisher and Event classes
Listener class
LogsIn Spring, we can handle both custom events and Spring Events.
For custom events, we need to write both publisher, listener and event classes and pass the event to publisher.publish(ApplicationEvent event) method. Whereas, Spring events (eg.Context events) are published by Spring. We need to write listeners to handle those.
Publisher and Event classes
package com.javarephrased.standalone.eg.eventhandling;
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 Publisher implements ApplicationEventPublisherAware{
@Autowired
ApplicationEventPublisher publisher;
public void setApplicationEventPublisher(ApplicationEventPublisher arg0) {
this.publisher = publisher;
}
public void publish(){
publisher.publishEvent(new MyEvent(this, "type1", "Event Object"));
}
}
class MyEvent extends ApplicationEvent{
String eventType;
String eventObject;
public MyEvent(Object source, String eventType, String eventObject) {
super(source);
this.eventType = eventType;
this.eventObject = eventObject;
}
public String getEventType() {
return eventType;
}
public String getEventObject() {
return eventObject;
}
}
Listener class
package com.javarephrased.standalone.eg.eventhandling;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* Custom Event Publisher published the My Event
* Custom Event Listener Registered for My Event to get notifications
* @author Naresh Doniparti
*/
@Component
public class CustomEventListener{
@EventListener
public String onApplicationEvent(MyEvent ev) {
System.out.println("CustomEventListener MyEvent Details :"+ev.getEventObject()+", "+ev.getEventType());
System.out.println("I will do some action for the Myevent");
return "SUCESS";
}
}
/**
* This class registers ContextRefreshedEvent published by ApplicationContext
* @author naresh
*
*/
@Component
class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent>{
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println("ContextRefreshedListener Time: "+event.getTimestamp());
}
}
ContextRefreshedListener Time: 1443945056727
CustomEventListener MyEvent Details :Event Object, type1
I will do some action for the Myevent
Comments
Post a Comment