Search code examples
javaipojo

Data transmission during ipojo reconfiguration


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?


Solution

  • 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);
    }