Skip to main content

How to call OSGI service from managed bean?

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
14 replies [Last post]
rcbandit
Offline
Joined: 2011-07-19

Hi,

Is it possible to call an OSGI service from a managed bean? I found some examples on internet how to call OSGI service from Java Servlet but this doesn't solve my problem. I get NPE when I try to call the OSGI service from managed bean. Is there any example how to do this?

Regards

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ss141213
Offline
Joined: 2005-03-30

Use OSGi/CDI bridge to call OSGi service from your managed bean. I have already sent you a pointer to the doc which talks about OSGi/CDI bridge.

- Sahoo

rcbandit
Offline
Joined: 2011-07-19

I follow the links and I tried to add metadata to the web application. Here is the source code:

http://www.2shared.com/file/GneOI9ry/NetBeansProjects.html

I'm still getting the same error - NPE after I deploy the web module as OSGI bundle. I modified the POM file of the web module (SR_57) that I'm developing. I use Netbeans 7.1. It seems that manifest file is generated by Netbeans during the build process. I can't think of any more options.

This is the error stack from glassfish

http://pastebin.com/Ev67fgdv

Would someone help me to find my mistake?

Best wishes

Peter

ss141213
Offline
Joined: 2005-03-30

There are two issues with your app:

a) in line #54 of userCheck.java, you have:

String test_var = this.hello.sayHello("Duke");

This will always cause NPE, as this code gets executed by constructor and this.hello would never have been injected by then. 

b) I don't see any beans.xml, so CDI is not enabled for your app. Add an empty WEB-INF/beans.xml.

By the way, since you are using NetBeans to do OSGi development, I suggest you take a look at our video tutorial if you have not:

http://blogs.sun.com/arungupta/entry/screencast_32_osgi_enabled_java

HTH,

Sahoo

rcbandit
Offline
Joined: 2011-07-19

Thank you for the fast reply!

a) I added beans.xml file. Now this appears

HTTP Status 404 -


type Status report

message

descriptionThe requested resource () is not available.

Maybe I need to add something else to the beans.xml file?

b)

What is the proper way to call method into OSGI bundle?

this.hello.sayHello("Duke")

Best wishes, Peter

ss141213
Offline
Joined: 2005-03-30

No, you don't need anything special in beans.xml.

I don't understand what you mean by "What is the proper way to call method into OSGI bundle?" There is nothing special about calling an OSGi service injected via CDI. You are trying to use the injected field when the instance containing the injected field is still under construction, so you are getting NPE. Try using that injected field from a method that gets called when your web app is processing a request.

Sahoo

rcbandit
Offline
Joined: 2011-07-19

a) maybe I'm missing something into the POM file. If I remove beans.xml I can load the JSF page.

b) Yes you're right. If I place the "hello.sayHello("Duke")" into a method which is called during request processing then I get NPE.

Maybe first I have to solve the problem with the beans.xml file. The the second problem will be fixed.

p.s now I get this error then I try to access the page

http://pastebin.com/3KX5ir90

ss141213
Offline
Joined: 2005-03-30

Why are you complicating your life? Take simple steps. First build a simple OSGi web app involving a Servlet and html page perhaps. Then introduce CDI followed by an OSGi service injected via CDI. Then you could add JSF to make a better interface. You can then validate your understanding at each step.

Sahoo

rcbandit
Offline
Joined: 2011-07-19

Unfortunatly I'm in a hurry this time. I need urgent help to solve this problem in order to solve this problem. Right now I'm having a big problem with this issue and I can't continue the project that I'm working on. Please Sahoo if you can help me I cannot continue the project without this working example.

Here is the latest development code and error stack.

http://www.2shared.com/file/6PNtf2mH/NetBeansProjects.html

http://pastebin.com/3KX5ir90

Best wishes,

Peter

p.s by the way I'm sure that this example is interested to many people. I will be glad if you publish it into your blog.

rcbandit
Offline
Joined: 2011-07-19

?????

I removed "@SessionScoped" and everything work very well. Is this a bug?

ss141213
Offline
Joined: 2005-03-30

I have asked our CDI expert to tell you if @SessionScope has any role to
play. In the meanwhile, I guess you can continue now that you have
things working.

Sahoo
On Thursday 19 January 2012 09:13 PM, forums@java.net wrote:
> ?????
>
> I removed "@SessionScoped" and everything work very well. Is this a bug?
>
>
> --
>
> [Message sent by forum member 'rcbandit']
>
> View Post: http://forums.java.net/node/882760
>
>

rcbandit
Offline
Joined: 2011-07-19

Hi Sahoo,

Are there any news from your CDI expert? I hope that you haven't forgot this problem.

Best Wishes,

Peter

rcbandit
Offline
Joined: 2011-07-19

Hi,

I fixed the problem. I added "transient" in order to disable serialization into the OSGI part.

@Inject @OSGiService(dynamic=true) transient SessionHandle hello;

Problem is solved.

Sivakumar Thyag...
Offline
Joined: 2012-01-04

Hi

On Monday 23 January 2012 07:17 PM, forums@java.net wrote:
> Hi,
>
> I fixed the problem. I added "transient" in order to disable
> serialization into the OSGI part.
>
> @Inject @OSGiService(dynamic=true) transient SessionHandle hello;
>
> Problem is solved.

Sorry for the delay. In your original error trace, there was this
following error:
> Caused by: org.jboss.weld.exceptions.UnserializableDependencyException: WELD-001413 The bean Managed Bean [class com.DX_57.SR_57.userCheck] with qualifiers [@Any @Default @Named] declares passivating scope but has non-serializable dependency org.glassfish.osgicdi.impl.OSGiServiceExtension$OSGiServiceBean@4d0dcfad
> at org.jboss.weld.bootstrap.Validator.validateInjectionPointPassivationCapable(Validator.java:328)
> at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:290)
> at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)

Since you are using a dynamic reference to the service, marking it as a
transient field in the SessionScoped Bean makes the Bean passivatable
and must help resolve your issue.

Thanks
--Siva.

>
>
> --
>
> [Message sent by forum member 'rcbandit']
>
> View Post: http://forums.java.net/node/882760
>
>

rcbandit
Offline
Joined: 2011-07-19

Ok, thank you!