Consuming OSGi services from a Java agent

To access an OSGi service from a Java™ agent, you import the packages of the service and then include the OSGi FrameworkUtil class to access this service from your code.

Procedure

  1. In a Java agent, import the packages that correspond to the OSGi service in the META-INF/MANIFEST.MF file of the Java agent project.
  2. Make sure that the correct version is specified. If the OSGi project is packaged within the solution, the qualifier component must be preserved in the version restriction.
  3. Use the OSGi FrameworkUtil class to get access to your service.
    For example:
    BundleContext ctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
    ServiceReference<org.acme.service.MyService.IMyService>serviceref = null;
    Object service = null;
    if( ctx != null ) {
       serviceref = ctx.getServiceReference("org.acme.service.MyService.IMyService");
       if( serviceref != null ) {
          service = ctx.getService(serviceref); 
       }
    }

Results

When the service is started, you can retrieve the reference to the service.