Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Howto access backend services from RAP application?(Which is the "pattern" to follow in order to use backend services from RAP?)
Howto access backend services from RAP application? [message #553936] Thu, 19 August 2010 13:27 Go to next message
Patrick is currently offline PatrickFriend
Messages: 17
Registered: August 2010
Location: Rome
Junior Member
Hi All,

I'm new to eclipse/rap development and I some (maybe silly) questions that I would ask to this community of experts.

I'm able to access a backend JEE application deployed on WebSphere from an OSGi bundle. Using the declarative services (DS) approach, I have exposed a IBackendService, whose EJB implementation performs the necessary JNDI lookup and returns a reference to the remote interface of the backend bean. The IBakendService is consumed by another OSGi bundle again using DS and everithing works fine.

Which is the best/only way to access the IBackendService from a RAP application? where and when should it be registered? For instance, if there is a view that must show a list of items returned by the backend service, how can I get reference to the backend service (through the OSGi bundle that provides the IBackendService) and obtain the items to feed the table?

Thanks a lot for your help and suggestions

Patrick
Re: Howto access backend services from RAP application? [message #553939 is a reply to message #553936] Thu, 19 August 2010 13:36 Go to previous messageGo to next message
Ashwani Kr Sharma is currently offline Ashwani Kr SharmaFriend
Messages: 119
Registered: July 2009
Location: Bangalore, India
Senior Member

Hi,

This FAQ describes how to use EJBs in RAP application:
http://wiki.eclipse.org/RAP/FAQ#How_do_I_make_remote_calls_t o_EJB.27s_deployed_on_Jboss_4.x_AS

Regards,
Ashwani Kr Sharma
Re: Howto access backend services from RAP application? [message #553943 is a reply to message #553939] Thu, 19 August 2010 13:44 Go to previous messageGo to next message
Patrick is currently offline PatrickFriend
Messages: 17
Registered: August 2010
Location: Rome
Junior Member
Thanks for the pointer, but my question is not related to remote access the backend service, I'm already able to do this from an OSGi bundle. The question is about the way a RAP application should consume the backend service. Should I register it someway or there is some technique to get a reference to the OSGi service which delegates to the remote EJB service ?
In a traditional JSF Web application I would have injected the backend service using a @EJB annotation directly in the backing bean, how should this be done i RAP ?

Thnks again for your help

Patrick
Re: Howto access backend services from RAP application? [message #553997 is a reply to message #553943] Thu, 19 August 2010 15:44 Go to previous messageGo to next message
Cole Markham is currently offline Cole MarkhamFriend
Messages: 150
Registered: July 2009
Location: College Station, TX
Senior Member

Patrick,

You can access your IBackendService through your view bundle's activator. Here is a good tuturial:
http://www.vogella.de/articles/OSGi/article.html#osgiservice _useservice

Cole
Re: Howto access backend services from RAP application? [message #554000 is a reply to message #553997] Thu, 19 August 2010 15:57 Go to previous messageGo to next message
Patrick is currently offline PatrickFriend
Messages: 17
Registered: August 2010
Location: Rome
Junior Member
i Cole,

this is the tutorial I followed to get things work in the OSGi domain, now I would use the same approach (based on DS) to inject the service into an application View, that shoul play the role of the class QuoteConsumer in the Vogella tutorial, is it possible ? I tried but no luck Sad

Thanks a lot

Patrick
Re: Howto access backend services from RAP application? [message #554014 is a reply to message #554000] Thu, 19 August 2010 16:48 Go to previous messageGo to next message
Patrick is currently offline PatrickFriend
Messages: 17
Registered: August 2010
Location: Rome
Junior Member
I lastly found a way to get a reference to the IBackendServce provided by an OSGi component from a RAP application,
but I don't know if this is the best way to go. Here are de details:

In the UI bundle I registered a factory of the IBackendService using the extension point org.eclipse.ui.services
The factory lookups the service from the BundleContext returned by the FrameworkUtil.getBundle(IBackendService.class)
utility method and returns the service:

public class GroupServiceFactory extends AbstractServiceFactory {

@Override
public Object create(Class serviceInterface, IServiceLocator parentLocator, IServiceLocator locator) {
if (!IGroupService.class.equals(serviceInterface)) {
throw new IllegalArgumentException("Unsupported service interface: " + serviceInterface"); +
}
BundleContext ctx = FrameworkUtil.getBundle(IBackendService.class).getBundleCont ext();
ServiceReference sr = ctx.getServiceReference(IBackendService.class.getName());
IBackendService svc = (IBackendService) ctx.getService(sr);
if(svc == null) {
throw new IllegalStateException("Backend Service is not available!");
}
return svc;
}

}

The service lookup is performed only the first time the factory is called by the framework; after that its reference is
cached in the service registry.

In the UI bundle I access the service as follow:

IBackendService svc = (IBackendService)getSite().getService(IBackendService.class) ;

Is this a valid approach? what about cleaning up things when deactivating bundles? should I provide some lifecycle listener
to manage this? I would prefer to use DS to inject the service, but it seems to not work in RAP applications... or more probably
I'm missing something, but what ?

Thanks again for your help

Regards,
Patrick



Re: Howto access backend services from RAP application? [message #554016 is a reply to message #554014] Thu, 19 August 2010 17:00 Go to previous messageGo to next message
Patrick is currently offline PatrickFriend
Messages: 17
Registered: August 2010
Location: Rome
Junior Member
Few minutes ago I found this interesting post

http://www.eclipse.org/forums/index.php?t=msg&goto=54761 5&

it exposes my same problem but in the context of a RCP application, so it uses the bundle activator as a single point of access to provide access to the backend service obtained from an OSGi component.

Is this approach possible also for RAP just replacing the activator with a session-scoped singleton obtained from SessionSingletonBase ?

Thanks again for any suggestion

Patrick
Re: Howto access backend services from RAP application? [message #554020 is a reply to message #554016] Thu, 19 August 2010 17:44 Go to previous messageGo to next message
Cole Markham is currently offline Cole MarkhamFriend
Messages: 150
Registered: July 2009
Location: College Station, TX
Senior Member

I think the workbench service idea looks sound. If you environment is dynamic, then you shouldn't cache the service and should call unget() on the service reference when you are done using it. Unless the backend service will be stopped and restarted I don't see this as an issue, we do things like it quite often because we know the system will only be restarted as a whole.

You don't need to use a session singleton, because it is a backend service that is shared by all the sessions anyway.

In our systems, we have a wrapper around a service tracker that we use and always go through the activator for access to osgi services. So we have a method in our base activator that lets you get a service like:
IBackendService service = Activator.getDefault().getService(IBackendService.class);

The code for the getService method is very similar to the examples from the tutorial in sections 6.6 and 6.7.

What I really like about your solution, however, is that your view doesn't know about the activator or even OSGi. It just grabs the service from it's view site. Very clean.

Cole
Re: Howto access backend services from RAP application? [message #554022 is a reply to message #554014] Thu, 19 August 2010 18:00 Go to previous messageGo to next message
Cole Markham is currently offline Cole MarkhamFriend
Messages: 150
Registered: July 2009
Location: College Station, TX
Senior Member

Patrick,

I just noticed your are using FrameworkUtil.getBundle with the class from the backend service. You probably want to use a class from your UI bundle since that is where you are using the service. In you example you could just use FrameworkUtil.getBundle(getClass()) and that should work fine. The reason is that the ServiceReference is tracking which bundles are using the service so you want that data to be accurate. Again, it doesn't really come into play unless you system dynamically starts and stops bundles.

Cole
Re: Howto access backend services from RAP application? [message #554602 is a reply to message #554014] Mon, 23 August 2010 16:32 Go to previous messageGo to next message
Paul Bilnoski is currently offline Paul BilnoskiFriend
Messages: 28
Registered: August 2010
Junior Member
Patrick wrote on Thu, 19 August 2010 12:48


The service lookup is performed only the first time the factory is called by the framework; after that its reference is
cached in the service registry.




This is not entirely correct.
Take a more careful look at the documentation here: http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/wrkAdv_services.htm

Workbench services are inherently localized and hierarchical, which comes with several implications.

Some features of workbench services as opposed to OSGi services are:

  • Services that support listeners get automatic listener registration cleanup
  • Services can provide more localized support depending on their context
  • Services are used in the scope of a context, and as such will be disposed when the context is disposed


What all this means is that your service factory will return a cached service, but only for access within the same service context. Each new view part gets its own new context.

The good part is that when you add listeners to a service handle you retrieved from your view's service locator, they will be unregistered when the view is disposed because the service locator is disposed and all associated services.

The bad part is that each unique context will make a new call to the service factory, so it must be implemented to handle this. Another bad part is that if your service is org.eclipse.ui.services.IDisposable and you return the same instance for each call to your service factory, the first context that goes away will dispose the service and the other references to it will be invalid.

Here are some things to consider when implementing your service factory to return a workbench service backed by an OSGi service:
If you always request services by using the root service locator, i.e. the workbench itself, you should only get a single instance of each service, so that may be a possible workaround: getSite().getWorkbenchWindow().getWorkbench().getService(*) instead of getSite().getService(*)
If you use each site's service locator, you will need to either limit the implementation of the service itself (no dispose/lifecycle, no instance listener registration, no localized service abilities) or implement your workbench service as a proxy to appropriately wrap the service access in a lifecycle for the workbench services framework. See the above link to the eclipse documentation for an example of an OSGi-workbench service proxy.

--Paul
Re: Howto access backend services from RAP application? [message #652088 is a reply to message #553936] Wed, 02 February 2011 16:25 Go to previous message
Bernd Meier is currently offline Bernd MeierFriend
Messages: 4
Registered: February 2011
Junior Member
i got the similiar problem.

@ patrick, i tried your "workaround" and it seems to work until now...


i try to implement the sniped code for future applications.
will give you feedback after some time.

thanks for your solution

and please do not worry about my english....



Tickets für aktuelle Events oder Tour Konzerte gibt es bei Tour Tournee

[Updated on: Sun, 20 February 2011 16:08]

Report message to a moderator

Previous Topic:Window.defaultImages should to be in session scope
Next Topic:[ANN] RAP 1.4 M1 is available
Goto Forum:
  


Current Time: Tue Feb 20 23:46:23 GMT 2024

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

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

Back to the top