Book Review of Building Modular Cloud Apps with OSGi by Bert Ertman and Paul Bakker (last edit: 25th October) osgi stack webapp

Book Review of Building Modular Cloud Apps with OSGi by Bert Ertman and Paul Bakker (last edit: 25th October)

(this article was written as a live blog: it was work in progress for several days- while I was reading this book – this article was updated several times; at this point, it is complete.

Practical Modularity with Java in the Cloud Age

    • Title: Building Modular Cloud Apps with OSGi – Practical Modularity with Java in the Cloud Age
    • By: Paul Bakker, Bert Ertman
    • Publisher: O’Reilly Media
    • Released: September 2013
    • Pages: 210
      Print ISBN: 978-1-4493-4515-0 ; | ISBN 10: 1-4493-4515-8
      Ebook ISBN:

978-1-4493-4510-5 | ISBN 10: 1-4493-4510-7

For a long time, OSGi has been a topic of which I was vaguely aware and had a dormant interest in. I never got round to actively diving into this subject. To me OSGi was synonymous with modular Java applications that run in an environment that allow partial and dynamic refresh of parts of the application. I have heard about attempts to apply OSGi concepts to Java EE application servers such as GlassFish (as of v3.0) – initially to the kernel of the Application Server rather than the applications deployed on top of it, subsequently also for deployed applications. Of course the ongoing Project Jigsaw that attempts to bring modularity to the Java platform – JVM and at some point applications running on it as well – is associated with OSGi as it attempts to provide an alternative to a similar problem.

When one of the authors of the recent O’Reilly publication “Building Modular Cloud Apps with OSGi” handed me a copy of his book, I had a concrete impetus to get beyond that very superficial understanding of OSGi and how it can be used. This article is about this book – and what I learned from it. I am writing this article as a live report of my reading of the book. Initially, the article is incomplete and my appraisal of the book in its entirety not yet available. As time progresses and I read my way through the book, I will add more content to this article.

As all O’Reilly publications do, this book looks and feels good. Pleasant, familiar user experience. I still have fond memory’s about Jason Hunter’s Servlet Programming that I used to get started with Java back in 2001 – and this book has a very similar look and feel. It is not very thick: just over 200 pages. It is not overwhelming at all. The book has three parts:  I- Introducing Modularity in Java, II- Developing Cloud Applications, III- Deploying Applications in the Cloud.

Part 1 – Introducing Modularity in Java

The first part that I am currently reading provides a great introduction into Modularity in Java (in general) and then progresses to discuss OSGi, various implementations and tools and the first steps with real code. The authors decided to work with Eclipse and Bndtools and Apache Felix as the OSGi container – and explain this decision carefully and clearly. Maybe more clearly than carefully as they are not afraid to share their opinion. They give clear recommendations and provide their reasons for making these recommendations – which is very helpful. They are not great fans of Maven (“[…]Maven itself is slow and painful to use, and it’s difficult to get very fast turnarounds on code changes, we don’t advise using it as a modularity solution.”).

Modularity is a means to several ends. It is interesting how the objectives of Modularity at the application level are very similar to what Service Oriented Architecture tries to achieve at systems level. The authors discuss this and hasten to add that SOA adds tremendous overhead and “applying integration patterns” would be “an extreme form of over-engineering and dramatic slow down of development and maintenance”. Well, that may be their opinion – it does not change the fact that many patterns and concepts that I am familiar with from Service Oriented Architecture are very similar -albeit at a different architectural level- to how the authors deal with modularity. Anyway, it seems that we agree on what really matters.

{edit: 23rd October]

Part 1 takes up about half of the book (some 90 pages in total). It is not yet about cloud (application) but covers the basics of OSGi. I was looking for such an introduction – so it suited me fine. Any Java developer should probably read this first part – to at least know what OSGi is before deciding not to apply it; if you do want to apply it, then read Part 1 again and continue to Part 2.

Many technologies, frameworks and tools are touched upon in the book and positioned as well relative to one another. They are in the Java SE and EE arena’s as well as in the supporting tools and ubiquitous libraries. Application architecture is also quite frequently discussed. That too is quite useful – especially with all the names I only have a fairly vague impression of.

Chapter 2 lays the foundation, Chapter 3 has the reader going through a simple but real application (Conference Application). Chapter 4 is on Advanced OSGi, introducing advanced but frequently required OSGi mechanisms such as versioning, configuring services , the log service, the notion of events, extensions or plugins, aspect services and the bundle cache. Some of these are only very briefly described; I need more on extensions – as I do not think the extension example in the book is optimally focused on the extension mechanism. Nevertheless, the introduction in the book is enough to let me appreciate that the mechanisms exist, what they are broadly used for and how by and large to use them. That is a good start to starting browsing the internet for more.

Chapter 5 is on pitfalls and best practices. That is where the authors make their real world experience available to the rest of us, by clearly warning us of certain mechanisms – bundle dependencies, fragment bundles, dynamic imort and optional import – and carefully making the case for certain others. They also explain how to deal with common challenges, such as working with non OSGi enabled libraries inside OSGi applications. The authors give me the impression that anything I really ought to know for working in anger with OSGi is discussed in this chapter. Not all the minute details, but all the major decisions and mechanisms. This chapter also introduces some of the low level APIs that I typically will never have to use but that are there just in (corner) case. These also help to understand some of the internals of the higher level black box mechanisms such as service injection. JDBC is briefly mentioned, as a cumbersome beast in terms of avoiding classpath scanning (the mother of all evil you would say after reading this chapter); more on JDBC and database interaction in chapter 9.

The authors appear big fans of the white board design pattern – an important pattern in OSGi, alternative to the use of listeners : components indicate their ability to do something or their desire to get something through simple means (a bundle header, an annotation or interface implementation or service property). Then they sit back an relax and if the OSGi framework can do something with or about their ability or interest, it probably will. In a way that is of no concern to the component.

[end of edit 23rd October]

[edit 24th October]

Part 2 – Developing Cloud Applications

The second part builds on the OSGi fundamentals introduced in part one and moves to building real (web) applications. The notion of Cloud application should not be regarded in a very sweeping way, only applicable to fancy applications deployed on cloud infrastructures. The authors work with a very hands-on, practical definition of cloud application: “any modern (web) application leveraging REST and NoSQL and requiring (or at benefiting from) some form of elastic scalability”. Scalability means that the response times (performance) of the application can more or less be maintained with increasing load on the application by scaling the platform (i.e. adding nodes).

The architecture described in the book makes sense both for on premise and for in the cloud. The stack assumed in part 2 by the authors consists of a regular (modern) HTML 5 Rich Client framework on top of RESTful (data) services exposed by OSGi application modules running in an OSGi container with Amdatu Components and an (embedded) Servlet Container inside Apache Felix (or possibly some other OSGi container such as Glassfish ).

osgi-stack-webappThe Amdatu project is strongly recommended and its components are used throughout part 2. This project is described as “Amdatu is an open source community effort focussed on bringing OSGi to the cloud. It contains components to create RESTful, scalable and distributed web applications that use NoSQL data stores, transparent multi-tenancy and much more.” (see http://www.amdatu.org/).

After clearly explaining the application’s layered architecture, all of a sudden a paragraph is devoted to Remoting – the OSGi equivalent of RMI. Remoting is positioned as an alternative – and more coarse grained – way of modularizing. No code examples are given for remoting and I am left wondering what point the authors tried to make here. Perhaps this section would have fitted better in chapter 4 (advanced) or chapter 5 (pitfalls and guidelines).

Chapter 7 is fairly short. It discusses Architectural Capabilities. Given that title, it is very readable. It touches upon many topics – providing a great introduction and overview for example of scalability, NoSQL and the crusade against session state. It also explains [why] relational databases can be a bottleneck in achieving horizontal scalability. Most of this chapter is not OSGi specific – or even all that new. But it is well written and offers valuable insights.

Chapter 8 is about the creation of modular Web Applications based on an OSGi foundation. The authors explain how to create HTTP services  – Servlets – in a modular way by leveraging the OSGi Compendium Services. Servlets are used only to serve static resources – such as images, JavaScript libraries and perhaps some HTML snippets. The heavy lifting in the thin-server-architecture is implemented using REST ful; services created using JAX-RS (and the Apache Wink implementation in this case). Servlets, filters and any server side generated HTML are by and large considered a thing of the past. With all the power in the browser and client – both in terms of functionality (HTML 5, JavaScript) as well as physical resources – why rely on heavy server side processing? A large part of chapter 8 deals with the development of RESTful services in a modular fashion. The authors return to their Conference sample from chapter 3 – a good example that perhaps they could have made more use of in the other chapters. After using Jackson – for automatic Java Class to JSON mapping – the chapter briefly glosses over Swagger – a framework for generating documentation of RESTful services that is integrated with Amdatu.

The modular set up of the client side is brief discussed: separate URL paths for different ‘modules’, module integration through menus. Note that a client side module can be packaged in an OSGi bundle – with all the CSS, JavaScript, HTML and image resources that are needed for the module. The authors proclaim their preference for AngularJS as the client side framework to use for creating the user interface part of their applications; they do not however show any client side code.

I like the example of the TokenProvider based on the Google Plus authentication. Near the end, just before the authors deliver a crushing blow on Portal products (Portal servers are hard to use and often heavy weight products) they describe Portlets as a way to achieve modularity for the user interface – albeit through the server side (which is not modern practice for web applications). To demonstrate modern modularity for user interfaces the authors do something peculiar in my opinion: they introduce the OpenSocial API and its gadget container specification as their showcase, after having explained how the Open Social API basically failed to fulfill its promise. I understand that the gadget container is a good example of client side modularity, but one would have hoped that there is a better example of such modularity than a specification that ‘never really took off’.

Chapter 9 discusses at length and in useful detail how OSGi bundles can work with relational databases. Through plain JDBC but much better:through JPA using EclipseLink and the Gemini JPA Distribution of the OSGi JPA specification. A simple, straightforward example is given of how a bundle can have an entity manager injected and access entities through that manager. Similar to – but much more lightweight and dynamic than – Java EE resource injection.

I really like the introduction of NoSQL: free from hype and prejudice the authors provide a clear reasoning how sometimes relational databases have to be complemented with NoSQL style database. They suggest polyglot persistence will typically be the way to go with a combination of relational database and NoSQL store. They explain various types of NoSQL databases, list the most popular products, describe the notion of BASE vs ACID and then go on to focus on document stores and more specifically MongoDB. In about five pages they manage to introduce MongoDB, explain how to configure and register MongoDB with the OSGi container and leverage a persistent service based on MongoDB in database agnostic bundles. That is really a great job. At the end of the chapter, we have a RESTful service – exposed from one OSGi bundle – that gets a data service injected that itself leverages a  MongoDB database instance for storing JSON documents and searching/retrieving these documents. Pretty cool – and well explained.

[edit 25th October]

[end of edit 24th October]

Part 3 – Deploying Applications in the Cloud

This 3rd part is the small one – only 21 pages. It focuses on how to get the OSGi application running outside the development environment, in a stand alone OSGi container that is hosted in a cloud environment (private or public). The chapter explains again that the authors prefer Apache Felix over Eclipse Equinox. They explain the concepts of SaaS, PaaS and IaaS. They conclude that there is no PaaS that offers a modularized environment – one that has an OSGi container to deploy into. Creating an OSGi platform in an IaaS environment – either a private or a public one is pretty straightforward, as chapter 10 demonstrates. The key enabler for a smooth deployment of our modular application to this run time environment is Apache ACE – a provisioning server for OSGi. By default Apache ACE runs on port 8080, as does the embedded Jetty server so in order to use both we need to change the port for one of the two. The book unfortunately does not provide instructions on how to change the port, even though most readers will want to because of this port conflict.

Apache ACE recognizes deployable OSGi artifacts, such as bundles and configuration files. It supports as target any OSGi framework (with Apache ACE agent), which can be on an embedded device or inside a Java EE container. The authors explain in a few pages how to set up Apache ACE and how to use it for deployment – including parameterized configuratoin.

The second part of chapter 10 was a little bit harder to understand. It discusses autoscaling of our infrastructure, more specifically auto-scaling in Amazon EC2. The chapter the shows a Groovy script and a Bash script and two console commands – and I get lost a little. The scripts are about registering a node (with an OSGi runtime although I am not quite sure how that run time ended up in the Amazon Machine Image) with the central Apache ACE server; that allows Apache ACE to start using the AMI as a deployment target. The automatic scheduled creation and discarding of Amazon AMIs is demonstrated with the console commands. I found this section the hardest to understand in the book that I consider very clear in all other parts. It feels like perhaps the authors tried to cram to much information into a few pages (deployment to Amazon EC2, auto-scaling, automatic startup script to register AMI with ACE,…).

Chapter 11 explains how some Java EE application servers have a built in OSGi container that allows us to deploy the modular application to a familiar server environment. The authors strongly doubt the need, usefulness and desirability of using a Java EE container but if management or other powers that be insist, it can be done. Note: OSGi applications can also be exported to single JAR file that contains an Embedded OSGi container. This JAR file can be deployed to any environment that runs Java, including PaaS providers with JVM support. This approach kills run time modularization though.

Glassfish seems to be the favorite in these conditions. It supports simple deployment of OSGi applications. As a bonus, many Java EE facilities – including EJB/JPA, transaction service, HTTP service, JDBC Service and JMS – are exposed in Glassfish as OSGi services that can easily be leveraged from OSGi bundles. JBoss also offers an OSGi container – but without this integration of Java EE facilities into OSGi services. WebSphere seems to be somewhere in between the two.  Apache Karaf is mentioned as a pure OSGi application server – and then dismissed by the authors, as is Eclipse Virgo.

One application server the authors completely fail to mention is the recent WebLogic Server 12.1.2 release (July 2013), that also provides embedded OSGi support.

Appendix A – the last part of the book – describes a sample application. This application – a web shop – is not discussed in any of the book’s chapters. It is like a bonus, a reference application, that shows all pieces of the modular OSGi Web Application introduced in the book applied together in a fairly simple application. The sample is available from Git and can easily be accessed by the reader.

Conclusion

As you may have gathered from the previous comments: I like this book. It is well written, concise and straightforward in its recommendations. The book provides a good introduction to many concepts for newbies in OSGi. It brings a large number of current topics together and connects them in a meaningful way. The book provides inspiration and confidence to get going with OSGi, Amadatu and several related tools and technologies. It approaches hot and frequently over hyped terms such as NoSQL and Cloud in a pragmatic, down to earth way that I can appreciate.

The book is a little sparse on illustrations: few diagrams and no screenshots at all. Perhaps the authors could make a number of screenshots available in a SlideShare presentation for example – just to make things a little clearer for readers who do not have time or resources to actually follow through all examples. A ready to go VM would also be nice…

All in all – well worth the read!

[end of edit 25th October]

Resources

Building Modular Cloud Applications in Java – Lessons Learned – SlideShare presentation from Geecon –

O’Reilly’s Home Page for this book: http://shop.oreilly.com/product/0636920028086.do

White paper on the OSGi Whiteboard Pattern.