1
votes

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
What makes you think that two bundles cannot import from each other? Of course they can. However it just makes a nonsense of the modularity... if two modules depend directly upon each other in such a tight loop, then just make them into a single module. - Neil Bartlett
I said that because Eclipse is showing the cycle error by displaying a red exclamation mark on both projects whenever I make import amongst two bundles. - Excite
Then Eclipse is lying to you. In OSGi it's perfectly possible to have a cyclic dependency (though not a good idea, as I described). - Neil Bartlett

1 Answers

2
votes

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.