Apache ServiceMix 4.0 / interconnect OSGi bundles

From Wiki
Jump to: navigation, search

Contents

Prerequisites

  • knowledge about the OSGi Service Plattform [1]
  • Apache ServiceMix 4.0 installed [2]
  • Eclipse Galileo [3]
  • basic knowledge about programming with Java

Introduction

This tutorial shows how to connect OSGi bundles via an interface.

Starting Apache ServiceMix 4.0

  • If you don't know how to start Apache ServiceMix follow the instructions on this page: [4]
  • When the application is correctly up and runnig you'll see the ServiceMix prompt
ServiceMixPrompt.png

Create the core project

  • this project just offers the interface
  • select: File > New > Project > Plug-in Project
  • choose a name for your project: e.g. de.btu.dbis.InterfaceCore
  • choose Target Plattform: OSGi Framework "Standard" and click next
  • afterwards click finish
  • Then you can delete the bundle activator class. We don't need it anymore.
  • Now we want to create an Interface. right click on your project and choose New > Interface
  • choose the package and a name for your interface then click finisch
BIInterface.png
  • add some methods in your interface
package de.btu.dbis.interfacecore;

public interface IBundleInfo {

	public String getString();
	
	public String getString(String aString);
	
}

Modify the Manifest

  • open the Manifest file
  • delete the activator class from the Manifest
  • click the runtime tab
  • add your package to the Exported Packages
  • Now your package is public and can be seen by other bundles.
BIInterfaceMF.png

Create a JAR-File an deploy your core bundle

  • right click the project explorer choose: Export > Deployable plug-ins and fragments > Next
  • choose your project and a destination afterwards click finish
  • to deploy the jar file open the Apache ServiceMix prompt
  • type: osgi/install file:///<path to your bundle>.jar
  • or just put your bundle into the Apache ServiceMix deploy folder
  • afterwards you can see the new bundle id
  • start your bundle with: osgi/start <bundle id>

Create a service bundle

  • create a new project
  • open the Manifest and choose the Dependencies tab
  • add to the Required Plug-ins the core package from the other bundle
  • just click add and type the package name
BServiceMF.png

Implement the interface

  • create a new class and implement the interface
  • the interface is visible to the project because we added it to the Required plug-ins
  • if it isn't visible to your package be sure that your Manifest is stored
package de.btu.dbis.servicenoone;

import de.btu.dbis.interfacecore.IBundleInfo;

public class ServiceOne implements IBundleInfo {

	public String getString() {
		return "Bundle number one";
	}

	public String getString(String aString) {
		return "Bunlde number one: " + aString;
	}
}

Modify the activator class

  • now we want to register this bundle as a service
  • open the activator class an use the method registerService form the bunlde context
package de.btu.dbis.servicenoone;

import java.util.Hashtable;

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

import de.btu.dbis.interfacecore.IBundleInfo;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		context.registerService(IBundleInfo.class.getName(), 
					new ServiceOne(), new Hashtable());
		System.out.println("Service No One is registered");
	}

	public void stop(BundleContext context) throws Exception {
	}

}

Deploy and start the service

  • like before deploy your service
  • you should see the new bundle id
  • use osgi/start <bundle id> to start your bundle
BServicePrompt.png

Create a consumer for the service

  • now you can create a consumer for your service bundle
  • create a new project
  • in the Manifest choose the Dependencies tab and add the core interface to the Required Plug-ins
  • afterwards you can implement the bundle activator
package de.btu.dbis.consumer;

import java.util.Scanner;

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

import de.btu.dbis.interfacecore.IBundleInfo;

public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {

		ServiceReference[] references =
			context.getAllServiceReferences(IBundleInfo.class.getName(), null);
		
		//just eye candy
		int i = -1;
		Scanner in = new Scanner(System.in);
		while(i!=0) {
			printMenu();
			i=in.nextInt();
			
			switch (i) {
			case 1:
				for(ServiceReference sr : references) {
					System.out.println(">> " + ((IBundleInfo)context.getService(sr)).getString());
				}
				break;
				
			case 2:
				for(ServiceReference sr : references) {
					System.out.println(">> " + ((IBundleInfo)context.getService(sr)).getString("test"));
				}
				break;

			case 3:
				for(ServiceReference sr : references) {
					System.out.println(">> " + sr.getBundle().getBundleId());
				}
				break;

			case 4:
				for(ServiceReference sr : references) {
					System.out.println(">> " + sr.getBundle().getSymbolicName());
				}
				break;
				
			case 5:
				System.out.println(">> bundle count: " + references.length);
			}
		}
	}
	
	private void printMenu() {
		System.out.println("\n=== Menu ===");
		System.out.println("0: quit");
		System.out.println("1: list all bundles / getString()");
		System.out.println("2: list all bundles / getString(aString)");
		System.out.println("3: list all bundles / id");
		System.out.println("4: list all bundles / name");
		System.out.println("5: bundle count");
		System.out.println("\n");
	}

	public void stop(BundleContext context) throws Exception {
	}
}

Deploy the consumer

  • deploy the consumer like before the service
  • test the implementation
  • implement and test a few more services
BServicePromptTest.png
Personal tools