-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
I want to use a singleton registry that manages process-scoped and session-scoped attributes. The purpose of this registry is to avoid explicitly using session attributes scattered all over the application. It would give me typed properties instead of having to cast everywhere and I would not have to use String attribute keys all over the code.
I have an abstract registry class that defines the properties and I have an implementation ZKSessionRegistry that ought to be backed by ZK sessions. However, this unfortunately does not work as expected. Here comes the problem:
I must initialize my registry with session scoped properties when a new session is created. This includes, for instance, a lock manager. I tried to do this initialization using two listeners which both did not work. Here are the two alternative listeners:
public class SessionInitListener implements SessionInit {
public void init(Session session, Object request) throws Exception {
Registry registry = (Registry)Registry.instance();
registry.setLockManager(
new SessionLockManager(((javax.servlet.http.HttpSession)session.getNativeSession()).getId(), LockManager.instance()));
}
}
public class SessionListener extends HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
LOG.info("Session created, registering lock manager.");
Registry registry = (Registry)Registry.instance();
registry.setLockManager(
new SessionLockManager(event.getSession().getId(), LockManager.instance()));
}
...
}
And this is part of the ZKSessionRegistry:
...
public void setLockManager(SessionLockManager lockManager) {
LOG.debug("Set lock manager for session: " + Sessions.getCurrent(true));
Sessions.getCurrent(true).setAttribute(SES_ATT_LOCK_MANAGER, lockManager);
}
It turns out that in both cases, the servlet-API session listener and the ZK SessionInit, the ZK session does not exist at the time of firing the listener. Unfortunately, I could not find any hint (by keyword search) in the ZK documentation about the session lifecycle. Is there any listener which fires _after_ the session is created?
Regards and thanks beforehand,
David
Hm, a second look at my code showed me one solution. I introduced a new setter method into the registry:
public void setLockManager(Session session, SessionLockManager lockManager) {
LOG.debug("Set lock manager for session: " + Sessions.getCurrent(true));
session.setAttribute(SES_ATT_LOCK_MANAGER, lockManager);
}
So, the ZK session already exists but Sessions.getCurrent(true) does not know about it. While the code now works, it is still a bit unsatisfactory as I don't know how exactly the session life cycle and the events are designed. A state- or interactivity-diagram for sessions and requests and the associated events would be nice.
Greetings,
David
Hi David,
When sessionCreated ZK Session doesn't exist.
Hello Jimmy,
thanks. I noticed that and designed the interface of my registry such that I have to hand in the session to set the session attribute. Although it's working now I wonder whether there is an event that tells me that the ZK session has been created and is completely set up including setting it in the Sessions object. Also, I wonder: is the ZK session set when I receive an HttpRequest? Maybe there is some lifecycle diagram in the documentation?
Greetings and thanks beforehand,
David
Hi,
We have not a diagram talk about this,
we just mention
http://books.zkoss.org/wiki/ZK_Developer%27s_Guide/Fundamental_ZK/ZK_User_Interface_Markup_Language/Implicit_Objects/Life_cycle_of_page,_execution,_desktop,_session,_application
Asked: 2011-08-18 03:45:17 +0800
Seen: 526 times
Last updated: Aug 31 '11