Refactoring a java code and correct use of Futures

HI,
Currently in my java code i am having the following class :
This is just an asynchronous way to make  HTTP post request with a json string.
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;

import java.util.concurrent.Future;


public class WebHookAdapter {
    private static Unirest unirest = new Unirest();
    static String webhookEndpoint;

    public static void main(String[] args) {
        Future<HttpResponse<JsonNode>> future = send("ldjflksdfj");

    }

    public static Future send(String text) {
        String json = "{\"text\" : \"" + text + "\"}";
        Future<HttpResponse<JsonNode>> future;

        future = unirest.post(webhookEndpoint).header("Content-Type", "application/json").body(json).asJsonAsync(new Callback<JsonNode>() {
            @Override
            public void completed(HttpResponse<JsonNode> httpResponse) {

            }

            @Override
            public void failed(UnirestException e) {

            }

            @Override
            public void cancelled() {

            }
        });
        return future;
    }
}

Open in new window


Here i am using unirest for java to just make an http post call.
unirest

To make the http calls non blocking i used the Future version.
Now in the main method i am using :        Future<HttpResponse<JsonNode>> future = send("ldjflksdfj");

what do i do with this future.
If i use future.get()... i read it blocks...
In the case where i will be doing multiple http post request asynchoronously.
Is this a correct approach ??

Also the new CallBack<...code can i move it out of the method. Whats the general practice.
Is this the way to use it. Can you suggest a better approach to this problem (Making multiple asycn http post request)

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
If you want to do multiple http requests, you send off the requests and collect up the Futures you get back and put them on a list.

Then you can walk that list checking for "isDone" on each of the futures - that won't block - and when they are done get the results and process them.

Or if you are sending off a set of requests as a group - take a look at invokeAll() on the executor:
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)

It basically does all of this for you in one step.  That's assuming you have access to the executor that is running the tasks.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.