Spring Boot Event Listener not working

Spring Boot Event Listener is not working because the event listener is not properly configured or the ApplicationListener interface is not implemented. The ApplicationListener interface listens for spring boot events and calls the onApplicationEvent interface function when one occurs. Instead of implementing the ApplicantListener Interface, the @EventListener annotation is used. To listen for spring boot events, add the @EventListener annotation before the listener method. The Listener class does not implement the ApplicationListener interface, and @EventListener is not used in the listener method.

A spring boot event listener class may be implemented in two ways. Implementing the ApplicationListener interface and the onApplicationEvent function provides a listener class. The @EventListener annotation is the second choice. The @EventListener annotation should be used on the listener method. The event object that is given as an argument should be extended with the ApplicationEvent class.



Root Cause

The publisher could not identify the listener class to send the published event in the spring boot application, which is the underlying cause of the spring boot event listener not working. The listener should implement the ApplicationListener interface or use the @EventListener annotation. The published event should be extended with an instance of the ApplicationEvent class. If the listener class is correctly implemented, the listener will listen to the published event.



Solution 1

The ApplicationListener implementation class should look like the one below. The class should be annotated with the @Component annotation. The ApplicationListener interface should be used to implement the Listener class. The ApplicationListener interface should be provided with the ApplicationEvent class, which is used as the listener method’s parameter. In the listener class, the onApplicationEvent method should be overridden. The onApplicationEvent listener method will be executed if the listener class is working.

MyCustomApplicationListener.java

package com.yawintutor;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MyCustomApplicationListener implements ApplicationListener<MyCustomApplicationEvent> {

	@Override
	public void onApplicationEvent(MyCustomApplicationEvent event) {
		System.out.println("MyCustomApplicationListener       : Source : "+event.getSource().getClass().getName() + ", Message : "+ event.getName());	
	}
}


Solution 2

Spring boot annotations might be used to configure the listener method. The @EventListener annotation should be applied to the listener method. The listener method should take an argument of the ApplicationEvent extended class and be annotated with @EventListener. The @EventListener annotated method will be called if the application event publisher publishes an event.

MyCustomApplicationListenerConfiguration.java

package com.yawintutor;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

@Configuration
public class MyCustomApplicationListenerConfiguration {

	@EventListener
	public void onApplicationEvent(MyCustomApplicationEvent event) {
		System.out.println("MyCustomApplicationListenerConfiguration      : Source : "+event.getSource().getClass().getName() + ", Message : "+ event.getName());	
	}
}


Solution 3

When creating the event class, the ApplicationEvent class should be extended. Make sure the event class is extended with the ApplicationEvent class. The event class is a java bean class with getter and setter methods for bean properties. The data will be sent from the publisher to the listener classes via the ApplicationEvent class.

MyCustomApplicationEvent.java

package com.yawintutor;

import org.springframework.context.ApplicationEvent;

@SuppressWarnings("serial")
public class MyCustomApplicationEvent extends ApplicationEvent {

	private String name;
	
	public MyCustomApplicationEvent(Object source) {
		super(source);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}	
}


Solution 4

The events are published using the ApplicationEventPublisher class. Ensure that the ApplicationEventPublisher class is autowired in the code and that it is used to publish the event to the listener. To publish the event, the listener class should use the publishEvent method. The event class used in the publishEvent method should be extended with the ApplicationEvent class.

MyCustomApplicationEventPublisher.java

package com.yawintutor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class MyCustomApplicationEventPublisher {

	@Autowired
	ApplicationEventPublisher publisher;
	
	public void publish() {
		MyCustomApplicationEvent event = new MyCustomApplicationEvent(this);
		event.setName("My Event");
		System.out.println("MyCustomApplicationEventPublisher : Source : "+this.getClass().getName() + ", Message : "+ event.getName());
		publisher.publishEvent(event);
	}
}



Leave a Reply