Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » RWT.getApplicationContext() in non-UI thread
RWT.getApplicationContext() in non-UI thread [message #1004225] Tue, 22 January 2013 20:46 Go to next message
Benjamin Wolff is currently offline Benjamin WolffFriend
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 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

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 #1004806 is a reply to message #1004443] Wed, 23 January 2013 22:43 Go to previous messageGo to next message
Benjamin Wolff is currently offline Benjamin WolffFriend
Messages: 136
Registered: July 2009
Senior Member
Thanks for the clarification!

It seems that I got the concept of the application context wrong. I was looking for a nice and ready-to-use application-scoped storage that I can also use in background threads and servlets. The servlet context would be accessible from the UI thread and servlet/serviceHandler, since there is an ongoing request. However, background threads (Quartz scheduler) won't have that.

Currently I'm using the org.eclipse.core.runtime.preferences.InstanceScope class that comes with the workbench to achieve that. This mechanism works nice, but requires the workbench. Another option would be a simple home-brewed implementation that works with a static and synchronized map...

Cheers,
Ben
Re: RWT.getApplicationContext() in non-UI thread [message #1005227 is a reply to message #1004806] Thu, 24 January 2013 17:14 Go to previous message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1313
Registered: July 2009
Senior Member

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/
Previous Topic:AbstractEntryPoint and the workbench
Next Topic:Replacing javax.servlet Dependency
Goto Forum:
  


Current Time: Fri Apr 26 14:13:34 GMT 2024

Powered by FUDForum. Page generated in 0.02926 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top