Osgi why use




















What is OSGi's focus? Modularity, class loading control, and small services that have a very focused purpose or even just a single purpose. That sounds a lot like micro-services. The main difference is that micro-services are expected to each run in their own containers allowing for more control over development and updates, whereas an OSGi container might be host to multiple services.

OSGi is not required. This is not without limitation, as running on the EAP Camel subsystem places some restrictions on Apache Camel components - some are and some are not compatible. Learning OSGi is definitely worth your time, just don't get caught up in the details at least in the beginning. First, focus on learning something that takes advantage of OSGi, like Camel. Then, once you have gotten your feet wet with some basics, start slow and with simple examples for OSGi.

Pay attention to the modularity, class loading, and concepts of OSGi rather than looking at how to configure a complex project to run in Karaf. Even if the future is micro-services, learning and understanding more about modularity and class loading will help will all of your future Java development. To create a service bundle, we need to provide both an interface and an implementation class.

Listing 4 is a simple interface. The only method is a addNum method that will do what it implies: return the addition of two numbers.

The implementation shown in Listing 5 is equally simple but adds a couple of OSGi-specific methods. This client will again make use of the BundleActivator interface. It will also add the ServiceListener interface, as shown in Listing 6.

Listing 6 has a start method that will add a service listener. This listener is filtered by the class name of the service we created in Listing 5. When the service is updated, it will call the serviceChanged method, as shown in Listing 7.

Note that the serviceChanged method is used to determine what event has occurred for a service we are interested in. The service will then respond as specified. It provides a defined lifecycle for hosting bundles in the client, and it supports bundles interacting as clients and services within the container. All of these capabilities taken together provide an interesting alternative to standard Java runtimes and frameworks, especially for mobile and IoT applications.

This story, "What is OSGi? A different approach to Java modularity" was originally published by JavaWorld. He believes in people-first technology. When not playing guitar, Matt explores the backcountry and the philosophical hinterlands. These are, for example, triggered if a new bundle is installed or de-installed or if a new service is registered. Once the service is no longer used, you must unregister the service with OSGi. OSGi counts the usage of services to enable the dynamic replacement of services.

So once the service is no longer used by your implementation, unregister it. This is demonstrated by the following code:. In the registerService method from the BundleContext class you can specify arbitrary properties in the dictionary parameter. You can use the getProperty method of the ServiceReference class from the org.

A bundle can acquire a service via the BundleContext class. The following code demonstrates that. A bundle can define a Bundle-Activator Activator in its declaration. This class must implement the BundleActivator interface. Tracking multiple services. Access OSGi services via web interface. Webkomponenten mit OSGi — einfach und elegant. OSGi blueprint services. OSGi Runtime Properties. OSGi Blueprint services. If you need more assistance we offer Online Training and Onsite training as well as consulting.

Introduction into software modularity with OSGi An application consists of different components. If a component uses the API from another component, it has a dependency to the other component. OSGi configuration files 2. The following listing is an example for a manifest file containing OSGi metadata. Manifest-Version: 1. Activator Require-Bundle: org.

The following table gives an overview of the OSGi meta-data used in the manifest file. Unique identifier for plug-ins The combination of Bundle-SymbolicName and Bundle-Version uniquely identifies a plug-in. If you change your plug-in code you increase the version according to the following rule set. Defining dependencies of a plug-in To access classes from another plug-in A in the source code of your plug-in B you need to:.

The following screenshots shows an example dependency definition in the Eclipse manifest editor. Dependencies using versions A plug-in can define that it depends on a certain version or a range of another plug-in. Life cycle of plug-ins The OSGi runtime reads the manifest file of a plug-in during startup.

Other OSGi runtimes behave differently, for example Felix always activates all plug-ins. OSGi Services Services provide functionality to other software components. The annotation is used to generate an XML file, which is read once the plug-in gets activated. This type must define a OSGi service immediate component - service is activated as soon as its dependencies are satisfied, does not need to specify a service factory component - creates and activate a new service an request, service is not re-used if it become unsatisfied or unregistered.

