cxf动态调用WebService

最近突然发现原来调用webservice服务端如此的方便,于是自己看了看,这种是使用cxf的JaxWsDynamicClientFactory动态调用的,主要时间花在了找包的时间上,另外,这种方式虽然调用方便,但是每一次调用都会扫描到接口中的所有方法,所以叫动态。下面直接看下具体的:
1.服务端
我采用的是JDK自带的webService注解方式,JDK1.6以上支持
package com.bing.log;

import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class RouteLog{

@WebMethod
public void writeLogMsg(String msg) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    SysWriteLog.writeLog(msg);
}

@WebMethod
public void writeLogMsg2(String msg,String msg2) {
    System.out.println("222");
    SysWriteLog.writeLog(msg);
}

}

发布接口
package com.bing.client;

import javax.xml.ws.Endpoint;

import com.bing.log.RouteLog;

public class LogServer {
public static void main(String[] args) {
System.out.println(“正在发布…”);
Endpoint endpoint =Endpoint.publish(“http://192.168.0.104/routeLog“, new RouteLog());
System.out.println(“发布成功..”);
}
}

2.客户端
我选取的是cxf 3.0.4的包
程序中需要依赖的最少包,本地maven资源库不全,不能直接提供pom文件
cxf动态调用WebService_第1张图片

package com.bing.util;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class AppUtil {

/**
 * 
 * 远程调用WebService 
 * 
 * @param methodName    方法类型
 * @param param         参数
 * @return
 * @throws Exception
 */
public static Object[] routeServer(String methodName,Object param) throws Exception{
    String url = "http://192.168.0.104/routeLog?wsdl";
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();    // 策略
    httpClientPolicy.setConnectionTimeout( 36000 );    //连接超时 
    httpClientPolicy.setAllowChunking( false );   
    httpClientPolicy.setReceiveTimeout( 10000 );       //接收超时
    Client client = dcf.createClient(url);
    HTTPConduit http = (HTTPConduit) client.getConduit();  
    http.setClient(httpClientPolicy);
    return client.invoke(methodName, param);

}

}

package com.bing.util;

/**
*
* @author Bing
*
* cxf动态调用WebService
*
*/
public class Client {
public static void main(String[] args) throws Exception {
Object[] o = AppUtil.routeServer(“writeLogMsg”,new Object[]{“eeee”});
Object[] o2= AppUtil.routeServer(“writeLogMsg2”, new Object[]{“eeee”,”eeeeeeettt”});
//System.out.println(o.length);

}

}

你可能感兴趣的:(java,webservice)