HttpAsyncClient 异步HttpCLient(二)

HttpAsyncClient 异步HttpCLient(二)

package com.abin.lee.junit.httpasyncclient.service;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpAsyncClientService extends HttpServlet{
 private static final long serialVersionUID = 807336917776643578L;

 @SuppressWarnings("rawtypes")
 public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
  Map map=request.getParameterMap();
  String id=(String)((Object[])map.get("id"))[0].toString();
  if(null!=id&&!"".equals(id)){
   String result=id+" is response";
   System.out.println("result="+result);
   ServletOutputStream out=response.getOutputStream();
   BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
   writer.write(result);
   writer.flush();
   writer.close();
  }else{
   String result=id+" is null";
   System.out.println("result="+result);
   ServletOutputStream out=response.getOutputStream();
   BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
   writer.write(result);
   writer.flush();
   writer.close();
  }
 }
}





<servlet>
  <servlet-name>HttpAsyncClientService</servlet-name>
  <servlet-class>com.abin.lee.junit.httpasyncclient.service.HttpAsyncClientService</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>HttpAsyncClientService</servlet-name>
  <url-pattern>/HttpAsyncClientService</url-pattern>
 </servlet-mapping>







package com.abin.lee.junit.httpasyncclient.example;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

public class CreateAsyncClientHttpExchangeFutureCallback {
  public static void main(String[] args) throws Exception {
         HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
         httpclient.getParams()
             .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000)
             .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000)
             .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
             .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);

         httpclient.start();
         try {
             HttpGet[] requests = new HttpGet[] {
                     new HttpGet("http://localhost:7000/global/HttpAsyncClientService?id=6"),
                     new HttpGet("http://localhost:7000/global/HttpAsyncClientService?id=8"),
                     new HttpGet("http://localhost:7000/global/HttpAsyncClientService?id=5")
             };
             final CountDownLatch latch = new CountDownLatch(requests.length);
             for (final HttpGet request: requests) {
                 httpclient.execute(request, new FutureCallback<HttpResponse>() {

                     public void completed(final HttpResponse response) {
                         latch.countDown();
                         System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
                     }

                     public void failed(final Exception ex) {
                         latch.countDown();
                         System.out.println(request.getRequestLine() + "->" + ex);
                     }

                     public void cancelled() {
                         latch.countDown();
                         System.out.println(request.getRequestLine() + " cancelled");
                     }

                 });
                 Future<HttpResponse> future = httpclient.execute(request, null);
                 HttpResponse response = future.get();
                 System.out.println("Response: " + response.getStatusLine());
                 System.out.println("Response1: " + EntityUtils.toString(response.getEntity()));
             }
             latch.await();
             System.out.println("Shutting down");
         } finally {
             httpclient.shutdown();
         }
         System.out.println("Done");
     }
  public static String AsyncHttp(){
  
   return null;
  }
}









你可能感兴趣的:(HttpAsyncClient 异步HttpCLient(二))