kailash gaur
kailash gaur

Reputation: 1447

Hit a url n times parallely

I want to hit a url n times parallelly. I googled for it and found this url. I tried to change according to my requirement(hit a url n times and see the response of each one),but not succeed.I added the following code for exacuting a request and getting the response in Task class constructor in above link,but this only work searially.

DefaultHttpClient client = new DefaultHttpClient();
  HttpPost method = new HttpPost(aURL);
  try {
      HttpResponse httpResponse=client.execute(method);
      StringBuffer buffer = new StringBuffer();
      BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
      String dataLine = null;
      while((dataLine = reader.readLine()) != null){
            buffer.append(dataLine);
      }
      String responseMsg =    buffer.toString();
      System.out.println(responseMsg);     

any help is appriciated.

Upvotes: 1

Views: 969

Answers (2)

Drona
Drona

Reputation: 7234

You can employ a threadPool with number of threads equal to the number of parallel requests you want to send. Wrap your logic (creating and sending the request) into a runnable Task and create and submit instances of the tasks using a loop.

int numOfThreads = ... <num of parallel requests to generate>
ExecutorService es = Executors.newFixedThreadPool(numOfThreads);
for (int i = 0; i < numOfThreads; i++) {
    es.submit(new Task());
}

Your task could be somthing like this. You can do whatever you have to within the run() implementation. Also, you can improvise on setting off the threads at once using synchronizers.

class Task implements Runnable{
    @Override
    public void run() {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost method = new HttpPost(aURL);
        try {
            HttpResponse httpResponse=client.execute(method);
            StringBuffer buffer = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String dataLine = null;
            while((dataLine = reader.readLine()) != null){
                buffer.append(dataLine);
            }
            String responseMsg =    buffer.toString();
            System.out.println(responseMsg);
        }
    }
}

Upvotes: 2

Ankit
Ankit

Reputation: 6622

One way is that, create all such request in thread, create n threads, and start() them serially. though the start() is serial, but as network operations will be performed parallaly (to some extent). though exact parallel is not possible this way.

Another way is to create and schedule these n request-task to particular time, though timer-task (for that, get current time, and give some offset to be able to instantiate n thread, and schedule all of those for the time fetched initially)

something like this code:

    public void createNparralelRequest(int n){
        long time = new Date().gettime();
        for(i=0;i<n;i++){
            Timer timer = new Timer();
            TimerTask task = new TimerTask(){
                public void run(){
                    //call to web request method
                }
            timer.schedule(task, time);
        }
    }
}

Upvotes: 1

Related Questions