Callback and passing object reference in OSGi

971 views Asked by At

Bundle-A binds a package from Bundle-B using declarative services in Eclipse Environment.

Then Bundle-A sends a message to Bundle-B by passing a 'data' and 'a reference of a class object' that should get the response to this message as an argument. Eg. send(data, EgClass_1.this);

Bundle-B should process the message and send the response back to the class in Bundle-A that is awaiting response.

Unfortunately that is not possible in OSGi as it creates a cycle. Two bundles cannot import each other.

I wanted to pass reference to a class object so that Bundle-B can call a method on it to get information rather than passing too many arguments but most importantly so that Bundle-B can keep track of which class instance it should call the callback on. I will have multiple instance of the class & its child classes.

As a work around I separated Bundle_A into two, the interfaces and the implementation classes. This way Bundle_A can bind Bundle_B and also Bundle_B can import the Interface definition of Bundle_A interface so that it can work with the object reference passed as parameter.

But the above approach does not feel clean and in coincide with OSGi principles. Is there a better approach for this kind of two way communication or am I doing it right? Thanks in advance!

1

There are 1 answers

2
Christian Schneider On BEST ANSWER

If I understood correctly you want to send data using the send call and be called back when B finishes. Bundle A needs to know the service interface to make the send call. So you will always have a dependency A->B. So to avoid a loop I would also define the callback interface in B. Some class in A can then implement the callback interface and you send the object instance as second parameter. B then just needs to know the callback interface.