Using a Specific Service Implementation from Registered Multiple OSGI Service Implementations

Shan Chathusanda Jayathilaka
5 min readJul 15, 2020

--

Hi guys, hope you guys are doing well and staying safely at your home. Today I am going to discuss how to use a specific implementation of a registered OSGI service. If you are new to OSGi (Open Services Gateway initiative) you can refer this blog post to get an idea.

Ok, let’s swim through the coding. First of all we need to have an OSGi service and an OSGi service consumer. I am going to keep this blog as simple as possible and I am not going to explain how to implement these components here. You can create these components by referring this blog post which is mainly written for this purpose.

https://ubiqum.com/blog/java-basics-explained/

As same as the aforementioned blog post I am going to use a WSO2 Carbon Platform to publish these OSGi components because WSO2 Carbon Platforms are based on OSGi. You can download the carbon platform from here.

Let’s go deep.

Now I am going to create another implementation class from the GreetingCardProducer interface. I will name it as GreetingCardProducerImplA and this will be located inside the org.wso2.carbon.greeting.card.producer package.

Now we need to register the newly implemented class to the same interface as an OSGi service. That can be done by adding the following code to the GreetingCardProducerServiceComponent class which is located in the org.wso2.carbon.greeting.card.producer.internal package.

Now the full GreetingCardProducerServiceComponent file should be like this.

Now we have successfully registered the second implemented class for the GreetingCardProducer interface. Now we need to get the registered instance of GreetingCardProducerImplA in order to create a greeting card from that producer. Let’s go to the consumer module (org.wso2.carbon.greeting.card.consumer). In this consumer module you can find the GreetingCardConsumerComponent class inside the org.wso2.carbon.greeting.card.consumer.internal package. Here we are going to do some modifications. First we will add the changes and then I will describe why we are changing these.

First go to the activate method and remove the following code segments. We will use these code segments in a coming step. Don’t worry.

NOTE : If you want these code segments to compare the difference please make sure to comment out them.

Now after the activate method you will see the setGreetingCardProducer method. On top of this method there is a reference code segment which describes the OSGi service details which will be considered in the setGreetingCardProducer method. This is annotated with @Reference annotation. Inside this reference you can see the cardinality reference. Change this cardinality to ReferenceCardinality.MULTIPLE. After this change the final reference code segment will be like the following one.

Now let’s go inside the setGreetingCardProducer method. Here we will do the following change to the existing code segment.

NOTE : If you want these code segments to compare the difference please make sure to comment out them.

This is the latest GreetingCardConsumerComponent class.

Okey. Now all the code changes are done. As I promised previously I will explain why we need to make those changes.

In the activate method we removed the GreetingCardConsumer object creation and calling of the requestGreetingCard method. As you can remember we registered two implementations for the same service at the GreetingCardProducer level. But previously there was only one. So in this scenario we need to get a specific service implementation and call the relevant methods. So I moved them inside the setGreetingCardProducer method.

Now in the reference code segment we changed the cardinality as ReferenceCardinality.MULTIPLE. Here the same reason as the previous. Since we are registering multiple implementations for a particular service and we need to get a specific implementation from those registered implementations we need to tell the setGreetingCardProducer method there are more than one service implementations for the service mentioned in the reference code segment. In this scenario that is service = GreetingCardProducer.class. If you need to know further about cardinality, you can refer this.

In the setGreetingCardProducer method we made some major changes. Here first we add a condition to check the class name of the OSGi service which is defined as a parameter for this method. In this scenario the OSGi service is the GreetingCardProducer interface and the service implementation we need is GreetingCardProducerImplA. Here OSGi will iterate the service implementations which are registered for the particular service. From that iteration we check the name of that service implementation and set the corresponding service implementation to the DataHolder class. In this scenario this class is GreetingCardConsumerDataHolder. After that we will create a GreetingCardConsumer object and call it’s requestGreetingCard method to request a greeting card creation from GreetingCardProducerImplA.

Got it. Let’s build the project and test this. Go to the parent directory of this project and open the terminal. Run maven clean install and if the build is success you may able to find the following jars.

org.wso2.carbon.greeting.card.consumer-1.0-SNAPSHOT.jar inside <PARENT-DIR>/org.wso2.carbon.greeting.card.consumer/target

org.wso2.carbon.greeting.card.producer-1.0-SNAPSHOT.jar inside <PARENT-DIR>/org.wso2.carbon.greeting.card.producer/target

Now copy the above two jars into the dropins directory located in <PRODUCT_HOME>/repository/components/dropins in the downloaded WSO2 carbon platform. After copying the jars, go to the <PRODUCT_HOME>/bin directory, open a terminal and start the carbon platform by executing the following command.

In Linux Environment

sh wso2server.sh

In Windows Environment

wso2server.bat

You can find the following logs in the terminal.

Here in the logs you can see the following log line which we added in the createGreetingCard method in GreetingCardProducerImplA class.

Created a greeting card Birthday with greeting: Happy Birthday! from GreetingCardProducerImplA producer.

This means we are all good. We got the exact service implementation from the consumer and we created a greeting card from that implementation. Greetings my friends.

If you want to run the WSO2 carbon platform in OSGi mode you can refer the Deploy and Test section of this.

Ok guys. Hope you find something interesting in this. Let’s meet with another discussion.

Ayubowan…!!! Stay Home, Stay Safe.

References

--

--

Shan Chathusanda Jayathilaka

Senior Software Engineer @ WSO2 | Graduate in Computer Science, University of Ruhuna