Monday, October 24, 2011

Trial by Fire

The best way to learn is by doing, so this blog journals the process of developing a distributed application with OSGi and Declarative Services with Java.

There is a public GIT, with all of the application work available for cloning at: git://github.com/GUSCRAWFORD/ECL-Assignment1.git

Our sample project is to create a declarative service with Java, and have a client-end 'Hotel Room Booking Software' use these services.

Resources



Setting Up
For my purposes, I've chosen Eclipse to perform my work with. Eclipse itself is based on OSGi, and I'm also using Equinox OSGi implementation.

To get started, we're going to create a new bundle in Eclipse.

This bundle is going to get defined as a service in XML.  The service in this case offers the capability to book a hotel room on a central computer.

Defining Hotel Service
The first thing I'm doing is creating a class and implementing java.lang.Runnable.
This runnable class is my service as I understand it.  It should get defined as a service.  Business logic and centralized transactions happen in here.  So we've got a source file HotelService.java in a package org.ecl_assignment1.ds that looks like this;
// HotelService.java
package org.ecl_assignment1.ds;

public class HotelService implements java.lang.Runnable {

    @Override
    public void run() {
        // TODO : SOME WORK
    }
}
The next step is declaring my service which I do within an xml. The tutorial I'm initially going by says to create a folder OSGI-INF in the project root; to create a *.xml file there with a filename matching that of the class-service I'm declaring. I'm going to use the already created META-INF folder for the time being.
Observe HotelService.xml in project-root/META-INF
<?xml version="1.0" ?>
<component name="hotelservice">
    <implementation class="org.ecl_assignment1.ds" />
    <service>
        <provide interface="java.lang.Runnable" />
    </service>
</component>

No comments:

Post a Comment