November 30, 2008

Building a simple OSGi Service

I have been experimenting with this technology and the main inspiration came from SpringSource's dm Server. Like other OSGi evangelist,I am beginning to see OSGi technology as technology that will transform Java development. I would like it to be more on the enterprise side. In this session, we will see how to build a simple service and consume it.

I started of my OSGi quest with Equinox and Eclipse IDE. But for the tutorial that we have today, we do not need Eclipse as it's a simple application (bundle). You may also use other OSGi runtime like Apache Felix or Knopflerfish. Knopflerfish even gives you are good GUI to work with. I will not be explaining fundamentals of OSGi technology. You may refer the technology overview, technical whitepaper and business whitepaper for more information.

What's OSGi service?
In very general terms, a service is a repeatable task. When it comes to business, any repeatable task in your business process is a service. Similarly in a application, you can have generic tasks (even specific tasks) that are repeatedly used can be represented as service. Representing and using these tasks as services is what SOA is all about! But that' at an enterprise level. When it comes to OSGi services, it is the same concept but applied at JVM level.

In OSGi, a service is a plain java object which is published to a registry. A consumer can consume the registered service through lookup. A service a be registered and unregistered at any point of time. Service is built using interface-based programming model. To implement or build a service you basically provide implementation to a interface. To consume, you only need the interface for the lookup and there is no need to know about the implementation. The service registry is the "middle man" who help producers and consumers to get in touch with each other.

Building HelloWorld service
The first step would be to create our interface or "front end" of our service. For our service, we will have a simple interface named IHelloService:

