ITPub博客

首页 > Linux操作系统 > Linux操作系统 > SOAP客户端

SOAP客户端

原创 Linux操作系统 作者:nicaipu 时间:2013-04-10 11:26:29 0 删除 编辑

1.主要是为了加强对SOAP远程方法调用的学习,特意自己写了一个SOAP Client端.
2.要求环境:JDK1.6 以上,Axis2 以上
3.主要代码

 

  1. package com.googlecode.soap.client;   
  2.    
  3. import java.io.ByteArrayOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.io.OutputStream;   
  7. import java.io.PrintStream;   
  8. import java.net.Socket;   
  9. import java.net.URL;   
  10.    
  11. import com.googlecode.soap.client.SoapResponse.Status;   
  12. import com.googlecode.soap.util.ExecuteThreadPool;   
  13.    
  14. public class SoapServiceClient {   
  15.    
  16.     private String address;   
  17.    
  18.     private String namespace;   
  19.    
  20.         
  21.     public static long MAX_TIME_OUT = 10000; // 10秒    
  22.    
  23.     private SoapResponse response;   
  24.    
  25.     static ExecuteThreadPool executor = new ExecuteThreadPool(5);   
  26.         
  27.     public final Object lock = new Object();   
  28.    
  29.      URL url = null;   
  30.         
  31.      Socket socket = null;   
  32.            
  33.     private void init(SoapRequest request){   
  34.         this.response = null;    
  35.         request.context = this;   
  36.     }   
  37.        
  38.     public SoapServiceClient(String address) throws Exception, IOException {   
  39.         this.address = address;   
  40.         if (!this.address.endsWith("?wsdl")) {   
  41.             thisthis.address = this.address + "?wsdl";   
  42.         }   
  43.         this.url = new URL(this.address);   
  44.         socket = new Socket(url.getHost(), url.getPort());   
  45.     }   
  46.    
  47.     public String getNamespace() {   
  48.         return namespace;   
  49.     }   
  50.    
  51.     public void setNamespace(String namespace) {   
  52.         this.namespace = namespace;   
  53.     }   
  54.    
  55.     public void sendRequest(final SoapRequest request) {   
  56.            
  57.         this.init(request);   
  58.         // 多线程环境下发送   
  59.         executor.execute(new Runnable() {   
  60.    
  61.             public void run()  {   
  62.                    
  63.                 OutputStream os = null;   
  64.                 InputStream input = null;   
  65.                 PrintStream ps = null;   
  66.                 ByteArrayOutputStream bos = null;   
  67.                 try {   
  68.                     String send = request.toString();   
  69.                     if( socket.isClosed()) {   
  70.                       socket = new Socket(url.getHost(), url.getPort());   
  71.                     }   
  72.                     socket.setSoTimeout((int)MAX_TIME_OUT);   
  73.                     os = socket.getOutputStream();   
  74.                     ps = new PrintStream(os, true, request.getCode());   
  75.                     ps.println(send);   
  76.                     input = socket.getInputStream();   
  77.                     int available = input.available();   
  78.                     if (available <= 0) {   
  79.                              long current= System.currentTimeMillis();    
  80.                              for(;;){    
  81.                                  input = socket.getInputStream();   
  82.                                  available = input.available();   
  83.                                  if(available  > 0 ) {   
  84.                                      break;    
  85.                                  }   
  86.                                  if(System.currentTimeMillis()-current >= MAX_TIME_OUT ) {    
  87.                                      synchronized(lock) {   
  88.                                           response = new SoapResponse("TimeOut Exception", Status.TIMEOUT,null);   
  89.                                           
  90.                                           return ;   
  91.                                      }   
  92.                                 }   
  93.                              }   
  94.                      }   
  95.                     bos = new ByteArrayOutputStream();   
  96.                     byte[] buffer = new byte[256];   
  97.                     int totalRead = 0;   
  98.                     while (true) {   
  99.                         int nRead = input.read(buffer);   
  100.                         totalRead += nRead;   
  101.                         bos.write(buffer, 0, nRead);   
  102.                         if (totalRead >= available) {   
  103.                             break;   
  104.                         }   
  105.                     }   
  106.                     String responseString = bos.toString(request.getCode());   
  107.                     System.out.println("response=\r\n" + responseString);   
  108.                     synchronized(lock) {   
  109.                        response = new SoapResponse(responseString,request.getInvocation().getReturnType());   
  110.                          
  111.                     }   
  112.                 } catch (Exception e) {   
  113.                     e.printStackTrace();   
  114.                     synchronized(lock) {   
  115.                       response = new SoapResponse("RequestError", Status.ServerError,null);   
  116.                        
  117.                     }   
  118.                 }  finally {   
  119.                     try {   
  120.                         if (ps != null) {   
  121.                             ps.close();   
  122.                         }   
  123.                         if (os != null) {   
  124.                             os.close();   
  125.                         }   
  126.                         if (input != null) {   
  127.                             input.close();   
  128.                         }   
  129.                         if (bos != null) {   
  130.                             bos.close();   
  131.                         }   
  132.                         if (socket != null) {   
  133.                             socket.close();   
  134.                         }   
  135.    
  136.                     } catch (Exception e) {   
  137.                         e.printStackTrace();   
  138.                     } finally {   
  139.                         ps = null; os =null; input = null; bos =null;    
  140.                     }   
  141.                 }   
  142.             }   
  143.         });   
  144.    
  145.     }   
  146.    
  147.     public SoapResponse getResponse() {   
  148.     if (this.response == null) {   
  149.             long current = System.currentTimeMillis();   
  150.             for (;;) {   
  151.                 if (System.currentTimeMillis() - current >= MAX_TIME_OUT) {   
  152.                     synchronized (lock) {   
  153.                         return this.response;   
  154.                     }   
  155.                 }   
  156.                 if (this.response != null) {   
  157.                     synchronized (lock) {   
  158.                         return this.response;   
  159.                     }   
  160.                 }   
  161.             }   
  162.         }   
  163.         synchronized (lock) {   
  164.             return this.response;   
  165.         }   
  166.            
  167.    
  168.     }   
  169.    
  170.        
  171.    
  172. }   
  173.  
  174. ackage com.googlecode.soap.util;   
  175.    
  176. import java.util.HashSet;   
  177. import java.util.Iterator;   
  178. import java.util.concurrent.BlockingQueue;   
  179. import java.util.concurrent.Executor;   
  180. import java.util.concurrent.LinkedBlockingQueue;   
  181. import java.util.concurrent.TimeUnit;   
  182. import java.util.concurrent.locks.ReentrantLock;   
  183.    
  184. public class ExecuteThreadPool implements Executor {   
  185.    
  186.     private final int maxPoolSize ;   
  187.        
  188.     private long idelTime = 0 ;   
  189.        
  190.     private int realSize = 0 ;   
  191.        
  192.     //线程池是否关

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28794270/viewspace-758142/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2013-04-10

  • 博文量
    1
  • 访问量
    1019