You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

This page describes the CXF Distributed OSGi with Declarative Services demo.

The Declarative Services demo uses a DS implementation to create a remoted OSGi service from a DS component. The consumer side uses DS to create a component that consumes the remote OSGi service. By using Declarative Services, you don't need to write code to interact with the OSGi Service Registry. That's all handled through injection which hugely simpliefies the code.
Declarative Services is similar to Spring-DM/OSGi Blueprint in that service dependencies are satisfied through injection. There are a few differences as well. DS is lighter weight than Spring-DM but also has less features. Declarative Services have been part of the OSGi specifications since version 4.0.

This demo can be used with any DOSGi distribution, in this document the single-bundle distribution is used with the Equinox implementation of DS.

Demo design

This demo is quite similar to the Spring-DM demo and the Greeter demo in structure. It consists of 3 bundles:

  • An interface bundle defining the Adder Service interface.
  • An Adder Service implementation bundle.
  • An Adder Service consumer bundle.

The service implementation and consumer bundle are built using DS.
@@@@ image
The Adder Service interface is as follows:

public interface AdderService {
    int add(int a, int b);
}

The Adder Service Implementation

The service implementation providers a simplistic implementation of the AdderService interface, which is instantiated as a DS component.

In the OSGI-INF/component.xml file the AdderServiceImpl is instantiated and registered with the OSGi service registry with the distribution properties. These properties instruct. Distributed OSGi into making the service available on http://localhost:9090/adder.

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
  <implementation class="org.apache.cxf.dosgi.samples.ds.impl.AdderServiceImpl"/>
  
  <property name="osgi.remote.interfaces">*</property>
  <property name="osgi.remote.configuration.type" value="pojo" />
  <property name="osgi.remote.configuration.pojo.address" value="http://localhost:9090/adder" />
  
  <service>
    <provide interface="org.apache.cxf.dosgi.samples.ds.AdderService"/>
  </service>
</scr:component>

So let's install the server side in Equinox, together with the Equinox DS implementation. You can do this from the Equinox command line, but in this document I'll launch Equinox from within Eclipse.
I'm starting off by importing the Single Bundle Distribution as a binary project in Eclipse by going File -> Import | Plug-ins and Fragments and then I select the directory that contains the single bundle distribution JAR file. My workspace now looks like this:
@@@ image
Next I'll create an OSGi Framework launch configuration that includes DS. To do this

  1. deselect the 'Target Platform' tickbox in the Eclipse Launch configuration screen
  2. select org.eclipse.equinox.ds
  3. hit the 'Add Required Bundles' button
    @@@ image
    Now run the OSGi container, you will get a setup like this:
    osgi> ss
    
    Framework is launched.
    
    id	State       Bundle
    0	ACTIVE      org.eclipse.osgi_3.5.0.v20090520
    1	ACTIVE      org.eclipse.equinox.util_1.0.100.v20090520-1800
    2	ACTIVE      org.eclipse.osgi.services_3.2.0.v20090520-1800
    3	ACTIVE      cxf-dosgi-ri-singlebundle-distribution_1.1.0.SNAPSHOT
    4	ACTIVE      org.eclipse.equinox.ds_1.1.0.v20090520-1800
    Now I can install the DOSGi DS bundles in the OSGi container directly from the maven repository.
    osgi> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-interface/1.1-SNAPSHOT/cxf-dosgi-ri-samples-ds-interface-1.1-SNAPSHOT.jar 
    Bundle id is 5
    
    osgi> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-impl/1.1-SNAPSHOT/cxf-dosgi-ri-samples-ds-impl-1.1-SNAPSHOT.jar
    Bundle id is 6
    
    osgi> start 6
    ... log messages may appear ...
    
    
    

    You can also

The Adder Service Consumer

  • No labels