Question

I've just got my hands-on with OSGI technology and I've few basic queries. Here is what I've done:

Created a HelloWorld interface in a package called "com.mypackage.osgi.bundle.service". This interface exposes a method as : public String sayHello(String arg);

Created a HelloWorldImpl class in a package called "com.mypackage.osgi.bundle.service.impl". This class implements the HelloWorld interface and provides an implementaion for sayHello() method.

Created a HelloWorldActivator class in a package called "com.mypackage.osgi.bundle.activator". This class implements the BundleActivator interface and implements the start() and stop() methods of interface.

In the start() method, I'm registering this bundle as a service via "BundleContext". The code is as shown below:

public class HelloWorldActivator implements BundleActivator {

ServiceRegistration helloServiceRegistration;

public void start(BundleContext context) throws Exception {

    HelloWorld helloService = new HelloWorldImpl();
    helloServiceRegistration =context.registerService(HelloWorld.class.getName(), helloService, null);    System.out.println("Service registered");    }

public void stop(BundleContext context) throws Exception {

    helloServiceRegistration.unregister();    System.out.println("Service un-registered");
} }

I'm then packaging this project as an OSGI bundle using maven plugin and deploying it on an OSGI container (Equinox). In the instructions I've exposed the interface as export-package. I can see that my OSGI bundle has successfully got deployed as a service on the OSGI container (The bundle id says ACTIVE state and also I can see the "Service registered" output on osgi console).

Now my next step is to consume the above OSGI bundle as a service. I understand that in order to do this, I can use "ServiceReference".

Suppose I'm now creating a totally new java project (so there is no link in this project's workspace with the one created above) such that it will act as a consumer to the service created above.

My query is - Do I need to create a "copy" of HelloWorld interface in this new java project ? In other words, I need to have this interface as a "stub" in this new project's workspace ?

The reason I ask this is because, if I do not have a copy of "HelloWorld" interface in my new project's workspace, I'm going to have compilation errors at line 2 and 3 mentioned below.

public class ConsumerActivator implements BundleActivator {

ServiceReference helloServiceReference;

public void start(BundleContext context) throws Exception {

    helloServiceReference= context.getServiceReference(HelloWorld.class.getName()); //2
    HelloWorld helloService =(HelloWorld)context.getService(helloServiceReference); //3
    System.out.println(helloService.sayHello("Test user"));

}
public void stop(BundleContext context) throws Exception {

    context.ungetService(helloServiceReference);
} }

So, is it correct to say that the consumer bundle should have "stubs" of the service interfaces that it intends to use ?

Sorry if it sounds a very basic question but just need to clarify as I couldn't find any mention of it anywhere on the net. All the examples provided assume that both consumer and service are part of same code workspace.

Many thanks in advance to clarify.

Best Regards LB

No correct solution

OTHER TIPS

No, just create a dependency between the two projects.

Also, you should really not use the low level OSGi APIs if you are just starting out with OSGi. This is liking trying to hack the Linux kernel before you've even learning how to write a shell script! Please start with Declarative Services... see the tutorial here: http://bndtools.org/tutorial.html

No, it is a best practice to have a separate bundle containing the interface as part of an API bundle. So you can have the interface separate from the actual service provider. However, in your case this approach is way over the top and I would colocate the HelloWorld interface and its (service) implementation in the same bundle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top