posted 7 years ago
In my application there is a list publisherPostListenerList which receives the real time user posts from the RabbitMQ queue to be sent to the subscribers/consumers. The list is a property of ApplicationListener class which listens to the events of the pubsub queue. The below controller method fetches the list elements via getter method & based on a logic pushes the posts to the subscribers.
The flow is as follows
User writes a post -> Post gets into DB + Queue -> Message from Queue is added in a list which is publisherPostListenerListto be pushed to the subscribers of the user.
As we can see the publisherPostListenerList is a common list for n concurrent requests due to ApplicationListener being a singleton. For a single instance the setup works fine but will fail in a clustered environment as each node will have its own individual publisherPostListenerList list.
How do I deal with this situation? I can't make ApplicationListener class stateless I need the list to store the post elements received from the queue. Do I put the list in a distributed in memory cache? Or there is any other conventional way?
ApplicationListener.java
Controller method for pushing the message to the subscriber