GlassFish OSGi - A Modular Runtime for Hybrid Applications Peter Doschkinow Senior Java Architect
The following is intended to outline our general product direction The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
Agenda OSGi basics OSGi meets Java EE Hybrid applications GlassFish v3 architecture GlassFish and OSGi Demos
What is OSGi Set of specifications delivered by the OSGi Alliance Originally for embedded devices, set top boxes, network Service oriented dynamic module system for Java Breaks applications into modules(“bundles”) Bundles can be managed dynamically without restarting the container Multiple versions are supported Dependencies explicitly defined with clear boundaries
Execution Environment OSGi Layers Service publish/find/bind intra-VM model Life Cycle bundle lifecycle in OBR Module consistent class loading rules Execution Environment CDC, Java SE Security
Module Layer Bundle – a unit of modularity in OSGi Encapsulation Lifecycle independent of the JVM Versionable Packaged as a JAR classes + Manifest with OSGi metadata Very explicit dependency specification Well defined dependency resolution rules
OSGi Metadata Bundle-SymbolicName: com.acme.hello.startup Bundle-Name: Hello World OSGi Sample Bundle-Version: 1.0.1 Bundle-ManifestVersion: 2 Bundle-Activator: com.acme.hello.startup.MyActivator Export-Package: com.acme.hello.startup;version=1.0.0 Import-Package: org.osgi.framework;version=1.3
Lifecycle Layer Provides API to manage bundles BundleActivator BundleListener Listens to BundleEvents
Service Layer In-VM SOA Service Registry Register Unregister Service is a POJO Service Dynamism Service tracking Service Discovery LDAP filter based queries
OSGi Advantages Summary Solves most problems that result from the insufficient Java support for modularity and dynamism Defines a module system Explicit code sharing through import/export of packages Supports versioning and evolution Lifecycle management Dynamic deployment and configuration Ease of deployment and management Automatic management of code dependencies Enforces sophisticated consistency rules for class loading Bundle deployment order is not relevant
Java EE Platform of choice for enterprise applications Widely used infrastructure APIs JPA, JTA, JAXB, JNDI, JAX-WS, JAX-RS, JCA Component Models (Servlet, EJB, JAX-RS) Frameworks (JSF, CDI) Ease of Use Annotations, Convention over Configuration Tools (IDE, Management, Monitoring) Ubiquitous deployment platform for enterprise applications
Hybrid Applications use advantages from both worlds OSGi + Java EE = Hybrid application Hybrid application An OSGi bundle And a Java EE Archive Leverage the capabilities of both the platforms Enterprise applications can be built as modular OSGi bundles use OBR, Config Admin, service tracking, etc. OSGi bundles can use Java EE services like JTA, JPA Require a runtime that supports both standards Current State • OSGi Enterprise Expert Group (EEG) – Relased OSGi Service Platform Enterprise Specification 4.2 in 2010 • Open source efforts – Project GlassFish – Project Aries – Eclipse Gemini
OSGi Enterprise Specifications OSGi Declarative Services Declarative model to simplify the work with OSGi services OSGi Blueprint Container IoC and DI framework, derived from Spring DM project OSGi/JPA OSGi/JDBC OSGi/JTA OSGi/JNDI OSGi/HTTP Service OSGi/Web Application Web Application Bundles (WAB)
OSGi Enterprise Specifications OSGi/JPA Makes Persistence Units/Provider availabe in OSGi Enhancement of JPA entities at runtime Same packaging rules as JPA and also deploy entities as a bundle Shared Persistence Unit and thereby shared second level cache OSGi/JDBC JDBC driver as DataSourceFactory Dynamic discovery of driver details Multiple versions of same driver OSGi/JTA JTA artifacts available as OSGi Services
Web Application Bundle (WAB) OSGi/Web Application WAR + OSGi bundle configuration Manifest-Version: 1.0 Import-Package: javax.servlet.http; javax.persistence Bundle-ClassPath: WEB-INF/classes/,WEB-INF/lib/entities.jar Bundle-Version: 1.0 Bundle-ManifestVersion: 2 Web-ContextPath: /hello Bundle-SymbolicName: test.hellowab Lifecycle now can be driven by OSGi Wrapped WAR Support webbundle:file:///myWebApp.war?Web- ContextPath=/hello
Strong community and developer adoption Project Glassfish Java EE RI Java EE 5.0, Java EE 6.0 Open Source Strong community and developer adoption Sources, bug DBs, discussions at Java.Net Roadmaps, Architecture Documents Production Quality GlassFish 3.1 – currently developed Clustering, HA, centralized administration Technology refresh, better OSGi support Moving beyond GlassFish v2, work has already begun on GlassFish V3. The GlassFish community is acting in a very transparent manner. Public roadmap and requirements document (with community input). Roadmap, dates and features are subject to change. http://wiki.glassfish.java.net/Wiki.jsp?page=GlassFishV3Requirements http://wiki.glassfish.java.net/Wiki.jsp?page=GlassFishV3Schedule At the core of GlassFish V3 is a microkernel architecture. Only the necessary modules are loaded during runtime (with no pre-configuration required). The application server core starts in less that one second and consumes a minimal amount of memory. As applications are deployed, only the required modules are loaded. If a web application is deployed, only the web container is started. If a Jruby application is deployed, then the Jruby container is started. Improved enterprise feature: Security: Role-based access control in management console Scale management: Manage over 100 instances from a single console Improved application versioning - Rollback to prior version of an application. - Goal (no commit): Multiple versions of an app running simultaneously. Improved developer support: Added features will not compromise ease-of-use Dynamic language support (JRuby, Groovy, Jython, others) GlassFish V3 is the delivery vehicle for Java EE 6 and its support of profiles. Work has just begun and progress can be followed at http://glassfish.java.net
GlassFish v3 Architecture Hundred Kilobyte Kernel
GlassFish v3 Modularization and Extensibility Based on OSGi Extensible Extensive APIs to replace or extend features OSGi also provides extensions capabilities SOA based architecture Services implement different types of interfaces Java SE style with a META-INF/services file HK2 OSGi style Lazy loading based on usage patterns Makes it easier to provide support for new container types
GlassFish v3 Runtime GlassFish v3 HK2 Module System Felix, Equinox binding static binding OSGi Runtime JDK 6
HK2 Module System Abstraction to the module subsystem No OSGi dependency above HK2 layer Created to support better GlassFish use cases Better lazy loading/resolving compared to OSGi Activator/Extender pattern that often require immediate resolution of modules Service Based Architecture with IoC Services are injected and injectable Services annotated with @Service, interfaces with @Contract Configuration handling Binding to XML files Transactional access Support validation APIs and MBeans/REST
GlassFish and OSGi Runs on top of OSGi (Felix by default) Also runs unmodified on Knopflerfish and Equinox Can be installed in an existing OSGi runtime GlassFish ships with 200+ bundles in v3 Can run without OSGi (Static mode, thanks to HK2) Can use OSGi management tools (CLI or Web) All GlassFish modules are OSGi bundles No OSGi APIs are used in GlassFish HK2 provides abstraction layer
GlassFish and OSGi OSGi visibility extended from GlassFish developers to GlassFish users OSGi services can be easily used in any Java EE application @Resource(mappedName=”checkOsgiService”) CheckService checkService; Client code portable, does not use any OSGi specific API Restriction: OSGi dynamism does not work Converged applications needed Implementation of OSGi Enterprise specifications HTTP Service, Web Container, JPA, JTA, JDBC
Extending GlassFish v3 with OSGi http://blogs. sun Extend GlassFish with an unmodified Spring dm container Simple Spring bean implementing the service Invoke the service from a servlet using standard @Resource injection Still no use of a GlassFish API Single runtime for both Spring and full Java EE
Managing OSGi Bundles in GlassFish http://blogs. sun GlassFish CLI asadmin deploy –type osgi File system operations cp/rm to/from glassfish/domains/domain1/autodeploy/bundles Felix remote shell/gogo shell telnet localhost 6666 Felix remote shell (GlassFish v3) Gogo shell(GlassFish 3.1) provides a scripting environment for administration Apache Felix web console
WAB Lifecycle and Deployments in GlassFish Java EE deployment on RESOLVED → STARTING Java EE undeployment on STOPPING → RESOLVED Alternative WAB Deployments With OSGi manifest asadmin deploy --type osgi hybridapp.war cp hybridapp.war glassfish/modules/autostart cp hybridapp.war domains/domain1/autodeploy/bundles Plain WAR file install webbundle:file://work/webapp/target/hybridapp.war?Web- ContextPath=/hello
OSGi/EJB GlassFish feature Allow EJBs to run within OSGi context Easily discovered/accessed by other OSGi bundles Can use container managed JPA TX, security, persistence contexts propagated Automatic enlistment of EJBs as OSGi services Local beans only (SFSB, SLSB, Singleton) Manifest entry Export-EJB: ALL|NONE Can split client & service into separate bundles OSGi ServiceTracker listens for EJB service availability, making deployment order irrelevant
OSGi JMS Producer public class JMSActivator1 implements BundleActivator { public void start ( BundleContext context ) { context.registerService ( ManagedService.class.getName(), new ManagedService() { public void updated ( Dictionary properties ) { sendMessage(); } }, props ); private void sendMessage() { ... Destination dest = (Destination) ctx.lookup("jms/osgi.Topic1"); session.createProducer(dest).send(...); ....}}
OSGi MDB consumer @MessageDriven(mappedName = "jms/osgi.Topic1") public class MyMDB implements MessageListener { public void onMessage(Message message) { ... }
CDI Extenstion for OSGi in GlassFish @Inject @OSGiService MyOsgiService svc; Eliminates boilerplate code in OSGi service clients For finding, binding and tracking OSGi service references Typesafe, in contrast to XML based Blueprint Service Available attributes for @OSGiService Service discovery criteria in standard OSGi Filter syntax Wait timeout Dynamic binding for service dynamism through container injected proxy Applicable in WAB Since BundleContext is needed
All interaction scenarios WAB, EJB module as OSGi service GlassFish OSGi Demos JavaOne 2010 Hands-on Lab All interaction scenarios WAB, EJB module as OSGi service http://blogs.sun.com/arungupta/entry/screencast_32_osgi_enabled_java OSGi declarative services example No boilerplate code, automatic OSGi service registration Maven SCR plugin for OSGi metadata generation http://blogs.sun.com/arungupta/entry/totd_124_osgi_declarative_services OSGi CDI example CDI extension for OSGi in GlassFish, applied to a WAB http://blogs.sun.com/sivakumart/entry/typesafe_injection_of_dynamic_osgi
Summary GlassFish is modular & extensible (HK2/OSGi) OSGi Declarative Services & CDI OSGi extension No OSGi API, no GlassFish API, Integration Nirvana! Hybrid applications bring OSGi benefits to JavaEE (and vice-versa) Reusable bundles Independent life-cycles Standard component models GlassFish covers all your Java EE and OSGi needs
Links OSGi Service Platform Core and Enterprise Specification 4.2 http://www.osgi.org GlassFish http://glassfish.org http://blogs.sun.com/theaquarium http://www.youtube.com/user/GlassFishVideos http://wikis.sun.com/display/GlassFish/OsgiDashboard http://wikis.sun.com/display/GlassFish/3.1OSGi Hybrid application samples http://wikis.sun.com/display/GlassFish/BlogsGfOsgi
GlassFish OSGi - A Modular Runtime for Hybrid Applications Peter Doschkinow Senior Java Architect