Home » Java » java – Unable to use multiple instances of spring event listener-Exceptionshub

java – Unable to use multiple instances of spring event listener-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to get familiar with the spring event listener in java and I am encountering weird behavior.
I have a publisher class and a listener class and I am trying to use multiple instances of the same listener class (don’t know if it’s possible).
Here is my code:

public class TestClass {

    public static void main(String[] args) throws Exception {
        testEvents();
    }

    private static void testEvents() throws InterruptedException {
        AnnotationConfigApplicationContext eventPublisherContext = new AnnotationConfigApplicationContext(EventListenerConfig.class);
        CustomSpringEventPublisher eventPublisher = eventPublisherContext.getBean(CustomSpringEventPublisher.class);
        List<CustomSpringEventListener> listenersList = new ArrayList<CustomSpringEventListener>();

        for (int i = 0; i < 5; i++) {
            listenersList.add(i, new CustomSpringEventListener());
        }

        for (int i = 0; i < 10; i++) {
            Thread.sleep(5000);

            LocalDateTime now  = LocalDateTime.now();
            eventPublisher.doStuffAndPublishAnEvent("new event " + now.format(Formats.isolong));
        }
    }
}

My listener class looks like that:

@Component
public class CustomSpringEventListener implements ApplicationListener<CustomSpringEvent> {

    @Override
    public void onApplicationEvent(CustomSpringEvent event) {
        System.out.println("Received spring custom event - " + event.getMessage());
    }

}

My publisher class looks like that:

@Component
public class CustomSpringEventPublisher {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void doStuffAndPublishAnEvent(final String message) {
        System.out.println("Publishing custom event --> " + message);
        this.applicationEventPublisher.publishEvent(new CustomSpringEvent(this, message));
    }

}

My config class looks like that:

@Configuration
@ComponentScan({"some.local.directory"})
public class EventListenerConfig {
}

I would expect the output of this code would be one print for the publish followed by 5 prints of the receiving. However, the actual output is:

Publishing custom event --> new event 2020-02-17T08:07:03.349
Received spring custom event - new event 2020-02-17T08:07:03.349
Publishing custom event --> new event 2020-02-17T08:07:08.389
Received spring custom event - new event 2020-02-17T08:07:08.389
Publishing custom event --> new event 2020-02-17T08:07:13.403
Received spring custom event - new event 2020-02-17T08:07:13.403

It seems like the for loop in which I add new instances of the same listener to a list is being ignored. Why is that happening? Is it even possible to make several instances of the same listener and have them receive the published event? How can I alter my code to make it work? What would happen if I will try to instantiate the same listener in several threads working together, would only one of them respond to the event?
Any help would be appreciated, thank you.

How to&Answers: