1.主要是为了加强对SOAP远程方法调用的学习,特意自己写了一个SOAP Client端.
2.要求环境:JDK1.6 以上,Axis2 以上
3.主要代码
- package com.googlecode.soap.client;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.net.Socket;
- import java.net.URL;
- import com.googlecode.soap.client.SoapResponse.Status;
- import com.googlecode.soap.util.ExecuteThreadPool;
- public class SoapServiceClient {
- private String address;
- private String namespace;
- public static long MAX_TIME_OUT = 10000; // 10秒
- private SoapResponse response;
- static ExecuteThreadPool executor = new ExecuteThreadPool(5);
- public final Object lock = new Object();
- URL url = null;
- Socket socket = null;
- private void init(SoapRequest request){
- this.response = null;
- request.context = this;
- }
- public SoapServiceClient(String address) throws Exception, IOException {
- this.address = address;
- if (!this.address.endsWith("?wsdl")) {
- thisthis.address = this.address + "?wsdl";
- }
- this.url = new URL(this.address);
- socket = new Socket(url.getHost(), url.getPort());
- }
- public String getNamespace() {
- return namespace;
- }
- public void setNamespace(String namespace) {
- this.namespace = namespace;
- }
- public void sendRequest(final SoapRequest request) {
- this.init(request);
- // 多线程环境下发送
- executor.execute(new Runnable() {
- public void run() {
- OutputStream os = null;
- InputStream input = null;
- PrintStream ps = null;
- ByteArrayOutputStream bos = null;
- try {
- String send = request.toString();
- if( socket.isClosed()) {
- socket = new Socket(url.getHost(), url.getPort());
- }
- socket.setSoTimeout((int)MAX_TIME_OUT);
- os = socket.getOutputStream();
- ps = new PrintStream(os, true, request.getCode());
- ps.println(send);
- input = socket.getInputStream();
- int available = input.available();
- if (available <= 0) {
- long current= System.currentTimeMillis();
- for(;;){
- input = socket.getInputStream();
- available = input.available();
- if(available > 0 ) {
- break;
- }
- if(System.currentTimeMillis()-current >= MAX_TIME_OUT ) {
- synchronized(lock) {
- response = new SoapResponse("TimeOut Exception", Status.TIMEOUT,null);
- return ;
- }
- }
- }
- }
- bos = new ByteArrayOutputStream();
- byte[] buffer = new byte[256];
- int totalRead = 0;
- while (true) {
- int nRead = input.read(buffer);
- totalRead += nRead;
- bos.write(buffer, 0, nRead);
- if (totalRead >= available) {
- break;
- }
- }
- String responseString = bos.toString(request.getCode());
- System.out.println("response=\r\n" + responseString);
- synchronized(lock) {
- response = new SoapResponse(responseString,request.getInvocation().getReturnType());
- }
- } catch (Exception e) {
- e.printStackTrace();
- synchronized(lock) {
- response = new SoapResponse("RequestError", Status.ServerError,null);
- }
- } finally {
- try {
- if (ps != null) {
- ps.close();
- }
- if (os != null) {
- os.close();
- }
- if (input != null) {
- input.close();
- }
- if (bos != null) {
- bos.close();
- }
- if (socket != null) {
- socket.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- ps = null; os =null; input = null; bos =null;
- }
- }
- }
- });
- }
- public SoapResponse getResponse() {
- if (this.response == null) {
- long current = System.currentTimeMillis();
- for (;;) {
- if (System.currentTimeMillis() - current >= MAX_TIME_OUT) {
- synchronized (lock) {
- return this.response;
- }
- }
- if (this.response != null) {
- synchronized (lock) {
- return this.response;
- }
- }
- }
- }
- synchronized (lock) {
- return this.response;
- }
- }
- }
- ackage com.googlecode.soap.util;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.Executor;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.locks.ReentrantLock;
- public class ExecuteThreadPool implements Executor {
- private final int maxPoolSize ;
- private long idelTime = 0 ;
- private int realSize = 0 ;
- //线程池是否关
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28794270/viewspace-758142/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28794270/viewspace-758142/