少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          #

          //方法一:
          package com.abin.lir.axis2.client;
          import org.apache.axiom.om.OMAbstractFactory;
          import org.apache.axiom.om.OMElement;
          import org.apache.axiom.om.OMFactory;
          import org.apache.axiom.om.OMNamespace;
          import org.apache.axis2.AxisFault;
          import org.apache.axis2.addressing.EndpointReference;
          import org.apache.axis2.client.Options;
          import org.apache.axis2.client.ServiceClient;
          public class UserClient {
          public static void main(String[] args) {
          try { 
          ServiceClient sc = new ServiceClient(); 
          Options opts = new Options(); 
          opts.setTo(new EndpointReference("http://localhost:9090/universal/services/play")); 
          opts.setAction("urn:echo"); 
          opts.setTimeOutInMilliSeconds(10000);
          sc.setOptions(opts); 
          OMElement res = sc.sendReceive(createPayLoad()); 
          System.out.println(res); 
          } catch (AxisFault e) { 
          e.printStackTrace(); 
          }
          public static OMElement createPayLoad(){ 
          OMFactory fac = OMAbstractFactory.getOMFactory(); 
          OMNamespace omNs = fac.createOMNamespace("http://localhost:9090/universal/services/play", "nsl"); 
          OMElement method = fac.createOMElement("getPassengerInfos",omNs); 
          OMElement value = fac.createOMElement("userID",omNs); 
          value.setText("1024"); 
          method.addChild(value); 
          return method; 
          }





          //方法二
          package com.abin.lir.axis2.client;
          import javax.xml.namespace.QName;
          import org.apache.axis2.AxisFault;
          import org.apache.axis2.addressing.EndpointReference;
          import org.apache.axis2.client.Options;
          import org.apache.axis2.rpc.client.RPCServiceClient;
          public class RPCClient {
          public static void main(String[] args) throws AxisFault {
           // 使用RPC方式調(diào)用WebService         
                  RPCServiceClient serviceClient = new RPCServiceClient(); 
                  Options options = serviceClient.getOptions(); 
                  // 指定調(diào)用WebService的URL 
                  EndpointReference targetEPR = new EndpointReference( 
                          "http://localhost:9090/universal/services/play"); 
                  options.setTo(targetEPR); 
                  // 指定方法的參數(shù)值 
                  Object[] requestParam = new Object[] {"1024"}; 
                  // 指定方法返回值的數(shù)據(jù)類型的Class對象 
                  Class[] responseParam = new Class[] {String.class}; 
                  // 指定要調(diào)用的getGreeting方法及WSDL文件的命名空間 
                  QName requestMethod = new QName("http://localhost:9090/universal/services/play", "getPassengerInfos"); 
                  // 調(diào)用方法并輸出該方法的返回值 
                  try {
          System.out.println(serviceClient.invokeBlocking(requestMethod, requestParam, responseParam)[0]);
          } catch (AxisFault e) {
          e.printStackTrace();
          }
          }




          //方法三
          package com.abin.lir.axis2.client;
          import org.apache.axiom.om.OMAbstractFactory;
          import org.apache.axiom.om.OMElement;
          import org.apache.axiom.om.OMFactory;
          import org.apache.axiom.om.OMNamespace;
          import org.apache.axis2.Constants;
          import org.apache.axis2.addressing.EndpointReference;
          import org.apache.axis2.client.Options;
          import org.apache.axis2.client.ServiceClient;
          public class AXIOMClient {
          private static EndpointReference targetEPR = new EndpointReference(
          "http://localhost:9090/universal/services/play");
          public static OMElement getPassengerInfos(String symbol) {
          OMFactory fac = OMAbstractFactory.getOMFactory();
          OMNamespace omNs = fac.createOMNamespace(
          "http://localhost:9090/universal/services/play",
          "tns");
          OMElement method = fac.createOMElement("getPassengerInfos", omNs);
          OMElement value = fac.createOMElement("userID", omNs);
          value.addChild(fac.createOMText(value, symbol));
          method.addChild(value);
          return method;
          }
          public static void main(String[] args) {
          try {
          OMElement getPassenger = getPassengerInfos("1024");
          Options options = new Options();
          options.setTo(targetEPR);
          options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
          ServiceClient sender = new ServiceClient();
          sender.setOptions(options);
          OMElement result = sender.sendReceive(getPassenger);
          String response = result.getFirstElement().getText();
          System.err.println("Current passengers: " + response);
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          }
          posted @ 2013-01-20 23:26 abin 閱讀(9315) | 評論 (0)編輯 收藏

          http://blog.csdn.net/leo821031/article/details/1545974
          http://zhangjunhd.blog.51cto.com/113473/25592/
          http://hi.baidu.com/lennydou/item/b57032c0ceb6f5b00c0a7b08
          http://www.cnblogs.com/markxue/archive/2012/09/01/2667123.html
          http://www.aygfsteel.com/zhaozhenlin1224/archive/2010/02/03/311783.html
          http://blog.csdn.net/yhhah/article/details/4158487






          posted @ 2013-01-20 23:23 abin 閱讀(518) | 評論 (0)編輯 收藏

          Axis2生成客戶端方式

          基于StockQuoteService類創(chuàng)建客戶端的四種方式

          構(gòu)建基于AXIOM的客戶端;

          使用Axis2 Databinding Frame work(ADB)生成客戶端;

          使用XMLBeans生成客戶端;

          使用JiBX生成客戶端。

          ADB:最簡單的生成Axis客戶端的方法。大部分情況下,這些主要的類都會以內(nèi)部類的形式創(chuàng)建在stub類中。It is not meant to be a full schema bindingapplication, and has difficulty with structures such as XML Schema element extensions and restrictions。 be meant to:有意要、打算
              XMLBeans:與ADB不同,他是一個全功能的schema編譯器。他沒有ADB的限制。然而,他也比ADB用起來更復雜。他會產(chǎn)成大量的文件,編程模型不如ADB直觀。
              JiBX:他是一個數(shù)據(jù)綁定框架。他不僅提供了WSDL-JAVA的轉(zhuǎn)換,而且提供了JAVA-XML的轉(zhuǎn)換。JiBX相當靈活,允許你選擇類來代表你的實體,但是這個卻不好做,但還句話說,如果這些都能建好,那么使用JiBX就更使用ADB一樣容易。

          對于簡單應用來說ADB已經(jīng)夠用了,如果想用更加強大更加靈活的功能,那么你可能需要使用其他兩種方式。

          Axis2提供的四種調(diào)用模式

          Web services可以用來為用戶提供廣泛的功能,從簡單的、少時間消耗的功能到多時間消耗的業(yè)務服務。當我們使用(調(diào)用客戶端的應用程序)這些Web Service時,我們不能用簡單的調(diào)用機制來針對那些對時間消耗有很大要求的服務操作。例如,如果我們使用一個簡單的傳輸通道(如HTTP)并使用IN-OUT模式來調(diào)用一個需要很長時間來完成的Web Service,那么多數(shù)情況下,我們得到的結(jié)果將是"connection time outs"。另一方面,如果我們從一個簡單的客戶端應用程序調(diào)用一個同步的服務,使用"blocking"的客戶端API將會降低客戶端應用程序的性能。現(xiàn)在來分析一下一些常用的服務調(diào)用形式。

          許多Web Service引擎提供給客戶Blocking和Non-Blocking的客戶端APIs。

          1)Blocking API-一旦服務被啟用,客戶端的應用程序?qū)⒈粧炱穑钡給peration被執(zhí)行完畢(表現(xiàn)為收到一個response或fault),才能重新獲得控制權(quán)。這是調(diào)用Web Service最簡單的方式,并且這種方式適用于多數(shù)業(yè)務情形。

          2)Non-Blocking API-這是一個回叫或輪詢機制的API。因此,一旦服務被起用,客戶端應用程序馬上得到控制權(quán),通過使用一個callback對象來獲得response。這種方式使得客戶端應用程序可以很方便的同步啟用多個Web Service。

          這兩種機制都是工作在API層面上的。稱將通過使用Non-Blocking API而產(chǎn)生的異步行為方式為API Level 異步。這兩種機制都使用單一的傳輸連接來發(fā)送request和接收response。它們的性能遠遠落后于使用兩個傳輸連接來發(fā)送request和接收response(不管是單工還是雙工)。所以這兩種機制都不能解決需要長時間處理的事務的傳輸問題(在operation處理完成之前,很有可能你的連接已經(jīng)超時了)。一種可能的解決方法是使用兩個獨立的傳輸連接來發(fā)送和接收request&response。這種異步行為,我們稱為Transport Level 異步。

          通過組合API Level異步和Transport Level 異步,我們可以得到四種調(diào)用模式。如下所示。

          API (Blocking/Non-Blocking)

          Dual Transports (Yes/No)

          Description

          Blocking

          No

          最簡單和常用的調(diào)用模式

          Non-Blocking

          No

          使用回叫或輪詢機制

          Blocking

          Yes

          在單工模式下,service operation為IN-OUT時,很有用。(如SMTP)

          Non-Blocking

          Yes

          此模式下的異步效果最大

          Axis2提供了所有上述4種調(diào)用Web Service的實現(xiàn)方式。

          posted @ 2013-01-20 20:13 abin 閱讀(2000) | 評論 (0)編輯 收藏

          http://www.aygfsteel.com/alex0927/archive/2008/06/20/209474.html
          http://blog.sina.com.cn/s/blog_4fb27fd80100ohyr.html
          posted @ 2013-01-18 23:47 abin 閱讀(405) | 評論 (0)編輯 收藏

          node.selectNodes("http://xml");
          node.selectNodes("/xml");
          node.selectNodes("xml");
          這三個寫法有什么區(qū)別

          問題補充:

          不是很明白
          就舉個例子吧
          <root>
          <xml>1</xml>
          <node>
          <xml>2</xml>
          <AAA>
          <xml>3</xml>
          </AAA>
          </node>
          </root>
          從node搜索的話,這三種方法分別能搜到1、2、3中的哪幾個?




          nodename  選取此節(jié)點的所有子節(jié)點
          / 從根節(jié)點選取
          // 從匹配選擇的當前節(jié)點選擇文檔中的節(jié)點,而不考慮它們的位置

          參考資料:http://www.w3school.com.cn/xpath/xpath_syntax.asp

          posted @ 2013-01-18 20:34 abin 閱讀(2492) | 評論 (0)編輯 收藏

           Java所有的類都具有線程的潛力,Java賦予的每個對象一個鎖,在計算機內(nèi)部工作在同一時間,只有一個對象可以持有鎖,也就是說程序在同一時間只有一個程序可以運行,這里我把對象比作是一個小的程序。而多處理器,那么就另當別論了。

                在這里我們首先學習一下公共方法wait,notify,notifyAll。

                wait方法可以使在當前線程的對象等待,直到別的線程調(diào)用此對象的notify或notifyAll方法(注意:調(diào)用的是此對象的notify和notifyAll),并且當前運行的線程必須具有此對象的對象監(jiān)視器

          package com.abin.lee.thread.thread;

          public class CarryTask extends Thread {
           public void run() {
                  try {
                      synchronized (this) {
                          Thread t = Thread.currentThread();
                          System.out.println(t.getId() + t.getName() + ":task start, wait for notify...");
                          this.wait();
                          System.out.println(t.getId() + t.getName() + ":task continue...");
                      }
                  } catch (InterruptedException ex) {
                     System.out.println(CarryTask.class.getName());
                  }
              }


          }





          package com.abin.lee.thread.thread;

          public class CarryWait {
           public static void main(String[] args) throws InterruptedException {
            CarryTask task = new CarryTask();
            Thread t = Thread.currentThread();
            System.out.println(t.getId() + t.getName() + ":task start...");
            task.start();
            Thread.sleep(2000);
            synchronized (task) {
             System.out.println("id="+Thread.currentThread().getId()+",Name="+Thread.currentThread().getName()+",task="+task+",notify");
             task.notify();
            }
           }

          }





          http://www.iteye.com/topic/1124814
          posted @ 2013-01-16 23:46 abin 閱讀(406) | 評論 (0)編輯 收藏

           今天試著把SpringMVC與fastjson整合了下,經(jīng)測試也能解決json含中文亂碼的問題,特此分享之。我也是初用,詳細文檔請見官網(wǎng)
          public class MappingFastJsonHttpMessageConverter extends 
                  AbstractHttpMessageConverter<Object> { 
              public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
           
              private SerializerFeature[] serializerFeature; 
           
              public SerializerFeature[] getSerializerFeature() { 
                  return serializerFeature; 
              } 
           
              public void setSerializerFeature(SerializerFeature[] serializerFeature) { 
                  this.serializerFeature = serializerFeature; 
              } 
           
              public MappingFastJsonHttpMessageConverter() { 
                  super(new MediaType("application", "json", DEFAULT_CHARSET)); 
              } 
           
              @Override 
              public boolean canRead(Class<?> clazz, MediaType mediaType) { 
                  return true; 
              } 
           
              @Override 
              public boolean canWrite(Class<?> clazz, MediaType mediaType) { 
                  return true; 
              } 
           
              @Override 
              protected boolean supports(Class<?> clazz) { 
                  throw new UnsupportedOperationException(); 
              } 
           
              @Override 
              protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) 
              throws IOException, HttpMessageNotReadableException { 
                  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                  int i; 
                  while ((i = inputMessage.getBody().read()) != -1) { 
                      baos.write(i); 
                  } 
                  return JSON.parseArray(baos.toString(), clazz); 
              } 
           
              @Override 
              protected void writeInternal(Object o, HttpOutputMessage outputMessage) 
              throws IOException, HttpMessageNotWritableException { 
                  String jsonString = JSON.toJSONString(o, serializerFeature); 
                  OutputStream out = outputMessage.getBody(); 
                  out.write(jsonString.getBytes(DEFAULT_CHARSET)); 
                  out.flush(); 
              } 




          SpringMVC關鍵配置:
          <mvc:annotation-driven> 
              <mvc:message-converters register-defaults="true">        
                  <!-- fastjosn spring support --> 
                  <bean id="jsonConverter" class="com.alibaba.fastjson.spring.support.MappingFastJsonHttpMessageConverter"> 
                      <property name="supportedMediaTypes" value="application/json" /> 
                      <property name="serializerFeature"> 
                          <list> 
                              <value>WriteMapNullValue</value> 
                              <value>QuoteFieldNames</value> 
                          </list> 
                      </property> 
                  </bean> 
              </mvc:message-converters> 
          </mvc:annotation-driven> 


          http://xyly624.blog.51cto.com/842520/896704
          posted @ 2013-01-12 23:56 abin 閱讀(5306) | 評論 (1)編輯 收藏

          //實例一,這里面用到了信號量Semaphore和FutureTask

          package net.abin.lee.mythread.callable;

          import java.util.concurrent.Callable;
          import java.util.concurrent.Semaphore;
          import java.util.concurrent.locks.Lock;
          import java.util.concurrent.locks.ReentrantReadWriteLock;

          public class FutureGo implements Callable<String> {
           private String message;
           private static final Semaphore semaphore=new Semaphore(3);
           private final ReentrantReadWriteLock rwl=new ReentrantReadWriteLock();
           public FutureGo(String message) {
            this.message = message;
           }

           public String call() throws InterruptedException {
            semaphore.acquire();
            Lock read=rwl.readLock();
            Lock write=rwl.readLock();
            read.lock();  
            System.out.println("message"+message+",Name"+Thread.currentThread().getName()+"進來了");
            read.unlock();
            write.lock();
            String result=message+"你好!";
            Thread.sleep(1000);
            System.out.println("message"+message+"Name"+Thread.currentThread().getName()+"離開了");
            write.unlock();
            semaphore.release();
            return result;
           }
          }



          //FutureTaskTest.java

          package net.abin.lee.mythread.callable;

          import java.util.concurrent.Callable;
          import java.util.concurrent.ExecutionException;
          import java.util.concurrent.ExecutorService;
          import java.util.concurrent.Executors;
          import java.util.concurrent.FutureTask;

          public class FutureTaskTest {
           public static void main(String[] args) throws InterruptedException,
             ExecutionException {
            Callable<String> go = new FutureGo("abin");
            FutureTask<String> task = new FutureTask<String>(go);
            ExecutorService executor = Executors.newCachedThreadPool();
            if (!executor.isShutdown()) {
             executor.execute(task);
            }
            String result = "";
            if (!task.isDone()) {
             result = (String) task.get();
             System.out.println("result=" + result);
            }
             executor.shutdown();
           }

          }






          //實例一,這里面用到了信號量Semaphore和FutureTask

          posted @ 2013-01-10 14:17 abin 閱讀(675) | 評論 (0)編輯 收藏

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

          import java.util.List;
          import java.util.concurrent.ExecutorService;
          import java.util.concurrent.Executors;
           
           import org.apache.http.HttpEntity;
          import org.apache.http.HttpResponse;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.methods.HttpGet;
          import org.apache.http.conn.ClientConnectionManager;
          import org.apache.http.conn.params.ConnManagerParams;
          import org.apache.http.conn.scheme.PlainSocketFactory;
          import org.apache.http.conn.scheme.Scheme;
          import org.apache.http.conn.scheme.SchemeRegistry;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.impl.conn.PoolingClientConnectionManager;
          import org.apache.http.params.BasicHttpParams;
          import org.apache.http.params.HttpConnectionParams;
          import org.apache.http.params.HttpParams;
          import org.apache.http.protocol.BasicHttpContext;
          import org.apache.http.protocol.HttpContext;
          import org.apache.http.util.EntityUtils;
          import org.junit.Test;
           
           public class ThreadPoolHttpClient {
               // 線程池
               private ExecutorService exe = null;
               // 線程池的容量
               private static final int POOL_SIZE = 20;
               private HttpClient client = null;
               String[] urls=null;
               public ThreadPoolHttpClient(String[] urls){
                   this.urls=urls;
               }
               @Test
               public void test() throws Exception {
                   exe = Executors.newFixedThreadPool(POOL_SIZE);
                   HttpParams params =new BasicHttpParams();
                   /* 從連接池中取連接的超時時間 */
                   ConnManagerParams.setTimeout(params, 1000);
                  /* 連接超時 */
                   HttpConnectionParams.setConnectionTimeout(params, 2000);
                   /* 請求超時 */
                   HttpConnectionParams.setSoTimeout(params, 4000);
                   SchemeRegistry schemeRegistry = new SchemeRegistry();
                  schemeRegistry.register(
                           new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
           
                   //ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
                   PoolingClientConnectionManager cm=new PoolingClientConnectionManager(schemeRegistry);
                   cm.setMaxTotal(10);
                   final HttpClient httpClient = new DefaultHttpClient(cm,params);
           
                   // URIs to perform GETs on
                   final String[] urisToGet = urls;
                   /* 有多少url創(chuàng)建多少線程,url多時機子撐不住
          56         // create a thread for each URI
          57         GetThread[] threads = new GetThread[urisToGet.length];
          58         for (int i = 0; i < threads.length; i++) {
          59             HttpGet httpget = new HttpGet(urisToGet[i]);
          60             threads[i] = new GetThread(httpClient, httpget);           
          61         }
          62         // start the threads
          63         for (int j = 0; j < threads.length; j++) {
          64             threads[j].start();
          65         }
          66
          67         // join the threads,等待所有請求完成
          68         for (int j = 0; j < threads.length; j++) {
          69             threads[j].join();
          70         }
          71         使用線程池*/
                   for (int i = 0; i < urisToGet.length; i++) {
                       final int j=i;
                       System.out.println(j);
                       HttpGet httpget = new HttpGet(urisToGet[i]);
                       exe.execute( new GetThread(httpClient, httpget));
                   }
                  
                  
                   //創(chuàng)建線程池,每次調(diào)用POOL_SIZE
                   /*
                   for (int i = 0; i < urisToGet.length; i++) {
                       final int j=i;
                       System.out.println(j);
                       exe.execute(new Thread() {
                           @Override
                           public void run() {
                               this.setName("threadsPoolClient"+j);
                              
                                   try {
                                       this.sleep(100);
                                       System.out.println(j);
                                   } catch (InterruptedException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                                   }
                                  
                                   HttpGet httpget = new HttpGet(urisToGet[j]);
                                   new GetThread(httpClient, httpget).get();
                               }
                              
                              
                          
                       });
                   }
                  
                   */
                   //exe.shutdown();
                   System.out.println("Done");
               }
               static class GetThread extends Thread{
                  
                   private final HttpClient httpClient;
                   private final HttpContext context;
                   private final HttpGet httpget;
                  
                   public GetThread(HttpClient httpClient, HttpGet httpget) {
                       this.httpClient = httpClient;
                       this.context = new BasicHttpContext();
                       this.httpget = httpget;
                   }
                   @Override
                   public void run(){
                       this.setName("threadsPoolClient");
                       try {
                           Thread.sleep(5000);
                       } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }
                       get();
                   }
                  
                   public void get() {
                       try {
                           HttpResponse response = this.httpClient.execute(this.httpget, this.context);
                           HttpEntity entity = response.getEntity();
                           if (entity != null) {
                               System.out.println(this.httpget.getURI()+": status"+response.getStatusLine().toString());
                           }
                           // ensure the connection gets released to the manager
                           EntityUtils.consume(entity);
                       } catch (Exception ex) {
                           this.httpget.abort();
                       }finally{
                           httpget.releaseConnection();
                       }
                   }
               }
           }

          posted @ 2013-01-08 23:51 abin 閱讀(3021) | 評論 (0)編輯 收藏

          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;
            }
          }









          posted @ 2013-01-08 23:16 abin 閱讀(3190) | 評論 (0)編輯 收藏

          僅列出標題
          共50頁: First 上一頁 16 17 18 19 20 21 22 23 24 下一頁 Last 
          主站蜘蛛池模板: 长阳| 浦县| 宁武县| 海林市| 永丰县| 寻甸| 和田县| 北碚区| 乃东县| 上犹县| 塘沽区| 海伦市| 肇源县| 柳河县| 铜梁县| 西乡县| 祥云县| 沙湾县| 托克逊县| 阿尔山市| 涞水县| 南京市| 泾阳县| 丰城市| 精河县| 合江县| 法库县| 恭城| 祁东县| 右玉县| 百色市| 兴隆县| 武城县| 岐山县| 自治县| 乐亭县| 乐安县| 双桥区| 全州县| 大渡口区| 息烽县|