java - 在每个请求之前获取新的 JAX-WS WebService 端口

标签 java web-services jax-ws

我正在对一些 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/

相关文章:

java - 为什么 "new"关键字比赋值效率高这么多?

java - Java 和 Tomcat Web 服务中的 SSL 握手问题

java - RESTful 网络服务 : how to set headers in java to accept XMLHttpRequest allowed by Access-Control-Allow-Origin

java - 将对象传递给 JAX WS

java - Weblogic 12c Web 服务 NullPointerException

java - @RequestBody 注释在 Spring 中不起作用并给出 NoSuchMethodError 错误

java - 在 Eclipse 中运行多模块 Maven Web 应用程序

java - 如何使用axis 1.4将java对象转换为XML?

c# - 嵌套的 Try 和 Catch block

Java indexof() 搜索