Gyrex/Learning Material/Develop OSGi DS
|Mailing List • Forums • IRC • mattermost|
|Open • Help Wanted • Bug Day|
|Browse Source • Project Set File|
This article will show you how to use OSGi Declarative Services in Gyrex. As a example the GreetingService will be implemented and can be used in later tutorials to let the consumer "greet". Using Declarative Services the GreetingService will be made available in the OSGi framework. To make this possible an OSGi DS component has to be created. A well structured pattern provides the service as an interface which will be re-implemented in a component class. This class keeps the reference to the real implementation and only delegates to it. You can find here (zip-file) the whole sample-project with all classes described later in the article.
The OSGi Declarative Services Tutorial requires a working Gyrex target plattform as well as Java 6 or Java 7 runtime environment.
This tutorial was created using eclipse Indigo Service Release 2.
Now it's required to import some packages. To do so open the MANIFEST.MF of your project. You will find this file in the folder META-INF. Switch to the register MANIFEST.MF and add following lines:
Import-Package: org.apache.commons.lang;version="[2.4.0,3.0.0)", org.apache.commons.lang.time;version="[2.4.0,3.0.0)", org.eclipse.gyrex.cloud.environment;version="[1.0.0,2.0.0)";resolution:=optional, org.osgi.service.component;version="[1.2.0,2.0.0)", org.slf4j;version="[1.6.0,2.0.0)"
Add following line to the MANIFEST.MF, too.
Bundle-ActivationPolicy: lazyPlease notice the empty line at the end of the text. This is absolutely required
Furthermore you have to add dependencies in the MANIFEST. Switch to register Dependencies to section Automated Management of Dependencies. There you have to add following packages:
With these packages you will be able to start the server at the end of this tutorial with the OSGi framework in which your bundle is available. You still need to implement your service. To do this you have to create a new package with the same name as your project with a right-click on src in your project selecting New -> Package. This package will contain at least 4 classes. In this example they are:
sample.service.GreetingService sample.service.GreetingServiceImpl sample.service.GreetingServiceProvider sample.service.GreetingServiceComponentGreetingService is an interface providing methods. These methods will be implemented later by several separated bundles, which can be added dynamically to the running system. Therefore this class should not be placed in the same bundle as the classes implementing the interface, but in a separated bundle. In this example there are three methods to be implemented.
Colleytion<String> getGreetings() void processGreetings() void sayHello(final String greeting)
With these methods the consumer can "greet" (sayHello), get a list of all sent greetings (getGreetings) and process greetings, which just en The real implementation of the interface are in the class GreetingServiceImpl. The delegation to these methods is done by the class GreetingServiceComponent. There is also an implementation of the methods, but it only delegates to the referenced "real" implementation. Further Methods are implemented to start/activate and stop/deactivate components. The GreetingServiceProvider provides the access to the GreetingService instance.
After you opened this file, switch to the register Source. There, methods activate and deactivate have to be added.
At the end you have to tell your MANIFEST.MF that there is a Service-Component. Open the MANIFEST.MF, switch to the register MANIFEST.MF and add the following line if it isn't already done:
Service Component: OSGI-INF/greetings-service.xml
With clicking Apply and then Debug the server will start. Now open the console view where you can monitor that all the necessary bundles are starting. Once you read the line: [Worker-0] INFO o.e.jetty.server.AbstractConnector - Started SelectChannelConnector@0.0.0.0:3110 first press Enter and you can issue a few console commands to check your components state. Possible commands are:
- 'ss '+name - shows all bundles of "name", their state and ID ('ss sample')
- 'start '+id - starts the bundle called by id ('start 297')
- 'stop '+id - stops the bundle called by id ('stop 297')
- 'ls' - shows all available components
This tutorial ends at this point. Now you have implemented a component in OSGi Declarative Services with the possibilty to change the state of it. This is the basis to implement whole OSGi frameworks. Like we said at the beginning, an interface is normally separated in an extra bundle from which you want to access the real implementations dynamicaly according to the scenario. This will be shown in a next tutorial.