简体   繁体   中英

Remote OSGI Service call

Still trying to learn OSGI. However can someone tell me if this is possible or where I might be going wrong with this sample code.

So I have two separate bundles that run in service mix.

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        context.registerService(Checker.class.getName(), new CheckerImpl(), null);
    }
}

public interface Checker{
    public void checkMe()
}

public class CheckerImpl implements Checker{
    public void checkme(){
        System.out.println("Hello World");
    }
}

The bundle Above contains an Interface called Checker which has one function runCheck() and a class that implements this interface (where in this case runCheck() just outputs hello world.)

This is the "remote" bundle.

public class RemoteActivator implements BundleActivator {

 public void start(BundleContext context) throws Exception{
    serviceRef = context.getServiceReference(SVC_REF_CERTCHECK);

    if (serviceRef == null) { 
        logMsg("Servicereference is null");
        throw new NullPointerException();
        }
    else{
        logMsg("Check: " + context.getService(serviceRef).getClass().toString());


        Object svc = context.getService(serviceRef);

        logMsg(svc.toString());

        Checker check = (Checker) svc;

        svc.runCheck();
}

public void logMsg(Object o){
    System.out.println(o);
}
}

public interface Checker{
    public void checkMe()
}

With this bundle we have another interface called Checker (which contains the same code as the interface in the 1st bundle) however whenever I run it get to the

Checker check = (Checker) svc;
svc.runCheck();

and stop working (doesn't even give me an error message). I know that the object it received is the other service as I can run

svc.toString()

and it will run the code from the overload method within the CheckerImpl.

Do I need to have a dependency of the .jar bundle within the remote calling service?

Thanks All!

You have two Checker interfaces: one in each bundle. Those are loaded by each bundle's class loader and are thus different classes as far as the VM is concerned. The bundle implementing the service and the client bundles using the service must share the same service interface class. This is why one bundle needs to export the package containing the service interface class as the API and the other bundles need to import that package. Services are built on top of type sharing.

So what I was meant to do is not add a new Interface in the remote / client Bundle and instead, I need to add the dependency of the main bundle to my pom.xml.

in the main service class I need to make sure the pom.xml export the required packages / classes.

<Export-Package>
    my.pachage.bundle.*;
</Export-Package>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM