jeudi 5 mars 2015

spring 4.2 application event is firing twice with Spring MVC , why?

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.idrene.emefana.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

Aucun commentaire:

Enregistrer un commentaire