-
1. Re: Change entity on session bean timeout
kragoth Feb 25, 2010 12:56 AM (in response to janhh)Why not just use an Application Scoped bean that registers which user has selected which entity (and in which session if you support multiple windows).
Then cleaning up is easy and you are not polluting your domain entities with application logic.
Obviously all the methods in the Application scoped bean should be synchronized to avoid two people getting access to the same entity.
This is how I have solved the same basic problem in my app.
-
2. Re: Change entity on session bean timeout
janhh Feb 25, 2010 5:01 AM (in response to janhh)how does the application scoped bean know when a user has a session timeout?
-
3. Re: Change entity on session bean timeout
kragoth Feb 25, 2010 5:47 AM (in response to janhh)Well, I guess I did leave out the step of how to work that out :)
All you need to do is create a session listener.
I just looked at my code, I didn't use an application scoped bean.. well, I did but then decided to just make it a class with static variables/methods.So this is my session listener.
public class GekkoSessionListener implements HttpSessionListener { private static final Logger log = LogManager.getLogger(GekkoSessionListener.class); @Override public void sessionCreated(HttpSessionEvent e) { log.trace("Session: " + e.getSession().getId() + " created"); } @Override public void sessionDestroyed(HttpSessionEvent e) { log.trace("Session: " + e.getSession().getId() + " destroyed"); UserSessionCertificateStore.removeHttpSession(e.getSession()); } }
Obviously the line
UserSessionCertificateStore.removeHttpSession(e.getSession());
is the important one. That is where I remove the session from my map of who is editing which entity (Certificate in my case).
Then all you need to do is create the class that will hold the value of which user has which entity.
So something like (THIS IS JUST MADE UP AND PROBABLY DOESN'T WORK, BUT IT IS THE GENERAL IDEA)
public final class UserEntityStore { private static final Map<User, Entity> userEntityMap = new Map...; private static final Map<HttpSession, User> userSessionMap = new Map...; //Call this method on a successful login public synchronized static void setupSession(User user, HttpSession session) { userSessionMap.put(session, user); } public synchronized static void setEntityForUser(Entity e, User u) { if (isEntityInUseByOtherUser(e, u)) { //Raise error about not being able to edit this entity because another user has it. return; } userEntityMap.put(u, e); } public boolean isEntityInUseByOtherUser(Entity e, User u) { //Search the map for the entity and return true if found and in use by a different user //else return false; } public void sessionTimeout(HttpSession session) { User u = userSessionMap.get(session); userEntityMap.remove(u); userSessionMap.remove(session); } }
I hope this helps. :)
-
4. Re: Change entity on session bean timeout
kragoth Feb 25, 2010 5:49 AM (in response to janhh)Grr, all those methods should be synchronized OK :)
Well, actually. I'm not really up to speed on the 'correct' usage of synchronized but that's the general idea. Only 1 thread at a time should execute those lines that access those maps.
-
5. Re: Change entity on session bean timeout
janhh Feb 26, 2010 1:53 AM (in response to janhh)Thanks but what you do seems somehow weird to me. It might work, but I guess it is not the default way how this should be done. Tracking which user is editing which entity with a map just to do some cleanup on session timeout cannot be The Right Way, I guess.