Let's suppose I have two osgi bundles. There is mvc pattern in each bundle. View - swing JFrame. What is the best practice to communicate between these two components? As I see there are two ways:

1) In service we do service function and inside this function we call controller function. Something like that

Class MyService {
    public void readSomething(){
      controller.readAction();
    }
}

2) Or service may return controller and the other component will use this foreign controller. Something like that:

Class MyService {
    public Controller getController(){
      return controller;
    }
}

I'd like to know what other people use and think.

有帮助吗?

解决方案

In general, it depends :-).

I assume you have two components A and B, and component A needs to notify component B of something, or, in other words, component B is interested in something that happens in component A.

One way for the components to cooperate is to use the EventAdmin service; component A emits events for all possible cases, and component B has registered an EventHandler with an appropriate filter for these events. Bonus: component A can specify sync/async handler execution, and there may be multiple components that will be notified without any extra overhead. Both A and B need not be services or even objects, if the bundle activators handle all the wiring.

Another way is to use Declarative Services: components A and B are objects, and you have defined an interface IB that specifies the valid operations on B, and which B implements. A is marked as needing at least one IB, and the DS service will instantiate both and invoke methods on A with the proper IB references. A can then normally call methods of IB.

As a simplification of DS, you can use normal services: A isn't a service, but it finds instances of IB everytime it needs to invoke methods on IB.

Last but not least, there's the Blueprint Service that can instantiate complex object graphs using dependency injection, including service proxies, which I have never used and don't feel that I can comment on...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top