0

I have a publish subscribe queue message listener method which fires an event when it reads a message in the queue & adds the message to a publisherPostListenerList in the ApplicationListener class.

The messages added in the list are being pushed to multiple subscribers like real time notifications from the controller method.

After pushing the message to a respective logged in user I am removing the message from the list to avoid the push of redundant message to the user also it keeps in check the size of the list.

The problem is there is only single instance of publisherPostListenerList for all the logged in users. The list being an instance list there should be a separate copy for every logged in user but its not the case. Please correct me if I am wrong.

If I delete a message it deletes the message from the publisherPostListenerList for all the subscribers.

Now my questions are

1. Shouldn't there be an individual copy of publisherPostListenerList list for every logged in user, the list being an instance list.

2. How do I create a separate copy of list for all the logged In users

P.S. I tried changing the bean scope to prototype but didn't work.

Below is the ApplicationEventListener class which holds the list & the controller code.

ApplicationListener.java

@Component
public class ApplicationEventListener {

    private List<Post> publisherPostListenerList = new CopyOnWriteArrayList<Post>();

    private static final Logger logger = Logger.getLogger(ApplicationEventListener.class);

    @EventListener
    public void postSubmissionEventHandler(PostSubmissionEvent event) throws IOException {
        Post post = event.getPost();
        logger.debug("application published user post received " + post);
        publisherPostListenerList.add(post);
    }

    public List<Post> getPublisherPostListenerList() {
        return publisherPostListenerList;
    }

    public void setPublisherPostListenerList(List<Post> publisherPostListenerList) {
        this.publisherPostListenerList = publisherPostListenerList;
    }
}

Controller method for pushing the message to the subscriber

@RequestMapping(value="/getRealTimeServerPushUserPosts")
    public SseEmitter getRealTimeServerPushUserPosts(@RequestParam("userId") int userId){
        SseEmitter sseEmitter = new SseEmitter();
        CustomUserDetail myUserDetails = currentUserAccessor.getCurrentLoggedInUser();
        User loggedInUser=myUserDetails.getUser();

        List<Integer> userPublisherIDList = this.userService.loadUserPublisherIdListWhichLoggedInUserFollows(loggedInUser);
        List<Post> postList =eventListener.getPublisherPostListenerList();


        for(Integer userPublisherId : userPublisherIDList){
            for(Post post:postList){
                    if((userPublisherId.intValue()) == (post.getUser().getUserId().intValue())){
                        try {
                        sseEmitter.send(post);
                        postList.remove(post); //removes the post for all the subscribers as the list acts as a global list.
                    } catch (IOException e) {
                        logger.error(e);
                    }
                }
             }
         }
        return sseEmitter;
    }
underdog
  • 4,188
  • 7
  • 38
  • 81

0 Answers0