mercredi 15 avril 2015

Spring MVC : Maintaining a list of online users using SessionRegistry


  • I am working on a Spring-MVC application in which there are groups and I would like to display who are the online users currently from the group. The initial method I did was to check the sessionRegistry with non-expired sessions and add those usernames in database and then the database can be queried for who is online. Plus I also started a cleaner every 5 minutes to see who went offline and all, and change display accordingly. Code for that is given below.


Needless to say, it turned out to be inefficient, and not always accurate, so I thought of cutting the middleman(Database) and directly ask the sessionRegistry if this username is online. Unfortunately I cannot find any methods to check that in SessionRegistry. Any help would be nice.



@Scheduled(fixedRate = 600000)@Override
public void listUsers() {
List < Object > principals = sessionRegistry.getAllPrincipals();
List < String > stringList = new ArrayList < > ();
for (Object principal: principals) {
if (principal instanceof User) {
for (SessionInformation sess: sessionRegistry.getAllSessions(principal, false)) {
stringList.add(((User) sess.getPrincipal()).getUsername());
}
}
}
this.onlineUsersDAO.deleteAllOnlineEntries();
for (String string: stringList) {
this.onlineUsersDAO.addOnlineUser(string);
}
stringList.clear();
}


After formatting, as you can see above, I am adding the users in a StringList, and later I am persisting that in database, but I want something like :



boolean doesUserExist = sessionRegistry.checkIfEmailExists(String email);


Kindly let me know what I can do. Thanks a lot. :-)


Aucun commentaire:

Enregistrer un commentaire