Issue
I have a program creating runnable objects (instances from the same class) on multiple threads.
Each runnable contains an object using ApplicationEventPublisher
to publish event and @EventListener to receive event.
What I want to do is that the receiver on the work thread only receive event published from that thread.
Since the method signatures in the objects on multiple threads are the same, so not sure if an event is published from one thread, then all threads will receive the same event change? If so, then how to make sure the work thread only receive the event published on that thread?
Solution
Event listeners listen to all events of the same type, so that's not a very good use case for Spring events.
If you have all the classes encapsulated anyway and you intend to dispatch the events synchronously, why don't you just bypass Spring, lose the ApplicationEventPublisher
and call the listener object directly when an event happens? Then the work and the events will be handled on the same thread.
Answered By - Kayaman
Answer Checked By - Gilberto Lyons (JavaFixing Admin)