public interface IHelloService {
public String sayHello();
And here is our service implementation.

public class HelloService implements IHelloService {
public String sayHello() {
return "Hello World";
That's it! Our service is ready for use. But, we need to inform consumers that the service is ready to serve. For this, we will have to register our service with the OSGi service registry.

OSGi framework provides us with standard APIs to register and unregister service with the registry. We will use the registerService method to register as shown below:
serviceRegistration = context.registerService(IHelloService.class.getName(),helloService,null);
I am sure for beginners this is not enough. Let's explain the stuff little further.To register our new service, we will build a simple bundle that will call registerService method.

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator {

private ServiceRegistration serviceRegistration;
private IHelloService helloService;

public void start(BundleContext context) throws Exception {
System.out.println("Starting HelloService Bundle..");
helloService = new HelloService();
serviceRegistration = context.registerService(IHelloService.class.getName(),helloService,null);


public void stop(BundleContext context) throws Exception {

Our Activator class implements BundleActivator. Basically, its a simple OSGi bundle with start and stop methods. We will register our service with the bundle starts up and unregister when the bundle is uninstalled from the framework.

Now lets have a closer look at start method. We create a instance of our service and then use registerService method. The first argument is service name which is obtained using InterfaceName.class.getName(). Its a best practice to use this method instead of specifying the name as string ( The second argument is the instance of the service itself. And the final argument is Map wherein developers can pass additional properties to the service.

To unregister the service, we simple call unregister method when we stop the bundle. So now we have a running service on our OSGi runtime. Lets see how to consume it.

Consuming a service
To consume a service, we first create serviceReference object form the BundleContext. This can be achieved by calling getServiceReference method. The method takes the class name as a argument. Once you have the serviceReference object, we will use getService method to finally get the service. We will have to typecast the object returned by getService method before using it.
helloServiceRef = context.getServiceReference(IHelloService.class.getName());
IHelloService serviceObjectHelloService = (IHelloService)context.getService(helloServiceRef);
System.out.println("Service says: " + serviceObjectHelloService.sayHello());
Implementing the service and consumer is the same package is easy. Because, the interface is available. When you have your service and consumer bundle separate, there are some important points to note. OSGi provides the capability of specifying the packages they can be exported or imported. With this facility you can expose your service interface and hide its implementation from the public. The configuration details are specified in the MANIFEST file. Have a look at our HelloService's MANIFEST file:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService Plug-in
Bundle-SymbolicName: HelloService
Bundle-Version: 1.0.0
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0"
Notice that we have exported package. Similarly, we import this package in our consuming bundle.

You may download the complete code of this tutorial. I have two eclipse projects, HelloService – implementation of the service and HelloBundle – will consume the service. And to add some notes; The code used for consuming the service is not the best way. I have made the code very simple and easy to understand without involving Exceptions handling,Null pointer checks and ServiceListeners. We will have a look at ServiceListeners next.

November 14, 2008

Gift from Jimdo, or just a Spam?

I am not sure if you know about Jimdo. I had blogged about the service some time back but, this time around I have a different reason to write about Jimdo. For an intro, Jimdo is a free web site creation tool. The service allows you to build your pages integrating images, video with different designs.The service also boost about the feature of copying themes and design of other sites in simple steps. Recenlty, I got a mail from one of the employees from Jimdo (atleast he looked like) and that's when all things started!

This was an unusual mai. Basically, the content was posted through my Jot Form. The message was from some one named Till and claimed to have the mail id "". There is the message:
We from Jimdo would like to send a little "Thank you" to all who have been spreading the Jimdo news online throughout the past 1.5 years.

Therefore we would appreciate if you could send us your address as soon as possible.

With regards,
And my response to this mail not with a mail with my address, but a simple note to know if this was genuine! And so far no reply. So was this whole thing fake? Or is somebody making trouble for Jimdo?

November 06, 2008

Sun Introduces OSGi based Server: Glassfish V3

Sun Microsystems announced the availability and support of the their latest application server: GlassFish version 3 Prelude. The application server is a lightweight and based on modular architecture. The new server is also a preview to the next version of java enterprise edition (JEE 6). Let's have look at Gfv3's features.

GlassFish is one of the leading open source application server. Sun claim to have more than 14 million downloads of the server. But I wonder how many are used in production environment and not by students and developers (I have a regirstered version of Gfv2 on my laptop)? The new version of server was redegined from top to bottom to run on the popular modular runtime called OSGi runtime. Glassfish makes use of Apache Felix, an open source OSGi runtime from Apache. Sun claims that, the application server can also run on Eclipse's Equinox runtime.

The server brings in lots of changes in how an application sever works. The server startup time is drastically reduced compared to the pervious version. This was acheived by the way how classes are loaded in the server. At startup a full fledged application server do not start, instead only the necessary modules (containers) get loaded.Containers do not get loaded unless they have a component to execute. For example, EJB container do not start untill I have a EJB depoyed.

The server comes with a full web container that can host servlets 2.5, JSP and JSF. JSF 2.0 is provided as preview. Developers can also have a first look at EJB 3.1. They can also make use of the JPA for persistence. Sun introduces the concept of a update center from which you can download other modules like web service stack or Jersey . The server also introduces containers that support native jRuby/Rails, Groovy/Grails.

The major advantage for a development team from GlassFish is that it provides the ability to maintain the sessions active even during deployment. This feature helps both developers and testers to test the code, refine them if need and test it rather than going through the long cycle that we currently go through. So next time you find a bug when testing your code, all you need to do is change your logic, deploy and start testing with the same session. Netbeans 6.5 is said to have good integration with the new server, providing deployment of new code on the server as and when you save the code and its compliation error free! Other advantages includes the availability of easy to use admin and cofiguration tool and server being available for all major platforms.

Now we have SpringSource,IBM and Sun implementing application servers on OSGi technology, are we seeing the tsunami of OSGi support and implementation? Even thought these changes do not provide a visible change to the end users, JEE seems to be adopting OSGi technology at a large scale. I am sure Oracle will come up with an implementation using their (orginally BEA's) mSA technology, which is based on OSGi.

November 02, 2008

Introducing SpringSource dm Server

Spring framework has been an unofficial standard for buliding java based enterprise applications. SpringSource, the company that developed Spring framework recently launched SpringSource dm Server, a completely modular, OSGi-based Java server to run enterprise Java applications. dm server brings simplicity to both developers and operations team.

Since I am a developer, I will start with advantages of have a OSGi based application server. I did play around with the dm server release 1.0. The startup time for the server is very small (15 seconds with my web web applications installed). Major advantage would be the deloyment of new application do not need a server restart. This reduce the development team's development,deplyment and test cycle and provide a platform for Agile development. SpringSource Tool Suite, the IDE from SpringSource is very tighly coupled with the server. My first application and its deployment was seamless. Even though the server supports deployment of application as war file, the company has come up with a different strategy to bundle the application. The application development is also bit different. Application is more OSGi oriented and most of your application's configuration goes into the manifest file and not into web.xml

Onto operations team, they can forget application server restarts on each deployment. Keeping tack of all the lbraries are easy and the server is very configurable. But these are the statements all the other server's boast about right? But other application servers do not provide the flexibity that dm server provides. I would say the server is still young. Currently, the server do not support clustering. But when you have these features built-in, with other features like capability to run EJBs and other java enterprise component it would be great.

The dm Server
The dm server is build on Eclipse Equinox and based on SpringSource Dynamic Module Kernel (dm-kernel). The kernel provides a modular tomcat, spring and other OSGi based technologies. The server is licensed under GPLv3 but some components are EPL and spring framework is licensed under Apache software license. You are free to download the server and its tightly integrated IDE from SpringSource. I would say all J2EE developers should have a look at this server and its programming model.

SpringSource and Its business perspective
Look at all other application server providers: IBM,JBoss,Oracle (Weblogic) or even Apache. All have their own application sotfware stack like Seam of Jboss, ADF of Oracle. Spring framework had the application stack but didn't have a complete product range – Application server and IDE. And now they have it! But the difference they brought in is that they didn't follow the same arhitecture as other companines. They brought in OSGi technoloy into main stream java enterprice basically. With the evolution of dm server, I am sure there will not lot of stuff pushed into OSGi. SpringSource have already recommed new features for OSGi. Now, they have a whole range of product to sell: application server,IDE and of course support for spring framework.

Some developers using Spring are not so happy with how SpringSource handle the support (as far as I understood from the forums). But I see that they have a business model in place to compete with other application servers.

Now, thats one perspective. The other part is, SpringSource being asquired by Microsoft. I don't see much news on it except in InfoQ. In an interview with Rod Johnson and InfoQ they talk about how SpringSource and Microsoft team are working on integrating spring into Windows products like Office, Visual Studio and the OS itself. Will Windows 7 have Spring pre installed? What will its main contribution be to the OS? We still have time to find out!