Application event handling in Spring Boot

Jeevan Patil
Deskera Engineering

--

Discussing the application event handling in Spring boot.

You might have heard about the queueing mechanism providers like RabbitMQ, Apache Kafka, ActiveMQ. These are best suited in distributed systems. Imagine the use case where an application has to send an email after a new user is registered. In an ideal world, email and registration services are deployed independently. Registration service will trigger an event with the payload containing the new user’s information. This event is then consumed by the email service and an email has been sent to the user.

What if you want to create an event and consume it in the same application. Of course, we can have Kafka, ActiveMQ configured, and consume those events. But there are certain scenarios where you don’t need all these queue mechanism providers. In such situations, you can make use of application events which spring provides.

Spring boot provides event publishing and subscriber capability. Suppose an application needs to update project-users mapping information in the database table as soon as the project is created.

So this is how it could be achieved:

  1. A New project has been created.
  2. Create a new entity, in our case a project created event which will be published by ApplicationEventPublisher.
  3. ApplicationListener will listen to this event and update the project-user mapping.

This is a sample Project entity:

@Data
public class Project {
@Id private String id; private String name; private String ownerEmail;}

Create a base event class that extends ApplicationEvent. This class will be a base class for all the entity creation events.

import org.springframework.context.ApplicationEvent;public class EntityCreatedEvent<T> extends ApplicationEvent {  private T entity;  public EntityCreatedEvent(Object source, T entity) {
super(source);
this.entity = entity;
}
public T getEntity() {
return entity;
}
}

Publish an event after an entity is created.

@Servicepublic class ProjectServiceImpl implements ProjectService {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void createProject(Project project) {
// create project and publish an event to update the mapping
publishProjectCreatedEvent(project);
}
void publishProjectCreatedEvent(final Project project) {
EntityCreatedEvent entityCreatedEvent = new EntityCreatedEvent(this, project);
applicationEventPublisher.publishEvent(entityCreatedEvent);
}
}

Listen to the event and send mail after an entity is created.

@Component
public class EntityCreatedEventListener implements
ApplicationListener<EntityCreatedEvent> {
@Autowired
private EmailService emailService;
@Override
public void onApplicationEvent(EntityCreatedEvent event) {
if (event.getEntity() != null && event.getEntity() instanceof Project) {
emailService.updateProjectUserMapping(project);
}
}
}

Conclusion:

We have learned how to create application events and listen to them. The situations where you do not need queue mechanisms, application events can be helpful.

--

--