Saturday, January 31, 2015

[OSGi][Service] Creating a service and calling it from other bundle

Hi,

We are going to create a service, export it and import in another bundle. If you did not create a simple bundle yet, follow the post [OSGi] Hello World from OSGi | Create your first OSGi Bundle

We are going to create a classic ECHO service. Whatever is given to the service is ECHOed back to us.

After you are done creating a simple bundle HelloFromOSGi. Follow the steps below.


Step 1. Create a new bundle following the same steps to create HelloFromOSGi. Lets name that as EchoService. So the project name is me.rkg.osgi.EchoService. At this point, both bundles are same except names.

Step 2. Create an EchoService interface and EchoServiceImpl class. Lets keep them in me.rkg.osgi.

EchoService Interface 

package me.rkg.osgi.echoservice.service;
public interface EchoService {
public String echo(String inputString);
}
EchoServiceImpl

package me.rkg.osgi.echoservice.service.impl;
import me.rkg.osgi.echoservice.service.EchoService;
public class EchoServiceImpl implements EchoService {
@Override
public String echo(String inputString) {
return inputString;
}
}
Step 3. Updating Activator to register service. We will use org.osgi.framework.ServiceRegistration. We can register service with help of BundleContext which is passed to start() method.
package me.rkg.osgi.echoservice;
import me.rkg.osgi.echoservice.service.EchoService;
import me.rkg.osgi.echoservice.service.impl.EchoServiceImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
public class Activator implements BundleActivator {
ServiceRegistration<EchoService> echoServiceRegistration;
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
System.out.println("Registering Echo Service");
EchoService echoService = new EchoServiceImpl();
echoServiceRegistration = context.registerService(EchoService.class, echoService, null);
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Unregistering Echo Service");
echoServiceRegistration.unregister();
}
}
view raw Activator.java hosted with ❤ by GitHub
Step 4. Updating MANIFEST.MF. We need to export the service using
Export-Package: me.rkg.osgi.echoservice.service 
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: EchoService
Bundle-SymbolicName: me.rkg.osgi.EchoService
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: me.rkg.osgi.echoservice.Activator
Bundle-Vendor: Ravi Kumar Gupta
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: org.osgi.framework;version="1.3.0"
Export-Package: me.rkg.osgi.echoservice.service
view raw MANIFEST.MF hosted with ❤ by GitHub
Step 5. See that we export only interface not our implementations in the last step.

Step 6. Now when service is exposed, we can import into HelloFromOSGi bundle and use it. Lets modify MANIFEST.MF for HelloFromOSGi to add
Import-Package: me.rkg.osgi.echoservice.service, org.osgi.framework;version="1.3.0"
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloFromOSGi
Bundle-SymbolicName: me.rkg.osgi.HelloFromOSGi
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: me.rkg.osgi.hellofromosgi.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: me.rkg.osgi.echoservice.service, org.osgi.framework;version="1.3.0"
view raw MANIFEST.MF hosted with ❤ by GitHub
Step 7. Now we can use this service in any of the files in HelloFromOSGi. We will use org.osgi.framework.ServiceReference. We can get this reference from BundleContext passed to start() method.

package me.rkg.osgi.hellofromosgi;
import me.rkg.osgi.echoservice.service.EchoService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator {
ServiceReference<EchoService> echoServiceReference;
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
System.out.println("Hello World From OSGi!!");
echoServiceReference = (ServiceReference<EchoService>) context.getServiceReference(EchoService.class.getName());
EchoService echoService = (EchoService) context.getService(echoServiceReference);
System.out.println("Echo :"+ echoService.echo("Hello Ravi!") );
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World --from OSGi!!");
context.ungetService(echoServiceReference);
}
}
view raw Activator.java hosted with ❤ by GitHub
Step 8. Running the bundles. You would be able to see the message.

That's it. Hope this gets you a clear idea.

Until next time :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.