我正在对一些 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
下面的代码工作得很好。
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));
}
关于java - 在每个请求之前获取新的 JAX-WS WebService 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749639/