0

ZK Session-backed registry

asked 2011-08-18 03:45:17 +0800

avidD gravatar image avidD
166 2

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

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2011-08-18 03:55:07 +0800

avidD gravatar image avidD
166 2

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

link publish delete flag offensive edit

answered 2011-08-21 23:49:14 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi David,
When sessionCreated ZK Session doesn't exist.

link publish delete flag offensive edit

answered 2011-08-22 11:03:44 +0800

avidD gravatar image avidD
166 2

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

link publish delete flag offensive edit
link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-08-18 03:45:17 +0800

Seen: 526 times

Last updated: Aug 31 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More