RWT.getApplicationContext() in non-UI thread [message #1004225] |
Tue, 22 January 2013 20:46 |
Benjamin Wolff Messages: 136 Registered: July 2009 |
Senior Member |
|
|
Hi,
I tried to migrate to the new ApplicationContext store, which is available since 2.0 M4, for storing application-wide settings. Since it works with the servlet context, it should be available in any thread of the application. However, if I access RWT.getApplicationContext() from a different thread, i.e. a servlet or the bundle activator, I get an "invalid thread access" exception.
After having a quick look into the code, the context is set and kept in a ThreadLocal of the UI-Thread. I was hoping that I can use this context to use as a central application storage, but if it is only set in the UI thread I apparently can't use it for this.
As I understand the context is basically just the servlet context under the hood. Can't this context be set in an application-scoped variable that is initialized in a servlet context listener, thus being available in any thread (non-UI)?
What do you think?
Cheers,
Ben
[Updated on: Tue, 22 January 2013 20:47] Report message to a moderator
|
|
|
Re: RWT.getApplicationContext() in non-UI thread [message #1004443 is a reply to message #1004225] |
Wed, 23 January 2013 08:39 |
|
Hi Ben,
there can be more than one ApplicationContext, e.g. when two RAP
applications are running in different web applications. The workbench is
an exception to this rule, due to their singleton nature (one extension
registry, static code, etc.) there can only be one single instance.
Therefore the application context cannot be kept statically, you need to
access the correct one for your current session.
To access the application context from a non-UI thread, you need a
reference to the current UISession. If you don't have it, but know your
display, you can get the UISession for the display:
UISession uiSession = RWT.getUISession( display );
At the moment, you would wrap your code in a session runnable:
uiSession.exec( new Runnable() {
public void run() {
ApplicationContext context = RWT.getApplicationContext();
}
} );
But we could also provide an accessor method on UISession to allow for
the following code:
ApplicationContext context = uiSession.getApplicationContext();
Would this be helpful?
Regards,
Ralf
--
Ralf Sternberg
Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/
Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
|
|
|
|
Re: RWT.getApplicationContext() in non-UI thread [message #1005227 is a reply to message #1004806] |
Thu, 24 January 2013 17:14 |
|
Hi Ben,
I'm sure you know it, but let me point it out once again anyway:
application scope != global scope
Maybe it does not make a difference for your deployment, but once you
deploy another RAP application on the same server, you'll have two
different application scopes, but only one instance of a static map.
Since an arbitrary background thread does not belong to any specific
application, you need a reference to the UISession or the
ApplicationContext to access the *correct* application context.
Cheers, Ralf
--
Ralf Sternberg
Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/
Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
|
|
|
Powered by
FUDForum. Page generated in 0.02926 seconds