Best practice OSGi

I would like to know what is considered the best method or patterns for decoupling application code from framework code, in particular with regard to OSGi.

I am going to use an example from the Felix SCI pages

An example of a service is a Comparator.

package sample.service;
import java.util.Comparator;
public class SampleComparator implements Comparator
{
    public int compare( Object o1, Object o2 )
    {
        return o1.equals( o2 ) ? 0 : -1;
    }
}

In the above code there is no framework plumbing, it is focused and concise. When using this application, when using OSGi, you must register it in the services registry. One of the methods described on the linked Felix pages is to use the Service Component runtime.

// OSGI-INF/sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<component name="sample.component" immediate="true">
  <implementation class="sample.service.SampleComparator" />
  <property name="service.description" value="Sample Comparator Service" />
  <property name="service.vendor" value="Apache Software Foundation" />
  <service>
    <provide interface="java.util.Comparator" />
  </service>
</component>

and

Service-Component: OSGI-INF/sample.xml

Everything is pleasant and beautiful, my service implementation is not related to OSGI at all.

Now I want to use the service ...

package sample.consumer;
import java.util.Comparator;
public class Consumer {
    public void doCompare(Object o1, Object o2) {
        Comparator c = ...;
    }
}

Using the SCR search strategy, I need to add methods only for the framework:

protected void activate(ComponentContext context) {
    Comparator c = ( Comparator ) context.locateService( "sample.component" );
}

SCR, :

protected void bindComparator(Comparator c) {
    this.c = c;
}

protected void unbindComparator(Comparator c) {
    this.c = null;
}

, , , , , , .

, , OSGi , .

package sample.internal;
public class OsgiDependencyInjector {
    private Consumer consumer;
    protected void bindComparator(Comparator c) {
        this.consumer.setComparator(c);
    }

    protected void unbindComparator(Comparator c) {
        this.consumer.setComparator(null);
    }
}

, SCR.

org.apache.felix.scr.annotations, , , maven-scr. , AFAICT, .

, , , , OSGi "" ?

+5
3

1) , , bean ( setXXX ). , .

2) bnd ( maven, ant, bndtools, eclipse ..), bnd. bnd ( ) xml.

package sample.service;
import java.util.Comparator;
import aQute.bnd.annotations.component.*;

@Component
public class SampleComparator implements Comparator {
    public int compare( Object o1, Object o2 ) {
        return o1.equals( o2 ) ? 0 : -1;
    }
}

@Component
class Consumer {
    Comparator comparator;

    public void doCompare( Object o1, Object o2 ) {
      if ( comparator.compare(o1,o2) ) 
        ....
    }

    @Reference
    protected setComparator( Comparator c ) {
       comparator = c;
    }
}

:

Service-Component: *

bnd. , OSGi . unset, bnd . , set , , , unset. , Consumer μservice, . bndtools, bnd , μservices.

PS. , o1 , , o2, o1!= O2, Comparator .

+3

, . OSGi Fuse ESB, - Apache Karaf . , Spring DM (http://www.springsource.org/osgi), OSGi. " Equinox 3.2.x, Felix 1.0.3+ Knopflerfish 2.1.x " ( ).

:

  • "osgi" xml -
  • OSGi.

?

  • OSGi:

< osgi: service id = "some-id" ref = "bean -implementing-service-to-expose" interface = "interface-of-your-service" /" >

  • OSGi:

< osgi: reference id = "bean -id" interface = "interface-of-exposure-service" /" >

, OSGi maven-bundle-plugin.

+2

The advantage of felix annotations over those of aQute.bnd.annotations.component is that the bind and unbind methods are automatically created by the felix scr plugin (you can annotate a private field). The disadvantage of the felix plugin is that it acts on Java sources and therefore does not work for class files created in other languages ​​(for example, scala).

+1
source

All Articles