Java Source Code: com.discursive.jccook.httpclient.MultithreadedExample


   1: /* 
   2:  * ========================================================================
   3:  * 
   4:  * Copyright 2005 Tim O'Brien.
   5:  *
   6:  * Licensed under the Apache License, Version 2.0 (the "License");
   7:  * you may not use this file except in compliance with the License.
   8:  * You may obtain a copy of the License at
   9:  * 
  10:  *   http://www.apache.org/licenses/LICENSE-2.0
  11:  * 
  12:  * Unless required by applicable law or agreed to in writing, software
  13:  * distributed under the License is distributed on an "AS IS" BASIS,
  14:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15:  * See the License for the specific language governing permissions and
  16:  * limitations under the License.
  17:  * 
  18:  * ========================================================================
  19:  */
  20: package com.discursive.jccook.httpclient;
  21: 
  22: import java.io.IOException;
  23: import java.util.ArrayList;
  24: import java.util.Iterator;
  25: import java.util.List;
  26: 
  27: import org.apache.commons.httpclient.HttpClient;
  28: import org.apache.commons.httpclient.HttpConnectionManager;
  29: import org.apache.commons.httpclient.HttpException;
  30: import org.apache.commons.httpclient.HttpMethod;
  31: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
  32: import org.apache.commons.httpclient.methods.GetMethod;
  33: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
  34: 
  35:	  public class MultithreadedExample {
  36:
  37:	      public static void main(String[] args) throws HttpException, IOException, InterruptedException {
  38:        MultithreadedExample example = new MultithreadedExample();
  39:        example.start();
  40:    }
  41:    
  42:	      public void start() throws InterruptedException {
  43:        HttpConnectionManager connectionManager = 
  44:                    new MultiThreadedHttpConnectionManager();
  45:                    
  46:        HttpConnectionManagerParams params = connectionManager.getParams();
  47:        params.setIntParameter( HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 2 );
  48:        params.setIntParameter( HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 4 );
  49:
  50:        List retrievers = new ArrayList();
  51:
  52:	          for( int i = 0; i < 20; i++ ) {
  53:            HttpClient client = new HttpClient(connectionManager);
  54:            Thread thread = new Thread( new PageRetriever(client) );
  55:            retrievers.add( thread );
  56:        }
  57:        
  58:        Iterator threadIter = retrievers.iterator();
  59:	          while( threadIter.hasNext() ) {
  60:            Thread thread = (Thread) threadIter.next();
  61:            thread.start();
  62:        } 
  63:    }
  64:    
  65:	      public class PageRetriever implements Runnable {
  66:        private HttpClient client;
  67:
  68:	          public PageRetriever(HttpClient client) {
  69:            this.client = client;
  70:        }
  71:
  72:	          public void run() {
  73:            String url = "http://www.discursive.com/jccook";
  74:        
  75:            HttpMethod method = new GetMethod( url );
  76:	              try {
  77:                client.executeMethod( method );
  78:                String response = method.getResponseBodyAsString();
  79:            } catch(Exception e) {
  80:                System.out.println( "Error making request" );
  81:            }
  82:            method.releaseConnection();
  83:            method.recycle();
  84:        }
  85:    }
  86:}