Tuesday Nov 23, 2010

Typesafe injection of dynamic OSGi services in hybrid Java EE applications

Update: Alexis, posted an excellent screencast/walkthrough of this demo here.

During this year's San Francisco JavaOne, Sahoo and I presented  a hybrid approach to enterprise Java application development [slides: here] where applications could use Java EE and OSGi capabilities in the same application. With hybrid applications, as we discussed in the talk and the hands-on-lab , developers can continue to build standard and familiar enterprise application components, such as Java Servlets and EJBs, and take full advantage of:
  • Features such as modularity/dependency management, declarative services, service dynamism, and more provided by OSGi
  • Infrastructure services such as transaction management, security, persistence, and more offered by Java EE.
One of the most interesting features in the OSGi environment is "Service Dynamism". The OSGi Service layer defines a dynamic, collaborative model that is highly integrated with the lifecycle layer of osgi. The OSGi service model is basically a publish-find-and-bind model. A Service is a normal Java Object that is registered under one or more Java interfaces with an OSGi Service Registry. Bundles can register services, search for them or receive notifications when their registration state changes. Key characteristics of the Service layer is that it encourage collaboration and dynamism of services in a secure manner.

When it came to consuming such dynamic services in a Java EE application component, a developer had to write standard boiler-plate, verbose, complex code to find, bind and track service references in the service registry(for example see here and here). The Java EE platform had already simplified injection of resources and depedencies through @Resource, CDI (JSR 299) etc. So, this boilerplate, error-prone code seemed out-of-place :) Other approaches such as OSGi Blueprint Service provide a verbose XML based, non-type-safe mechanisms for specifying dependencies and wiring between services and makes it tedious for a Java EE 6 developer who works with type-safe dependency injection technologies in the platform.

With GlassFish 3.1, application components can express their dependency on an OSGi Service, and have the container handle the discovery and binding of OSGi Services and inject them, by providing an additional Qualifier, @OSGiService, in the injection point. So instead of all the verbose service discovery and binding code,  the application developer states the requirement for an OSGi Service as follows:
    @Inject @OSGiService
    StockQuoteService sqs;

Note that the specification of the OSGi service type in the injection point is type-safe. The developer specifies that the injected service must implement the StockQuoteService interface using the field's type. Type-safety usually implies lesser runtime errors/easier debugging, refactoring etc.

Since the injection is specified through standard @Inject coupled with a custom OSGiService Qualifier, all standard CDI injection capabilities are available (constructor, field, setter method injection, programmatic lookup etc). The container automatically manages service references and ungets them when the component scope is completed.

A standard CDI portable extension (org.glassfish.osgi-cdi) comes pre-installed with GlassFish 3.1, that intercepts deployment of hybrid applications that has components who have expressed dependencies on OSGi services as shown above. The portable extension takes care of discovering the Service from the service registry using the criteria specified in the injection point, bind and track the service and inject the Service. Additional service discovery and injection related metadata could also be specified through annotation elements in the OSGiService Qualifier.For example, here are the current metadata attributes that could be specified:
  • Service Discovery criteria: the standard Filter syntax specified in the OSGi Core Specification can be used to narrow down choices for the Service type in the Service registry
  • Wait timeouts: Waits for the specified amount of time for at least one service that matches the criteria specified to be available in the OSGi Service registry.
  • Dynamic binding: used to handle service-dynamism. Since OSGi services are dynamic, they may not match the lifecycle of the application component that has injected a reference to the service. The developer could indicate that a service reference can be obtained dynamically or not through this attribute. For stateless or idempotent services, a dynamic reference to a service implementation would be useful. The container then injects a proxy to the service and dynamically switches to an available implementation when the current service reference is invalid.
[GlassFish 3.1 is still a work in progress and so, please consider this interface as unstable. Please provide inputs/feedback]

Let us see a simple sample that demonstrates this feature. The sample has a bundle that registers a StockQuoteService implementation on bundle-startup. Then we have another web application bundle(WAB) use the StockQuoteService, by having the container inject the service implementation using the @OSGiService qualifier. The servlet then finds all the symbols for which stock quotes are available, and print their current quotes.

To try out this sample
  • Extract the archive to a temporary location (say /tmp). This is a maven project that uses the JavaOne hands-on-lab template. It uses the maven bundle plugin to create the maven bundles and the maven war plugin to create the WAB. More details on the organization of the maven project structure is available in the docs archive here.
  • Extract the attached zip file and execute "mvn install" to create the two artifacts (a service implementation bundle and the WAB that uses the service) discussed below

  • A StockQuoteService API and implementation is in the stockquote_service project. The service interface is as follows:
