3

I am working with Spring 4.2.0.BUILD-SNAPSHOT events , to some reason that I haven't figured out yet,the listener is firing twice after publishing any events whether extending from ApplicationEvent or any arbitrary event, however everything works as expected while running test-cases, now wondering what is going on with annotation-driven events in Spring MVC context

Event publishing Interface

public interface ListingRegistrationService {
    public void registerListing(ListingResource listing);

}

@Component
class ListingRegistrationServiceImpl implements ListingRegistrationService{

    private final ApplicationEventPublisher publisher;

    @Autowired
    public ListingRegistrationServiceImpl(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    @Override
    public void registerListing(ListingResource listing) {
       //process
        publisher.publishEvent(new ListingCreatedEvent(listing));
        System.out.println("Event above...");
    }

}

Event Listener

@EventListener
    @Async
    public void sendMailForSuggestedListing(Supplier<ListingResource> listingCreatedEvent)  {
        System.out.println("Event fired...");
    }

end-point/entry point

public ResponseEntity<ResponseStatus> registerListing(@RequestBody @Valid ListingResource listing,BindingResult result) throws URISyntaxException {
       ResponseEntity<ResponseStatus> response = null;
       listingService.registerListing(listing); //  publish the event
       response = ResponseEntity.created(new URI(""));
         return response;
            }

Result : Event fired... Event fired... Event above..

I suspect indeed that the EventListener bean is registered twice or something. You can enable org.springframework.context.event.EventListenerMethodProcessor to trace level to check what happens to this particular class.

– Stéphane Nicoll

TRACE org.springframework.context.event.EventListenerMethodProcessorIt is happening twice for everything

12:02:32,878 DEBUG ntext.event.EventListenerMethodProcessor: 138 - 1 @EventListener methods processed on bean 'mailServiceImpl': [public void com.service.MailServiceImpl.sendMailForSuggestedListing(com.service.events.CreationEvent)]
12:02:32,878 DEBUG ntext.event.EventListenerMethodProcessor: 138 - 1 @EventListener methods processed on bean 'mailServiceImpl': [public void com.service.MailServiceImpl.sendMailForSuggestedListing(com.service.events.CreationEvent)]
12:02:32,878 TRACE ntext.event.EventListenerMethodProcessor: 132 - No @EventListener annotations found on bean class: class com.service.MetaServiceImpl
12:02:32,878 TRACE ntext.event.EventListenerMethodProcessor: 132 - No @EventListener annotations found on bean class: class com.service.MetaServiceImpl

Java configuration

@Configuration
@ComponentScan(basePackages = {"com.**.domain",
        "com.**.repositories", "com.**.service",
        "com.**.security" })
@PropertySource(value = { "classpath:application.properties" })
public class ServiceConfig 


Configuration
@EnableWebMvc
@EnableSwagger
@EnableSpringDataWebSupport
@EnableMongoRepositories("com.**.repositories")
@ComponentScan(basePackages = {"com.**.config","com.**.rest.controllers","com.**.rest.tokens"})
public class WebConfig extends WebMvcConfigurerAdapter {

@Configuration
@EnableMongoRepositories("com.**.**.repositories")
public class MongoRepositoryConfig extends AbstractMongoConfiguration
15
  • somehow is the event listener registered twice? post the class level code for the event listener. what all does it implement?
    – Paul John
    Mar 5, 2015 at 6:44
  • @iamiddy - I could not find the @EventListener annotation in spring. Could you please let us know from where are you importing this annotation? In a spring application events and listeners are created using ApplicationEvent and ApplicationListener respectively.
    – Mithun
    Mar 5, 2015 at 7:02
  • It could be that your spring context is duplicated e.g. stackoverflow.com/questions/22436195/…
    – StanislavL
    Mar 5, 2015 at 7:23
  • I suspect indeed that the EventListener bean is registered twice or something. You can enable org.springframework.context.event.EventListenerMethodProcessor to trace level to check what happens to this particular class. Mar 5, 2015 at 8:54
  • indeed @StéphaneNicoll , by the TRACE as you suggested the EventListener is registered twice, have edited the question to include the trace
    – iamiddy
    Mar 5, 2015 at 17:25

2 Answers 2

6

I had this same issue. For me... I registered the event listener twice because I blanketed scanned my entire package path in one configuration class

@ComponentScan(  basePackages = {"my.root.package"} )
@Configuration
public class MyAppConfig {
      //... 
}

and then I manually added the same eventListener bean in another configuration class like so

@Configuration
public class SpringConfig {
    ...
    // SomeEventListener was located @ my.root.package.event and had @Component annotation
    @Bean 
    public SomeEventListener someEventListener() {
        return new SomeEventListener();
    }
    ...
}

causing it to register twice.

0

Encountered a similar situation - events published twice.

Root cause in my case was a misconfiguration of the web application context in web.xml: DispatcherServlet was passed the same context xml file as was passed to the servlet listener. Replaced the duplicate reference in the DispatcherServlet declaration with an empty one, as suggested here:

If an application context hierarchy is not required, applications may configure a “root” context only and leave the contextConfigLocation Servlet parameter empty.

That resolved the issue for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.