public class JettyServerMain { public static void main(String[] args) throws Exception { Server server = new Server(8888); server.setHandler(new HelloHandler()); server.start(); server.join(); } } class HelloHandler extends AbstractHandler { /** * 做爲測試,在這個方法故意sleep 3秒,而後返回hello; */ @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { long threadId = Thread.currentThread().getId(); Log.getLogger(this.getClass()).info("threadId="+threadId+" come in"); try { Thread.sleep(3000); } catch(Exception e) { e.printStackTrace(); } response.setStatus(HttpServletResponse.SC_OK); PrintWriter out = response.getWriter(); out.println("hello+"+threadId); baseRequest.setHandled(true); Log.getLogger(this.getClass()).info("threadId="+threadId+" finish"); } }
public class HTTPClientV1 { public static void main(String argvs[]){ CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 建立Get請求 HttpGet httpGet = new HttpGet("http://localhost:8888"); // 響應模型 CloseableHttpResponse response = null; try { // 由客戶端執行(發送)Get請求 response = httpClient.execute(httpGet); // 從響應模型中獲取響應實體 HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { System.out.println("響應內容爲:" + EntityUtils.toString(responseEntity)); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 釋放資源 if (httpClient != null) { httpClient.close(); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
public static void main(String argvs[]){ CloseableHttpClient httpClient = HttpClientBuilder.create().build(); for(int i=0;i<10;i++) { new Thread(new Runnable() { @Override public void run() { GetRequest(httpClient); } }).start(); } }
22:48:46.618 INFO log: Logging initialized @897ms 22:48:46.655 INFO Server: jetty-9.2.14.v20151106 22:48:47.051 INFO ServerConnector: Started ServerConnector@5136ac92{HTTP/1.1}{0.0.0.0:8888} 22:48:47.052 INFO Server: Started @1346ms
22:49:59.056 INFO HelloHandler: threadId=15 come in 22:49:59.057 INFO HelloHandler: threadId=14 come in 22:50:02.080 INFO HelloHandler: threadId=14 finish 22:50:02.080 INFO HelloHandler: threadId=15 finish 22:50:02.144 INFO HelloHandler: threadId=15 come in 22:50:02.144 INFO HelloHandler: threadId=19 come in 22:50:05.144 INFO HelloHandler: threadId=19 finish 22:50:05.144 INFO HelloHandler: threadId=15 finish 22:50:05.148 INFO HelloHandler: threadId=19 come in 22:50:05.148 INFO HelloHandler: threadId=14 come in 22:50:08.149 INFO HelloHandler: threadId=19 finish 22:50:08.149 INFO HelloHandler: threadId=14 finish 22:50:08.153 INFO HelloHandler: threadId=15 come in 22:50:08.153 INFO HelloHandler: threadId=19 come in 22:50:11.153 INFO HelloHandler: threadId=19 finish 22:50:11.153 INFO HelloHandler: threadId=15 finish 22:50:11.158 INFO HelloHandler: threadId=14 come in 22:50:11.158 INFO HelloHandler: threadId=19 come in 22:50:14.158 INFO HelloHandler: threadId=19 finish 22:50:14.158 INFO HelloHandler: threadId=14 finish
// 建立Get請求 HttpGet httpGet = new HttpGet("http://localhost:8888"); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(2000) .setConnectTimeout(2000) .build(); httpGet.setConfig(requestConfig);
22:55:32.751 INFO HelloHandler: threadId=15 come in 22:55:32.751 INFO HelloHandler: threadId=14 come in 22:55:34.758 INFO HelloHandler: threadId=19 come in 22:55:34.759 INFO HelloHandler: threadId=21 come in 22:55:35.751 INFO HelloHandler: threadId=15 finish 22:55:35.751 INFO HelloHandler: threadId=14 finish 22:55:36.761 INFO HelloHandler: threadId=23 come in 22:55:36.767 INFO HelloHandler: threadId=14 come in 22:55:37.760 INFO HelloHandler: threadId=19 finish 22:55:37.761 INFO HelloHandler: threadId=21 finish 22:55:38.764 INFO HelloHandler: threadId=15 come in 22:55:38.769 INFO HelloHandler: threadId=19 come in 22:55:39.761 INFO HelloHandler: threadId=23 finish 22:55:39.767 INFO HelloHandler: threadId=14 finish 22:55:40.766 INFO HelloHandler: threadId=21 come in 22:55:40.771 INFO HelloHandler: threadId=23 come in 22:55:41.764 INFO HelloHandler: threadId=15 finish 22:55:41.770 INFO HelloHandler: threadId=19 finish 22:55:43.766 INFO HelloHandler: threadId=21 finish 22:55:43.771 INFO HelloHandler: threadId=23 finish
import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; /** * Date: 2019/5/22 * TIME: 21:25 * HTTPClient * 一、共享httpclient * 二、增長超時時間 * @author donlianli */ public class HTTPClientV3 { public static void main(String argvs[]){ // 得到Http客戶端(能夠理解爲:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不同的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); for(int i=0;i<10;i++) { new Thread(new Runnable() { @Override public void run() { GetRequest(httpClient); } }).start(); } } private static void GetRequest(CloseableHttpClient httpClient) { // 建立Get請求 HttpGet httpGet = new HttpGet("http://localhost:8888"); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(2000) .setConnectTimeout(2000) .build(); httpGet.setConfig(requestConfig); // 響應模型 CloseableHttpResponse response = null; try { // 由客戶端執行(發送)Get請求 response = httpClient.execute(httpGet); // 從響應模型中獲取響應實體 HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { System.out.println("響應內容爲:" + EntityUtils.toString(responseEntity)); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
public static void main(String argvs[]){ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // 總鏈接數 cm.setMaxTotal(200); // 這個至少要大於10 cm.setDefaultMaxPerRoute(20); CloseableHttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(cm).build(); for(int i=0;i<10;i++) { new Thread(new Runnable() { @Override public void run() { GetRequest(httpClient); } }).start(); } }