Search |
||
Our second hybrid application - EJB as OSGi ServicePosted by ss141213 on June 16, 2009 at 1:02 AM PDT
Last time, we talked about our first hybrid (Java EE + OSGi) application where we discussed not only about development of hybrid applications including kind of maven plugins to use, deployment steps, etc., but also about the lifecycle of OSGi bundle and Java EE artifacts. Now, it's time for our second hybrid application which demonstrates an EJB as an OSGi service. Set up java -jar glassfishv3/glassfish/modules/glassfish.jar 2. Start embedded JavaDB by running glassfishv3/glassfish/bin/asadmin start-database 3. Download "OSGi web container" bundle from here and install it by copying it: cp osgi-web-container-3.0-SNAPSHOT.jar glassfishv3/glassfish/domains/domain1/autodeploy-bundles/
Sample Application 2. service @Singleton @Startup public class UserAuthServiceEJB implements UserAuthService
Please refer to EJB 3.1 spec for more information on these annotations. static volatile UserAuthService selfRef; which is initialized by an ejb reference in the postConstruct() method of the EJB:
@PostConstruct
public void postConstruct() {
selfRef = sessionContext.getBusinessObject(UserAuthService.class);
System.out.println("UserAuthServiceEJB.postConstruct: " + selfRef);
}2b. Activator - This is an OSGi bundle activator which looks up a data source in JNDI, ceates a table there and then registers the EJB reference (selfRef) in OSGi service registry under the service interface sahoo.hybridapp.example2.UserAuthService. For simplicity, we have not used any service properties here. 3. client
private class UserAuthServiceTracker extends ServiceTracker {
UserAuthServiceTracker() {
super(bctx, UserAuthService.class.getName(), null);
}
@Override
public Object addingService(ServiceReference reference)
{
UserAuthService service = (UserAuthService)context.getService(reference);
service.register("f..", "b..");
return service;
}
}
Build and Test mvn clean install in hybridapp2 directory. 2. To deploy, simply copy the artifacts like this (please note, because of a bug as described last time), while copying the .war file, give it a .jar extension as a work around. cp common/target/hybridapp2-common.jar glassfish/domains/domain1/autodeploy-bundles/ cp client/target/hybridapp2-client.jar glassfish/domains/domain1/autodeploy-bundles/ cp service/target/hybridapp2-service.war glassfish/domains/domain1/autodeploy-bundles/hybridapp2-service.jar Since deployment order is immaterial, you can copy them in any order. Benefits of EJB as OSGi service 2. Local client views of an EJB are accessible from other applications collocated in the same JVM. Please note, since Java EE can not assume a module management system is in place, it takes a safer route of isolated class space for application classes. This does not have to be the case when we are using hybrid applications. We can safely cross application boundaries and call local EJBs. 3. Better modularity of application: as shown here, the app has been broken into three independent deployment units. Once they are deployed, they can be managed (e.g., patched) separately. 4. Deployment order is a thing of past. OSGi takes care of class loading dependencies and then application can take advantage of facilities like Service Tracking to build very dynamic application. As shown in this example: if client bundle comes up before service bundle, then it simply waits for the service to be available. You can observe this by removing hybridapp2-service.jar from autodeploy-bundles/ and copy it back.I hope you find this example useful. Let us know what you want to hear. 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)
Why do you say session context is null?
Submitted by ss141213 on Wed, 2009-11-18 22:22.
Why do you say session context is null? Can you elaborate? Were it null, we would have got NullPointerException while trying to use it, won't we? Please provide more information.
Thanks,
Sahoo
|
||
|
|
session context is always null?