如何解決Webservice第一次訪問特別慢的問題
Webservice第一次訪問特別慢問題
最近做一個(gè)項(xiàng)目遇到首次加載webservice的時(shí)候特別慢,于是Google一番,得到結(jié)果是
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>原理是:由于web代理默認(rèn)是開啟的,也就是HttpWebRequest.DefaultWebProxy的值不為null,而這個(gè)DefaultWebProxy是一個(gè)全局變量。故第一次調(diào)用webservice方法的時(shí)候只有等這個(gè)默認(rèn)代理超時(shí)以后才能繞過,所以第一次比較慢。
然而這個(gè)方法還不是特別慢的最大原因,所以即使這么做了效果依然沒有明顯的變快,于是又是一番的Google。
最終發(fā)現(xiàn)一個(gè)另一個(gè)因素:
原因很簡單,就是因?yàn)樵诘谝淮芜B接Webservice時(shí),應(yīng)用程序動態(tài)編譯生成序列化程序集導(dǎo)致的慢。
問題知道了那么就說說如何解決
1、首先如何提前生成序列化程序集

這個(gè)時(shí)候你會發(fā)現(xiàn)你的bin目錄下回生成一個(gè)“***.XmlSerializers.dll”
2、接下來就簡單了,在程序啟動的時(shí)候就把這個(gè)文件加載進(jìn)來就OK了
Assembly.LoadFrom(Application.StartupPath + "\\***.XmlSerializers.dll");
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
bool ok;
var m = new System.Threading.Mutex(true, "***.exe", out ok);
if (!ok) return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Assembly.LoadFrom(Application.StartupPath + "\\***.XmlSerializers.dll");
Application.Run(new FormMain());
GC.KeepAlive(m);
}3、騷年啟動你的應(yīng)用程序吧
cxf動態(tài)調(diào)用Webservice接口
package cxfClient;
import org.apache.cxf.endpoint.Endpoint;
import javax.xml.namespace.QName;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;
public class CxfClient {
public static void main(String[] args) throws Exception {
String url = "http://localhost:9091/Service/SayHello?wsdl";
String method = "say";
Object[] parameters = new Object[]{"我是參數(shù)"};
System.out.println(invokeRemoteMethod(url, method, parameters)[0]);
}
public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
if (!url.endsWith("wsdl")) {
url += "?wsdl";
}
org.apache.cxf.endpoint.Client client = dcf.createClient(url);
//處理webService接口和實(shí)現(xiàn)類namespace不同的情況,CXF動態(tài)客戶端在處理此問題時(shí),會報(bào)No operation was found with the name的異常
Endpoint endpoint = client.getEndpoint();
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation);
BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding();
if(bindingInfo.getOperation(opName) == null){
for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){
if(operation.equals(operationInfo.getName().getLocalPart())){
opName = operationInfo.getName();
break;
}
}
}
Object[] res = null;
try {
res = client.invoke(opName, parameters);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot2以代碼的方式統(tǒng)一配置Jackson教程
這篇文章主要介紹了Springboot2以代碼的方式統(tǒng)一配置Jackson教程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云
這篇文章主要介紹了在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型
這篇文章主要介紹了SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Java方法遞歸的形式和常見遞歸算法(方法遞歸結(jié)合File類查找文件)
方法遞歸方法直接調(diào)用自己或者間接調(diào)用自己的形式稱為方法遞歸( recursion),遞歸做為一種算法在程序設(shè)計(jì)語言中廣泛應(yīng)用,這篇文章主要介紹了Java方法遞歸的形式和常見遞歸算法-方法遞歸結(jié)合File類查找文件,需要的朋友可以參考下2023-02-02
詳解SpringBoot整合RabbitMQ如何實(shí)現(xiàn)消息確認(rèn)
這篇文章主要介紹了SpringBoot整合RabbitMQ是如何實(shí)現(xiàn)消息確認(rèn)的,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

