[英]Getting a new JAX-WS WebService Port before each request
我正在对某些Java应用程序进行更改,并且注意到它们在循环的每次迭代中都实例化了服务客户端,如下所示:
for(String name : names) {
HelloService helloWS = new HelloService();
HellowServicePort helloPort = helloWS.getHelloServicePort();
helloPort.sayHello(name);
}
像这样,而不是一次获取端口:
HelloService helloWS = new HelloService();
HellowServicePort helloPort = helloWS.getHelloServicePort();
for(String name : names) {
helloPort.sayHello(name);
}
使用第二种方法有什么区别吗?
是的,您可以多次重用端口对象,而无需创建一个新对象。
如JoãoRebelo所指出,我的先前评论不正确。
我已经通过此计算器服务对其进行了验证
http://www.webservicex.com/globalweather.asmx?WSDL
使用wsdl2java http://www.webservicex.com/globalweather.asmx?WSDL
导出工件后, wsdl2java http://www.webservicex.com/globalweather.asmx?WSDL
以下代码可以正常工作。
Calculator calculatorClient = new Calculator();
ICalculator port = calculatorClient.getICalculator();
for(int i = 0; i < 10; i++) {
float x = (float)Math.random() * 100;
float y = (float)Math.random() * 100;
System.out.printf("%f + %f = %f%n", x, y, port.add(x, y));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.