Accessing the BundleContext Sometimes you need to access information about the bundle, e. Setting the start level for declarative services You need to ensure that the org.

Good practice for defining services It is good practice to define a service via a plug-in which only contains the interface definition. OSGi Services have their own life cycle, the following states are possible:. More information on OSGi ds services 5. References to other services OSGi service can define dependencies to other services.

Defining the service priority OSGi allows to define multiple service implementations. Configuration As OSGi service component can be configured via key-value pairs properties. Inline You can add properties to a declarative service component via the Component annotation property type element.

Using the OSGi configuration admin The configuration admin service allows to configure properties of services. The following is an example for a servlet registration via the whiteboard pattern. Using the OSGi console 7. Required bundles The following plug-ins are necessary to use the OSGi console. Accessing the OSGi console via a telnet client If you want to access your application via a Telnet client, you can add another parameter to the -console parameter.

Exercise: Create a plug-in for a data model In this exercise you create a plug-in which contains the the definition of a data model. Create the plug-in for the data model Naming convention: simple plug-in. No Activator No contributions to the user interface Not a rich client application Generated without a template.

Press the Finish button on this page to avoid the usage of templates. Create the base class Create the com. You see an error for your final id field. This error is solved in the next section. Ensure that you have created both constructors, because they are required in the following exercises. Why is the id field marked as final? At this point the resulting class should look like the following listing. Write a copy method Add the following copy method to the class.

Add constants for its field To access later the fields on Task create the following constants in the class. Validate The resulting Task class should now look similar to the following. LocalDate ; import java. Define a service interface to access tasks Create the following TaskService interface. List ; import java. Optional ; import java. Define the API of the model plug-in Export the com. Next Steps In the following exercises, you will implement and use an OSGi service which uses this data model.

Check arguments Switch to the Arguments tag. In the next exercise you define an OSGi service which uses this plug-in. Save a shared file To share you run configuration, you can also save the file.

Terminate running OSGi instance As you specified the osgi. Use the exit command or the terminate button to terminate the running instance. Exercise: Implementing and providing an OSGi service In this exercise you create a plug-in for a service implementation.

Define the dependencies in the service plug-in To use classes from other plug-ins in a plug-in, you need to add a dependency to them. Manifest-Version : 1. Java 11 is used in this example, a higher Java version will also work. Enable annotation processing and plug-in activation You want to generate OSGi service metadata based on annotations in classes. Provide an implementation of the TaskService interface Create the com. ArrayList ; import java.

AtomicInteger ; import java. Consumer ; import java. Collectors ; import org. Component ; import com. Task ; import com. Add your new plug-in to the product Add this new plug-in to your product via the feature. Create a new plug-in and Create another simple plug-in named com. Add manifest dependencies Add the required dependency in the manifest to use the TaskService interface and the Component annotation.

Activate ; import org. Component ; import org. Reference ; import com. Add to your product Add the new plug-in to your product via your feature. Validate Start again via the product and ensure you see the output of the Activate on the console. Exercise: Validate Add your com. Exercise: Creating a feature based product for the runtime configuration To manage the plug-ins included in the OSGi runtime, you can use a product configuration file.

Create a feature project Create a feature project named com. If you have already an existing launch configuration you can use that to popular the feature. Create a project to host the product configuration file Create a new project named com. Create a product configuration file Right-click on the com. Press the Finish button.

The file is created and opened in an editor. Add your feature as content On the Contents tab, add your feature as content. Currently on release 7, OSGi has provided a stable and evolvable technology platform for development for open source projects and commercial products for almost two decades.

Widely adopted, yet often unreported, OSGi is used in a broad array of open source and commercial products and solutions ranging from business and life critical applications through to the more mundane.

OSGi significantly reduces complexity in almost all aspects of development: code is easier to write and test, reuse is increased, build systems become significantly simpler, deployment is more manageable, bugs are detected early, and the runtime provides an enormous insight into what is running.

Reuse components to build and manage your highly complex systems. Make code easier to write, test and reuse.



0コメント

  • 1000 / 1000