Mintz
Mintz

Reputation: 969

Most efficient way to handle HTTP POST REQUEST in Java

I have a respond.php that returns a JSON data for each HTTP POST with different header data. What is the most efficient way to generate roundly 30 requests at once and handle their responded data? This needs to be very efficient since the hardware is rather limited in performance.

Upvotes: 1

Views: 1120

Answers (2)

Stephen C
Stephen C

Reputation: 718758

It is not clear what you mean by "generate roundly 30 requests at once and handle their responded data".

But the most efficient use of >>YOUR<< time (and our time) would be to implement this in a straight-forward fashion, and then see if the performance is good enough. It is pointless to spend many extra hours coding an ultra-efficient solution when the simple solution is going to be good enough.

The simple solution is to create 30 client threads and have each one send a single POST request using HttpUrlConnection. Whether that is "efficient" depends on what resources are the bottleneck. (And of course, the same applies to ideas on how to make it more "efficient".) Saying that the hardware is "rather limited in performance" doesn't really give us much of a clue ...

Once you have a simple solution, and you've identified what the actual bottleneck areas are (client-side CPU? thread stacks? network speed?) ... we could make meaningful suggestions about how to make your code faster. But by asking for "the most efficient" solution, you are just encouraging people to guess what the bottlenecks are.


One final point. When you say "most efficient" there are two ways to interpret that. You could mean, a solution that sends the requests and processes the responses in the shortest elapsed time. Alternatively, you could mean a solution that places the least resource drain (CPU and/or memory and/or network bandwidth) on the client and/or server. This is not just a quibble. The different flavours of "efficient" require markedly different solutions.

Upvotes: 3

Hiro2k
Hiro2k

Reputation: 5587

Make use of these high level apis instead, the code is much cleaner and easier to work with.

public static void main(String args[]) {
    int THREAD_COUNT = 30;
    ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
    //Add the enw jobs where we just insert
    List<Callable<Void>> jobs = new ArrayList<Callable<Void>>();
    for (int i = 0; i < THREAD_COUNT; i++) {
        jobs.add(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                URL url = new URL("http://yourserver.com/respond.php");
                URLConnection conn = url.openConnection();
                conn.setDoInput(true);                  
                // Here is where you would use a JSON parser library to parse from the conn.getInputStream method.
                // Don't forget to close the input stream.
                // And then you can handle and do stuff here.
                return null; //aren't interested in the returned value
            }
        });
    }
    executorService.invokeAll(jobs);
}

Upvotes: -1

Related Questions