|
Re: SessionSingletonBase [message #72362 is a reply to message #72289] |
Wed, 30 January 2008 11:36 |
martin Messages: 12 Registered: July 2009 |
Junior Member |
|
|
Hi,
with SessionSingletonBase you can provide your Objects on a per-Session
basis.
If you implement a class using the Singleton-Pattern (private Constructor,
static getInstance()-Method to retrieve sole instance), all Usersessions
within an Application access the same Object. This is not always desirable
(think of the UIWorkbench of you RAP-App.).
So the wonderful Developers of RAP ;) created a possibility to overcome
this problem. To provide your Objects per Session, you can extend
SessionSingletonBase and access the "SessionSingleton" with the Method
getInstance(class type).
Let me give you an example.
You have a Class MySingleton implemented like this:
public class MySingleton{
private static MySingleton instance = new MySingleton;
private MySingleton(){
//you cannot acces me
}
public static MySingleton getInstance(){
return instance;
}
}
This is a typically implemented Singleton which resides in the
"Application Scope", so all Usersessions use this single on instance
returned by getInstance().
If you do want to provide this instance per session, use
SessionSingletonBase.
This could look somewhat like this:
public class MySingleton extends SessionSingletonBase{
//note: no instance has to be created by yourself
//private static MySingleton instance = new MySingleton;
private MySingleton(){
//you cannot acces me
}
public static MySingleton getInstance(){
//is calling the Method getInstance(Class type) of SessionSingletonBase
//no change to Interface needed
return getInstance(MySingleton.class);
}
}
So now, if several UserSessions access this Singleton, every single
Session has its own Instance of this class. Internally, the Instances are
stored in the particular HTTPSessions.
This makes it a litte difficult, to access this instance, because
normally, you can only access Data in a Usersessions during eradication of
a Client-Request. If you try to access the instance eg. directly from a
Serverthread, youŽll receive a IllegalStateException.
AFAIK there are 2 main possibilities to overcome this, first: just load
the Data in the Session in time and cache it, so that you can use it later
, or take a look at the Method runNonUIThreadWithFakeContext in the Class
UICallback. Within this Method, a Dummy-Request is created and access to a
Session is provided, very well done.
Well, i had no time to check this code, I hope everything is ok, if not,
someone will certainly correct me:-)
Have fun with RAP
Martin
|
|
|
Powered by
FUDForum. Page generated in 0.02270 seconds