You already have your software in place, but you still need a more effective method of enabling your technical users to expand the platform.
Even though OSGi is well-known to most Java developers, integrating it into your product may seem like a daunting task. The lack of clarity about OSGi’s precise workings further muddles the road forward.
In contrast to anything they would like to experiment with, OSGi feels more like attempting to sail the wide sea to many Java developers.
This article will introduce you to the OSGi Felix spring tutorial using a straightforward program and go through, its advantages, and other important details.
So what is OSGi?
Creating and distributing modules and components is made possible by the Java framework known as OSGi (Open Service Gateway Initiative).
It emphasizes function encapsulation and loose coupling, which offers several advantages to developers, including modular functionality that is readily transferable between source codes and testing that doesn’t need unending dependencies.
How do OSGi works?
A dynamic component system for Java is defined by a set of specifications called OSGi. These specs enable a development approach where an application is built from a number of parts and then packaged into bundles.
Local and network-based services are used by these components to communicate. Making the core code of the program as compact as feasible is the aim. High coupling between this code and several components is possible.
The application’s reusable building elements are called components. Think about features like a shopping cart for your e-commerce website or a payroll application for your staff management system.
Components can use OSGi to hide their implementations from other components and only provide the necessary data to them via services.
This prevents components from unintentionally changing data they shouldn’t have access to and manages dependencies.
All connections between components in an OSGi application are made through services in an ideal world. A Java package contains the API that is specified for services.
The collaboration between service providers and customers requires the use of classes and/or interfaces, which make up the API.
All of the OSGi components are packaged together in bundles, each of which includes the resources they require to function. Bundles are quite clear about what they need from the environment and what they are capable of.
- OSGi applications have access to external bundle repositories.
- The framework allows service-oriented design at the module level.
- It makes it possible to manage the versions and dependencies of application bundles in addition to standardizing and making it easier to integrate third-party libraries.
- Applications are more adaptable to changing demands, more portable, and quicker to reengineer.
- A web application can be deployed as a group of versioned OSGi bundles with a dynamic lifetime thanks to the framework’s integration with the Java EE programming model.
- Instead of being installed as a third-party library as part of the application, the framework provides the declarative assembly and streamlined unit testing of the Spring Framework in a standardized form that is provided as part of the application server runtime.
- For corporate applications made up of several versioned bundles with dynamic lifecycles, the framework offers isolation.
- It contains an integrated bundle repository that can store shared common and versioned bundles used by several applications, preventing each application from deploying a separate copy of each shared library.
Getting started with OSGi Felix spring
Getting the Resources
By downloading Apache Karaf’s most recent version from this site, we can begin our OSGi adventure.
Based on Apache Felix, the Apache implementation of the OSGi specification, Apache Karaf provides a platform for OSGi-based applications.
Karaf provides various helpful features on top of Felix that will aid us in familiarizing ourselves with OSGi, such as a command line interface that will enable us to communicate with the platform.
Entry Point For Bundles
An application must be packaged as an OSGi bundle before it can be run in an OSGi environment. Additionally, the application entry point must be specified; it is not the standard public static void main(String args) function.
So let’s begin by creating a “Hello World” application based on OSGi.
We begin by establishing a basic OSGi API dependency:
The OSGi runtime will have access to the dependency, therefore the bundle does not need to contain it. Therefore, it is marked as supplied.
Now let’s create the straightforward HelloWorld class:
Classes that act as entry points for bundles must implement the OSGi-provided BundleActivator interface.
When the bundle containing this class is launched, the OSGi platform calls the start() function. On the other side, shortly before the bundle is halted, the function stop() is called.
Let’s not forget that each bundle can only have one BundleActivator. Both ways can communicate with the OSGi runtime using the BundleContext object that is given.
Creating a Bundle
Make the pom.xml a true OSGi bundle by making the necessary changes.
We must first express clearly that our goal is to produce a bundle.
Then, we use the maven-bundle-plugin to package the HelloWorld class as an OSGi bundle, courtesy of the Apache Felix community:
The values of the OSGi headers we wish to include in the bundle’s MANIFEST file are specified in the instructions section.
Bundle-Activator is indeed the properly qualified name of the BundleActivator solution that will be employed to start and stop the bundle. It is compatible with the just-created class.
While it is not an OSGi header, the Private-Package header instructs the plugin to add the package to the bundle but just not make it accessible to other plugins. The mvn install command should be used to create the bundle.
Installing and Running the Bundle
Let’s begin Karaf by running this command:
where “KARAF HOME” refers to the folder where Karaf was set up. We can use the following command to install the bundle when the Karaf console prompt appears:
Using the local Maven repository is how Karaf is told to load the bundle.
Returning the bundle’s numeric ID, Karaf writes it out. This ID might differ depending on how many bundles have previously been installed. The bundle has now been successfully installed, and the command to launch it is as follows:
The moment you launch the package, “Hello World by Jay” displays. We can now halt and remove the bundle using:
The console displays “Goodbye World by Jay.”
Developers can easily add new functionality to an existing web application while keeping it secure and isolated from other components of the main program by using OSGi.
We have learned about OSGi in this post, including how it functions, its advantages, and a straightforward program. It is not difficult to incorporate OSGi into your own application.