public interface StockQuoteService {
    public Double getQuote(String symbol);
    public Set<String> getSymbols();
and the service implementation is at org/acme/stockquoteservice/impl/SimpleStockQuoteServiceImpl.java and has a list of a fixed list of symbols and quotes. The service implementation is registered in the start() method in the BundleActivator.
public class SimpleServiceActivator implements BundleActivator {
    public void start(BundleContext context) throws Exception {
        context.registerService(StockQuoteService.class.getName(), new SimpleStockQuoteServiceImpl(), null);
  • Install the stock quote service implementation bundle Let use the Apache Felix Gogo shell  to deploy the bundle. Apache Gogo comes with GlassFish 3.1. So telnet localhost 6666 to access it. If the demo archive was exploded is in /tmp, Install the bundle by executing install file:///tmp/stockquote_service/target/stockquote_service.jar
The shell provides a Bundle Id for the installed bundle as follows
Bundle ID: 275
Start the bundle by executing "start 275". Replace 275 in this command with the bundle id provided by the shell above. The Stock Quote service implementation is initialized during bundle start and registered in the OSGi Service Registry. An entry similar to the following must appear in the gogo shell.
Registered:[IBM, MSFT, HPQ, ORCL]

  • The stockquoteweb application bundle: references and uses the StockQuote service
public class StockQuoteServlet extends HttpServlet {
    @OSGiService(/\* wait for 1 min \*/ waitTimeout=60\*1000)
    StockQuoteService sqs;
Note that this WAR is a normal web application bundle [OSGi RFC 66 support in GlassFish], with an empty beans.xml descriptor to indicate that it is a CDI bean archive. The context root is specified as "stock_quote", using the Web-ContextPath manifest header
Web-ContextPath                         /stockquote                             
For simplicity, the service API and one implementation of that service was bundled in the stockquote_servic bundle.
  • Install the stock quote WAB bundle using the shell.  If the demo archive was exploded is in /tmp, Install the bundle by executing install file:///tmp/ stockquote_cdi_wab/target/stockquote_cdi_wab.war in the shell. Start the WAB bundle using the identifier provided by the shell.  For example, here is the sequence of steps:
g! install file:///tmp/stockquote_cdi_wab/target/stockquote_cdi_wab.war
Bundle ID: 276
g! start 276
  • Visit http://localhost:8080/stockquote/list to see the stock quotes provided by the stock quote service. The web application uses the StockQuote service implementation to get the quotes for a set of stock symbols.
  • Now, let us see how service dynamism is handled. Stop the service bundle by executing "stop 275" in the gogo shell. This stops the service bundle and the registered service implementaiton is removed from the service registry and is now unavailable for use. Hit the http://localhost:8080/stockquote/list URL now. Since we have a wait timeout of 30 seconds, the OSGi CDI extension waits for 30 seconds before it bails out and the web application prints "service unavailable". However within the 30 seconds, if you execute "start 275" to start the service bundle, the service bundle would register the service implementation again and the container would get the latest service implementation and provide it to the Servlet.
g! stop 275
SimpleServiceActivator stopped
g! start 275
SimpleStockQuoteServiceImpl::Initializing quotes
Registered:[IBM, MSFT, HPQ, ORCL]
SimpleServiceActivator::registration of Stock quote service successful

Through this sample, we have seen how easy it to consume OSGi services in a hybrid Java EE application in a dynamic, type-safe manner using the OSGi CDI extension in GlassFish 3.1.

Tuesday Dec 22, 2009

Java EE 6 and GlassFish v3 virtual seminar replays available -- Connectors 1.6 overview talk

As you might have seen in the recent Aquarium post, the session replays and slide-sets of the talks in the Java EE 6 and GlassFish v3 virtual seminar is now available (registration required for access. registration is free). The replay for the technical overview session on Connectors 1.6 (in slide-cast form) is also made available as part of this set. This talk highlights the new features, discussed in an earlier post, in Java EE Connector Architecture 1.6 (JSR 322).

The slides of the JavaOne talk on Connectors 1.6 are also now available at slideshare and embedded below.

Wednesday Dec 02, 2009

Java EE Connector Architecture 1.6 Specification approved!

The final approval ballot for the Connectors 1.6 specification and the Java EE 6 platform closed yesterday and the EC has approved the specification and the platform. As Roberto shared in his blog, the final release happens on December 10 along with the release of the reference implementation, GlassFish v3.

The Connector Architecture in the Java EE platform enables an enterprise application to work with disparate enterprise information systems (EIS), like databases, MoM products, transaction monitors etc.

The Connector 1.6 specification developed through the JSR 322 expert group, builds upon the the earlier Connector 1.5 specification in the following areas:

  • Generic Work Context mechanism: Defined a generic mechanism for propagating contextual information during Work execution. The specification standardizes passing in security, transactional and other quality-of-service parameters from an EIS to a Java EE component during Work execution.
  • Security Work Context: Defined a standard contract that a resource adapter could employ to establish security information(established identity) while submiting a Work instance for execution to a WorkManager, and while delivering messages to message endpoints (MDBs) residing in the application server
  • Ease of Development: Dramatically simplify the development of resource adapters through extensive use of Java language annations, reducing the need to write redundant code and the need for a deployment descriptor, provide better programmatic defaults, etc.
  • Standalone Container Environment: Defined container provider requirements for implementations of the Connector specification that exists outside the Full Java EE Profile.
  • Bean Validation: Defined how resource adapters could leverage the underlying Bean Validation support in Java EE to express their configuration validation requirements.
  • Added Features that enhance QoS and reliability around Connection Management, Work execution etc.

I had provided brief overviews of these features in previous blog entries and presentations. There are more articles/tutorials on the technology coming up in the next few weeks.

I would like to take this opportunity to thank all those who have helped in this version of the specification -- the expert group members of JSR 322, the spec-leads and EG members of related JSRs, the reference implementation (RI) and the technology compatibility kit (TCK) teams and all those from the community who had provided feedback and suggestions on the previous milestone drafts.

Edit: Jagadish Ramu, the Connectors RI lead, blogs about the release here.

Edit: GlassFish v3 is now available and further Aquarium coverage here.

Monday Jun 01, 2009

Connectors 1.6 Proposed Final Draft and the availability of Java EE SDK Preview

The JSR 322 expert group released the proposed final draft (PFD) of the Java EE Connectors 1.6 specification during mid-Febraury 2009.

We had covered the features introduced in the earlier milestones of the specification in this blog (see entries on early draft and public review).
Some of the changes and new features introduced in the PFD draft are:
  • Config property attributes: New attributes (See Section have been introduced to configuration properties to indicate if they
    • support dynamic updates: to indicate to the application server that dynamically reconfiguring the config property is supported by the resource adapter
    • are confidential: to indicate that a particular configuration property is confidential, so that deployment/runtime tools could use special visual aids denoting confidentiality and not expose the values as plain text.
  • The original InflowContext interfaces have been renamed to WorkContext or its equivalent, as the interfaces were not relevant only during Message Inflow but during all Work processing. (See Ch.11 and 16)
  • Robust handling of error conditions during MessageEndpoint creation and Work submission: Enhanced the MessageEndpoint creation APIs to support timeouts and also enhanced the exceptions thrown by the MessageEndpointFactory and WorkManager to handle retryable scenarios. (See 13.3 and 10.3.3 for more details)
  • Provide guidelines on the relationship of the Connector Architecture and other integration technologies such as Java Business Integration (JBI) and Service Component Architecture (SCA). (See Section 1.5)
  • Updated the ease of development annotations and contracts, that were introduced  in the Public Review draft of the specification, to address feedback received.  (See the change history, Section I.1, for a list of changes and Ch. 18 for more details)
  • Misc updates and clarifications to the Connection and Transaction Management chapters (see the change history, Section I.1 for a complete list of changes).
As always, we would like your feedback on the draft through the JSR comments alias jsr322-comments@jcp.org or in the comments section of this blog entry. Since this is the last milestone of the JSR before going final, please try to spend some time reading  the new features introduced in this version of the specification, try out the reference implementation (details below) and provide your review comments and feedback through the alias above.

As you might know, the reference implementation for the Connectors specifications and other components specifications in the Java EE 6 platform are developed in the GlassFish community. Now, with the release of the Java EE 6 SDK early access in JavaOne, most of the features in the PFD draft are now available for you to try out.

Jagadish, the lead of the Connectors technology reference implementation in GlassFish, has blogged recently about how a resource adapter can be built using the new metadata annotations introduced in the spec and demonstrates how the use of the EoD features could reduce the need for redundant boilerplate code and eliminate/reduce the need for XML deployment descriptors. Please also go through the Connectors 1.6 mail-connector sample that he has made available through the Java EE 6 Preview SDK to understand how these annotations could be used while developing a resource adapter.

Thursday Aug 07, 2008

Connectors 1.6 Early Draft Specification now available!

I am very happy to announce that the Early Draft of the JavaTM EE Connector Architecture 1.6 specification is available now. We started to work on an update to the earlier Connectors specification (J2EE Connector Architecture 1.5) in the Expert Group of JSR 322 in January this year. The Expert Group has been working very hard on the Early Draft and we are looking forward to hear your feedback. Please send your feedback and comments to jsr-322-comments@jcp.org.

The purpose of the Java EE Connector Architecture 1.6 specification is to address some areas in the earlier specification, where further support has been requested by the developer/user community and the expert group. Some of the important features that are being planned to be addressed in this release include:
  • Generic Inflow Context: a mechanism for enabling a resource adapter to provide additional contextual information while a Work gets executed by the application server's WorkManager
  • Security Inflow: enabling a resource adapter to propagate security identity information during Work execution and delivery to MessageEndpoints(MDBs)
  • General improvements to the specification: in the areas of handling connection failures, inbound and outbound configuration consistency, better configuration property processing (ability to specify better validation rules etc) and clarifications around the classloading of standalone resource adapters.
  • Focus on ease-of-development of resource adapters. Aligning with common programming model of Java EE by defining helper classes and annotations for the Connector API wherever applicable.
As a general reminder, since we're still relatively early in the process, the exact feature set is subject to change (for instance, the contracts have already gone through a lot of change since our JavaOne 2008 BoF presentation a couple of months ago :) ).

Here is a brief overview of the features that have been discussed and made it to the early draft. This is not a comprehensive list and so please see the Change History (Section I.1) for more information on all the changes made to the specification, in this early draft.
  • Generic Inflow Context: Certain Enterprise Information System (EIS) integration usecases requires the propagation of contextual information from the EIS to the application server. For example, a resource adapter may want to flow-in Security context information, (or in the case of an EIS that deals with conversational messaging, correlation information that might be necessary to recreate a conversational session state in the container) from the EIS to the application server during inbound message delivery. The resource adapter may also want to run a particular Work instance in the context of the "flown-in" Security information. 
The Generic Inflow Context is a new system contract that enables a resource adapter to control the execution context of a Work instance that it has submitted to the WorkManager for execution. The Generic inflow contract provides the mechanism for a resource adapter to augment the runtime context of a Work instance with additional contextual information flown-in from the EIS.

Inflow Contexts for propagating in transaction and security information from the EIS into the application server during the execution of a Work instance have now been standardised via the TransactionInflowContext and SecurityInflowContext interfaces. An application server must support both these inflow contexts and therefore a portable resource adapter can assume an application server’s support for both these inflow contexts. Since the Inflow Context contract has been defined to be generic and extensible, the Connectors specification or other Profiles may define additional context types in the future.

For more information on this new system contract, its API and an illustrative example of how a resource adapter can pass in (say) Transactional information along with a Work instance during Work submission, please refer "Chapter 11. Generic Inflow" of the Early Draft.
  • Security Inflow Context: It is critical, in EIS integration scenarios, that all interactions between an application server and resource adapter are secure.To achieve end-to-end application security, it is important that all activities that a Work instance performs, including delivering messages to a MessageEndpoint (MDB) happens in the context of an established identity.
The Security Inflow Context is a new standard contract that enables a resource adapter to control and establish security information during the execution of a Work instance. This contract provides a mechanism to support the execution of a Work instance in the context of an established identity. It also supports the propagation of user information/Principal information from an EIS to a MessageEndpoint(MDB) during Message Inflow.

So, for instance, if the resource adapter uses the new Security Inflow contracts, deliveries to Message Driven Beans (MDBs) could be made in the context of a security identity [that is, MessageDrivenContext.getCallerPrinicipal() and MessageDrivenContext.isCallerInRole() would returns values established by the resource adapter/EIS].

For more information on this new system contract, its API and an illustrative example of how (say) a XMPP resource adapter can deliver a message with appropriate security information, please refer "Chapter 16. Security Inflow" of the Early Draft.
  • Other changes: In addition to the two new changes discussed above, a suite of new features/changes have also been discussed in the early draft. A few of them are:
    • a definition of minimum set of requirments that must besupported by a compliant Java EE Connectors Architecture 1.6 container within an implementation of any subset of the Java EE Full Profile (like a Web Profile). Refer Section 3.5 
    • an ability to specify the transaction support level of a resource adapter at runtime. Refer Section 7.13
    • ClassLoading requirements for standalone resources adapters. Refer Section 19.3




« April 2014