Search |
||
Developing Hybrid (OSGi + Java EE) applications in GlassFishPosted by ss141213 on June 14, 2009 at 1:02 AM PDT
(If you are in a hurry, then download the sample and jump to "build and test" section after doing the necessary set up.) In my last blog, I mentioned about adding a new module in GlassFish called "OSGi web container for GlassFish." I am glad to say that, it is fully functional now. I have been able to use JSPs, JSFs, Servlets (of course), annotations, resource injection, transactions, EJBs, etc. in a hybrid application (more on hybrid application below). If you noticed, I mentioned EJBs. Although I call it "OSGi web container," since GlassFish now supports "EJB inside war" as standardized by Java EE 6, the new container allows you to use both Web and EJB components in a hybrid application. What exactly is a hybrid application? I call an application that uses both Java EE & OSGi a hybrid application. Both the technologies have their strengths. While Java EE excels when it comes to security, transaction management, rich component model, persistence, resource pooling, messaging, etc., OSGi brings modularity, dynamism, life cycle management to the table. They make a very powerful combination. Since their combined use is a new development, to make things easier, I shall walk you through the steps of developing and deploying hybrid applications in GlassFish v3. Setting the stage: java -jar glassfishv3/glassfish/modules/glassfish.jar or asadmin start-domain (You need Java SE 6 to run GlassFish v3) 2. Start embedded JavaDB by running glassfish/bin/asadmin start-database
3. Download "OSGi web container" bundle from here. Sample Application Download the complete sample from here and unzip it. Those who don't want to build, the final war file is inside hybridapp1/target folder of the zip file. Let's look at the relevant details: 1. pom.xml 2. Java EE artifacts 3. Activator.java - the OSGi bundle activator. Build and test That's it. If your server is running, you will see the OSGi bundle getting installed and started as part of which the web application gets deployed. If it is not running, start it for this to happen. You can manage the life cycle of the OSGi bundle by file system commands like cp, rm, touch, etc. See an earlier blog on this. Now that the application is deployed, you can point your browser to http://localhost:8080/hybridapp1 Follow the registration link and then login link. You can see the server.log to see that user name and password got stored in database. Now, let's delete hybridapp1.jar from autodeploy-bundles folder. As soon as you do that, server's output will show that the bundle got uninstalled and the application got undeployed as well. Now if you try to visit any of the aforementioned URLs, you will get a 404 error. Relationship between life cycle of OSGi bundle and Java EE Application The actual state transition of the bundle looks like this: The sequence of events is like this: Understanding bundle life cycle and deployment sequence I will use the former one as I prefer command line tools. telnet localhost 6666 ps install file:///tmp/hybridapp1/target/hybridapp1.war start I hope this is sufficient for you to get started. Now, let your imagination take over and let us know how you are able to take advantage of this hybrid approach. In my next article, I show how you can use an EJB as an OSGi service.As always, your feedback is very welcome. Please visit The Aquarium to know the latest news about GlassFish. If you have questions about how to use this feature, please use our forum. Thank you. »
Related Topics >>
Java Enterprise Comments
Comments are listed in date ascending order (oldest first)
Submitted by splitshade on Sun, 2009-07-12 04:38.
Hi Sahoo,
thats quite interesting, I tried do deploy your WAB, but I can´t get it working, what I get is
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
So, what to do?
By the way, is there Code available for the OSGI-Web-Container-Bundle?
Submitted by ss141213 on Mon, 2009-07-13 06:47.
splitshade:
I tried deploying the WAB in my environment and it worked. Which build of GlassFish v3 are you using? Can you send the complete stack trace?
Yes, the sources are available. They are in svn: https://svn.dev.java.net/svn/glassfish-svn/v3/web/osgi-web-container
Please try to ask questions in our user forum at users(at)glassfish(dot)dev(dot)java(dot)net. That way, more users can get the benefit from your effort.
Submitted by ss141213 on Wed, 2009-07-15 00:11.
splitshade:
I was wrong. I was using a slightly old build where things were working. When I switched to latest build as of 20090714, I did see an exception while trying to look up something in JNDI, but it was caused by a weired bug in jdbc module which was not able to look up an internal resource and hence datasource lookup failed. It has been fixed and if you can wait for b55 to come out, then things will be back to normal.
Thanks.
Sahoo
|
||
|
|