[置顶] WebService调用工具(AXIS2)
package com.metarnet.util; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.Arrays; import java.util.List; 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; /** * WebService调用工具 * 用法: * 1:(推荐)WebServiceUtil.create(发布地址,命名空间,接口类); * 创建的对象可以直接当成本地接口调用,例如 * WebService service = WebServiceUtil.create("http://localhost:8080/test/cxf/webservice", * "http://www.yanfan.com/",WebService.class); * List<CeicsNeinfo> list = service.selectNeList(); * 2:创建Util方式 WebServiceUtil util = new WebServiceUtil( "http://127.0.0.1:8090/metarnetos_v3/cxf/webservice", "http://www.metarnet.com/"); CeicsNeinfo[] tasks = util.callback("selectNeList",CeicsNeinfo[].class); * * @author YANFAN */ public class WebServiceUtil implements InvocationHandler{ private RPCServiceClient serviceClient; private String nameSpace;//命名空间 private int timeout = 30;//超时时间,单位:秒 public WebServiceUtil(String endpoint,String nameSpace) throws AxisFault{ serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference(endpoint); options.setTo(targetEPR); options.setTimeOutInMilliSeconds(timeout*1000); this.nameSpace = nameSpace; } /** * 调用 method 方法 * 有返回值,返回值类型 clazz */ public <T> T callback(String method,Class<T> clazz) throws AxisFault { return callback(method,new Object[]{},clazz); } /** * 调用 method 方法,传递一个参数param * 有返回值,返回值类型 clazz */ public <T> T callback(String method,Object param,Class<T> clazz) throws AxisFault { return callback(method,new Object[]{param}, clazz); } /** * 调用 method 方法,传递多个参数param * 有返回值,返回值类型 clazz */ @SuppressWarnings("unchecked") public <T> T callback(String method,Object[] param,Class<T> clazz) throws AxisFault { QName opName = new QName(nameSpace,method); Class<?>[] returnTypes = new Class[]{clazz}; T[] results = (T[]) serviceClient.invokeBlocking(opName,param,returnTypes); return results[0]; } /** * 调用 method 方法 * 无返回值 */ public void call(String method) throws AxisFault { QName opName = new QName(nameSpace,method); serviceClient.invokeBlocking(opName,new Object[]{}); } /** * 调用 method 方法,传递多个参数param * 无返回值 */ public void call(String method,Object[] param) throws AxisFault { QName opName = new QName(nameSpace,method); serviceClient.invokeBlocking(opName,param); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Class<?> retType = method.getReturnType(); if(args == null){args=new Object[]{};} if(retType!=null) { if(retType.isAssignableFrom(List.class)) { Type type = method.getGenericReturnType(); if (type instanceof ParameterizedType) { retType = (Class<?>)((ParameterizedType) type).getActualTypeArguments()[0]; retType = java.lang.reflect.Array.newInstance(retType,2).getClass(); } return Arrays.asList((Object[]) callback(method.getName(),args,retType)); }else { return callback(method.getName(),args,retType); } }else { call(method.getName(),args); return null; } } /** * 创建代理对象 * @throws IllegalArgumentException * @throws AxisFault */ @SuppressWarnings("unchecked") public static <T> T create(String endpoint,String nameSpace,Class<T> clazz) throws AxisFault, IllegalArgumentException { ClassLoader loader = clazz.getClassLoader(); Class<?>[] proxy = new Class[]{clazz}; T t =(T) Proxy.newProxyInstance(loader,proxy,new WebServiceUtil(endpoint,nameSpace)); return t; } }
posted on 2013-10-16 13:05 love so much 阅读(346) 评论(0) 编辑 收藏 举报