File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes OSGi + Glassfish + JEE 6 howto? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "OSGi + Glassfish + JEE 6 howto?" Watch "OSGi + Glassfish + JEE 6 howto?" New topic
Author

OSGi + Glassfish + JEE 6 howto?

Szymon Pietrzak
Greenhorn

Joined: Apr 27, 2011
Posts: 6
I am somewhat new to using pure JEE without Spring framework or dm/Virgo server. I've worked with OSGi using Spring framework and their XML based OSGi services/references declarations.

Is there any way to configure OSGi services in the same manner using pure JEE 6 on Glassfish?

I've found some examples of modular projects using beans from each other's context, but it was done directly in the code via references to container classes and resources, which I consider rather crude and unaesthetic solution.

I'd be grateful for any help and examples of such configuration.
Sanjeeb Sahoo
Greenhorn

Joined: Apr 27, 2011
Posts: 3
GlassFish supports OSGi bundle deployment and various OSGi enterprise technologies. So not only GlassFish allows one to use OSGi APIs, it also allows one to use OSGi and Java EE together. Check out
http://wikis.sun.com/display/GlassFish/BlogsGfOsgi

Feel free to send questions to users(at)glassfish.java.net

-- Sahoo
Szymon Pietrzak
Greenhorn

Joined: Apr 27, 2011
Posts: 6
It is true, but what I was thinking about was something similar to Spring OSGi context declaration, which allows you to declare beans, OSGi services and import them from the context of one module bundle to another.

Here the import seems easy using JEE dependency injection and annotations, but you still have to declare bundle activator and register your services in code, not in config file. I was wandering whether or not such way of bundle services declaration (one XML config file) is supported by JEE and whether or not it is possible to avoid declaring them in code.

BTW. great video tutorial, clarifies a lot!
Sanjeeb Sahoo
Greenhorn

Joined: Apr 27, 2011
Posts: 3
We do support various forms of declarative services. e.g., GlassFish includes an implementation of OSGi Declarative Service implementation (aka Service Component Runtime) which one can use to declaratively register services in OSGi service registry. GlassFish also supports an interesting concept called EJB based OSGi service. This is again a form of declarative service where in user does not have to explicitly register their services. User develops regular stateless EJBs with locan business interfaces makes them part of an OSGi bundle. GlassFish will automatically register those EJBs as OSGi services for consumption from any other OSGi bundle context using whichever means user choses to use. Look for EJB/OSGi support in that Wiki - you shall find some examples. The advantage of using an EJB based OSGi service is that you have access to a rich set of Java EE APIs as well as features like declarative transaction, security, dependency injection available to your OSGi service!

GlassFish also makes available Java EE resources like JDBC DataSources, JMS Queues and Topics as OSGi Services.
Szymon Pietrzak
Greenhorn

Joined: Apr 27, 2011
Posts: 6
I can see multitude of examples, but still have some questions:

1. Can an EJB be registered as OSGi service without use of BundleActivator and direct call to OSGi APIs (for instance via annotation)? From your reply I understand that each EJB is automatically registered as OSGi service on GlassFish - am I correct?

2. Can an OSGi service which is EJB be referred to from another module only via use of annotations (@Injected @OSGiService) or do I have to create BundleActivator and use ServiceTracked to find the service in the context?
Sanjeeb Sahoo
Greenhorn

Joined: Apr 27, 2011
Posts: 3
Szymon Pietrzak wrote:I can see multitude of examples, but still have some questions:

1. Can an EJB be registered as OSGi service without use of BundleActivator and direct call to OSGi APIs (for instance via annotation)? From your reply I understand that each EJB is automatically registered as OSGi service on GlassFish - am I correct?

2. Can an OSGi service which is EJB be referred to from another module only via use of annotations (@Injected @OSGiService) or do I have to create BundleActivator and use ServiceTracked to find the service in the context?


Answer to first question is YES - that's why I call it one kind of declarative service. To use this automatics registration feature, you have to package your EJBs in an OSGi bundle - you can basically turn your existing ejb-jar to an OSGi bundle. You then have to add a manifest entry called Export-EJB which can have values like "ALL" to indicate export all EJBs or comma separated names of EJBs that need to be exported. Currently the implementation supports only registration of Statelss or Singleton EJBs with local business interfaces, i.e, you can't export EJBs with remote interfaces or stateful EJBs.

Answer to second question is also yes. Once the EJB is exported as a service, you can consume it however you like including but not limited to via @Inject @OSGiService.

Please use users@glassfish.java.net while starting any new thread - you will get faster responses as well as responses from other users.

Thanks,
Sahoo
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

Just curious to know why do you need to run JEE and OSGI together. I believe JEE 6 is more advanced and support same modularity which OSGI supports.

-Jignesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OSGi + Glassfish + JEE 6 howto?