WebService call tool (AXIS2)

Source: Internet
Author: User

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. a Xis2.client. options; import org. apache. axis2.rpc. client. RPCServiceClient;/*** WebService call tool * usage: * 1 :( recommended) WebServiceUtil. create (release address, namespace, interface class); * The created object can be called directly as a local interface, for example, * WebService service = WebServiceUtil. create ("http: // localhost: 8080/test/cxf/webservice", * "http://www.yanfan.com/", WebService. class); * List <CeicsNeinfo> list = service. selectNeList (); * 2: Create Util WebServiceUtil util = new WebS ErviceUtil ("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; // nameSpace private int timeout = 30; // timeout time, unit: seconds public WebServiceUtil (String endpoint, Strin G 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;}/*** call method * has a return value. The return value type is clazz */public <T> T callback (String method, Class <T> clazz) throws AxisFault {retu Rn callback (method, new Object [] {}, clazz);}/*** call the method and pass a parameter param * with a return value, return Value Type clazz */public <T> T callback (String method, Object param, Class <T> clazz) throws AxisFault {return callback (method, new Object [] {param }, clazz);}/*** call the method and pass multiple parameters. param * has a return value. The return value type is clazz */@ SuppressWarnings ("unchecked ") public <T> T callback (String method, Object [] param, Class <T> clazz) throws AxisFau Lt {QName opName = new QName (nameSpace, method); Class <?> [] ReturnTypes = new Class [] {clazz}; T [] results = (T []) serviceClient. invokeBlocking (opName, param, returnTypes); return results [0];}/*** call method * No return value */public void call (String method) throws AxisFault {QName opName = new QName (nameSpace, method); serviceClient. invokeBlocking (opName, new Object [] {});}/*** call the method and pass multiple parameters param * No return value */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 ;}/ *** create proxy object * @ 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 ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.