Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Event Handling in Spring

          Spring provides a very simple mechanism to publish and listen to events. It exposes the abstract class ApplicationEvent which has concrete implementations in ApplicationContextEvent and RequestHandledEvent. This class can be extended to implement custom application events.
First create the event classes you want to listen to and add fields for any additional information you want the listener to get hold of.


import org.springframework.context.ApplicationEvent; 

public class CreateClientEvent extends ApplicationEvent {
private final String payload;
public CreateClientEvent (final Object source, final Long payload) {
super(source);
this.payload = payload;
}
}

Then create an enum to keep track of all the events you want to publish.


public enum EventType {

CREATE_CLIENT("create_client”),
UPDATE_CLIENT("update_client”);

private String eventName;

private EventType(final String eventName) {
this.eventName = eventName;
}

public static EventType get(final String eventName) {
for (final EventType et : values()) {
if (et.eventName.equalsIgnoreCase(eventName)) {
return et;
}
}
throw new UnknownEventTypeException(eventName);
}
}

Let’s get to the publishing part. We will assume there is a single entry point somewhere in your application where services get looked up or some controller or some authentication filter so that event publishing code is uniform and not strewn all across your code. Now get hold of the spring ApplicationContext or ApplicationEventPublisher.  

@Service 
public class SomeService implements ApplicationEventPublisherAware {
private ApplicationEventPublisher eventPublisher;

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

public void someFunction(final String eventName, final String payload) {
final EventType eventType = EventType.get(eventName);
this.publishEvent(eventType, payload);
}

private void publishEvent(final EventType eventType, final String payload) {
switch (eventType) {
case CREATE_CLIENT:
this.eventPublisher.publishEvent(new CreateClientEvent(this, payload));
break;
case UPDATE_CLIENT:
this.eventPublisher.publishEvent(new UpdateClientEvent(this, payload));
break;
}
}

If you wish to make this part asynchronous, you need to add the following bit of configuration :-


<bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster"> 
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>


Now, all you have to do is add your event listeners and add the processing logic inside onApplicationEvent and you are done.


@Component 
public class CreateClientEventListener implements ApplicationListener<CreateClientEvent> {

@Override
public void onApplicationEvent(CreateClientEvent createClientEvent) {
logger.info("Processing create client event…");
}
}


This post first appeared on Night Without End, please read the originial post: here

Share the post

Event Handling in Spring

×

Subscribe to Night Without End

Get updates delivered right to your inbox!

Thank you for your subscription

×