0

I have a problem relating data transmission between ipojo components during reconfiguration. Here's an example:

  • A component Calcul_1 provides a calculation service to return a value (a+b) (ex: f(a,b)=> (a+b))
  • A component Calcul_2 provides a calculation service to return a value (a*b) (ex: f(a,b)=> (a*b))

These two components implement the same calculation service (ex: f).

  • Now, I have a component CallCalcul that uses the calculation service of Calcul_1. The component CallCalcul calls f(5,6) in the component Calcul_1. Then, the CallCalcul component receives the value of 11.

Problem:

  1. When Calcul_1 receives the value (5,6) (not yet calculate) from CallCalcul, CallCalcul reconfigure by changing connector to Calcul_2, i.e., it binds to Calcul_2. In this case, how can I transmit (5,6) from Calcul_1 to Calcul_2 and return (5*6=30) to CallCalcul ?

  2. When Calcul_1 receives the value (5,6) (and calculate their, i.e. 5+6=11) from CallCalcul, CallCalcul reconfigure. In this case, how can I transmit 11 to Calcul_2 and return this value to CallCalcul?

Federico Zancan
  • 4,846
  • 4
  • 44
  • 60
HNT
  • 147
  • 1
  • 10
  • Have you actually ran into this kind of problem where a service provider changes in the middle of a method, or is it just a scenario you think might happen ? Also, Do you inject your service dependencies with the `Requires` annotation (or equivalent xml) ? – archz Nov 19 '14 at 14:16
  • this is a scenario i am sure it happen. I inject my service dependencies with the Requires . Note: Two components Calcul_1 and Calcul_2 implement same service. I use filter to change binding of CallCalcul to service – HNT Nov 19 '14 at 14:47

1 Answers1

0

As far as I know, ipojo handle synchronization so that this behavior does not happen.

From the ipojo documentation :

The synchronization is managed by iPOJO. As soon as you are 'touching' a dependency in a method, iPOJO ensure that you will keep these objects until the end of the method. Nested methods will share the same service object set.

So as long as you are in the same method (or a nested method), you know that your service has not been modified since the first time you accessed it (I assume that your call to f is synchronous, so tell me if I am wrong). A Calcul_1 component will not be replaced by a Calcul_2 in the middle of a method invocation.

However, I guess this also means that if you want the reconfiguration to take effect, you have to reach the end of the method using your service. For instance, if your component launch a thread with a function like :

public void run() {
    while(!end) {
        //while this function is running, myService will not change
        int var = myService.f(a,b); 
        // ... do something with the result
        Thread.sleep(SLEEP_TIME);
    }
}

If this function keeps running, ipojo will not bind a new service to myService even if your filter is modified. You should put the call to the service in another helper method. It will allow ipojo to lock the access to the service while your component is being reconfigured. for instance :

public void run() {
    while(!end) {
        // the service may not be the same each time because this function does
        // not directly use the service
        int var = callDynamicService(a,b); 
        // ... do something with the result
        Thread.sleep(SLEEP_TIME);
    }
}

private int callDynamicService(int a, int b) {
    return myService.f(a,b);
}
archz
  • 1,073
  • 13
  • 19
  • Thank you for your reponse. But i don't understand why a component sleep, all components pause? although there are not dependencies between them. – HNT Nov 20 '14 at 12:47
  • Are you talking about the call to thread.sleep() ? It is just an example of a simplistic loop you may implement in a runnable (the problem I wanted to point out would still happen without it). The components are not "pausing"; I just mean that when method is executing in your component, ipojo ensure that `myService` references the same instance as long as your method has not returned. If you call `myService` twice in the same method you can be sure the calculation service will be the same each time (multiplication or addition, but not one of each). – archz Nov 20 '14 at 14:28
  • Thank you for your help. I agree with you that requiring component will wait to receive result before reconfiguring. I tested by using Thread.sleep() as you talked and it lead to another issus http://stackoverflow.com/questions/27039926/sleep-a-ipojo-component-all-disable. I want create independent components each other. :( – HNT Nov 20 '14 at 15:23
  • I re-tried your example but it has the same result. there are not the difference between putting the call to the service in another helper method and not. – HNT Nov 25 '14 at 08:28