简体   繁体   中英

Return API callback value synchronously

I want to make a simple interface that make all the asynchronous requests(callbacks) and return the results synchronously. I could use Future callable,but the requests I use are API requests So Future won't help. For example:

 public boolean login(String user,String pass) {
    boolean result = false;
    API_Login(user,pass,new APICallback() {
        @Override
        public void done(APIException e) {
           //logic here
             result = true;

        }
    });
    return result;
}

I thought about using promise library but then again,I would have to implement a callback outside the API for "then" or "done" events of the promise.

You can use message broker. 1. Start a request processor thread. 2. Receive API request in this thread. 3. To send request towards downsteam, use a message broker. Publish you downstream request to message broker. Start a listener on correlationId. 4a. A dispatcher component can listen to message broker & pick up your downstream request. It will actually communicate with downstream. 4b. Get the response (Async) from downstream. Publish this response to message broker 5. Your main listener would receive a response. 6. Send back the response or timeout to consumer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM