Creating a Custom OSGi Service

It’s time to implement your OSGi service. Make sure to examine the service and service reference details, if you haven’t done so already. Here you’ll create a custom service that implements the service interface, declares it an OSGi service of that type, and makes it the best match for binding with other components.

The example custom service CustomServiceImpl (from sample module overriding-service-reference) implements service interface SomeService, declares itself an OSGi service of the SomeService service type, and even delegates work to the existing service. Examine this example code as you follow the steps for creating your custom service.

@Component(
    property = {
        "service.ranking:Integer=100"
    },
    service = SomeService.class
)
public class CustomServiceImpl implements SomeService {

    @Override
    public String doSomething() {

        StringBuilder sb = new StringBuilder();
        sb.append(this.getClass().getName());
        sb.append(", which delegates to ");
        sb.append(_defaultService.doSomething());

        return sb.toString();
    }

    @Reference  (
        target = "(component.name=override.my.service.reference.service.impl.SomeServiceImpl)"
    )
    private SomeService _defaultService;
}

Here are the steps to create a custom OSGi service:

  1. Create a module.

  2. Create your custom service class so that it implements the service interface you want. In the example above, CustomServiceImpl implements SomeService. Step 5 (later) demonstrates implementing the interface methods.

  3. Make your class a Declarative Services component that is the best match for references to the service interface:

    • Use an @Component annotation and service attribute to make your classes a Declarative Services (DS) component. This declares your class to be an OSGi service that can be made available in the OSGi service registry. The example class above is a DS service component of service type SomeService.class.

    • Use a service.ranking:Integer component property to rank your service higher than existing services. The "service.ranking:Integer=100" property above sets the example’s ranking to 100.

  4. If you want to invoke the existing service implementation, declare a field that uses a Declarative Services reference to the existing service. Use the component.name you copied when you examined the service to target the existing service. The example above refers to an existing service like this:

    @Reference  (
        target = "(component.name=override.my.service.reference.service.impl.SomeServiceImpl)"
    )
    private SomeService _defaultService;
    

    The field lets you invoke the existing service in your custom service.

  5. Override the interface’s methods. Optionally, delegate work to the existing service implementation (see previous step).

    The example custom service’s doSomething method delegates work to the original service implementation.

  6. Register your custom service with the OSGi runtime framework by deploying your module.

Components that reference the service type you implemented and whose reference policy option is greedy bind to your custom service immediately. Components bound to an existing service and whose reference policy option is reluctant can be dynamically reconfigured to use your service. That’s demonstrated next.

OSGi Services and Dependency Injection with Declarative Services

« Examining an OSGi Service to OverrideReconfiguring Components to Use Your OSGi Service »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0