mardi 30 mai 2017

Java implementation to handle callback messages

I have the following situation I need to handle in my code:

public class Class1 {

    IRequester requester;

    public Class1(Requester impl) {
        requester = impl;
    }

    public List doSomething() {
        requester.request1();  // sends messages to a set of nodes
        //do some more local processing
        list = requester.request2(); // sends some more messages and returns a list
        return list;
    }
}

In this case request1() sends a request to a set of nodes and returns a result which will be used locally for more processing, and then the request2() is made which returns a list. This needs to be returned at the end of execution of doSomething(). request1() and request2() are done through requester which is of type IRequester

public interface IRequester {

    request1();

    List request2();
}

Now request1() and request2() are implemented by the class which actually does the requests. This is the class that handles the communication between the nodes.

public NetworkManager implements IRequester {

    request1() {

        // Create an operation
        // Add callback to the operation             
        // schedule operation
    }

    request2() {

    }
}

Now, my issue is that when I implement request1() here in there I need to create a procedure which will send a message to the node. This procedure can have a callback attached. When the node responds it returns the result. How do I implement this such that it returns the result at the end of my request1?

Aucun commentaire:

Enregistrer un commentaire