title: Java implementation to handle callback messages
body: <p>I have the following situation I need to handle in my code:</p>
  
  <pre><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;
      }
  }
  </code></pre>
  
  <p>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</p>
  
  <pre><code>public interface IRequester {
  
      request1();
  
      List request2();
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>public NetworkManager implements IRequester {
  
      request1() {
  
          // Create an operation
          // Add callback to the operation             
          // schedule operation
      }
  
      request2() {
  
      }
  }
  </code></pre>
  
  <p>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?</p>