【问题标题】:Getting a new JAX-WS WebService Port before each request在每个请求之前获取一个新的 JAX-WS WebService 端口
【发布时间】:2016-08-13 11:26:07
【问题描述】:

我正在对一些 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);
}

使用第二种方法有什么不同吗?

【问题讨论】:

  • @11thdimension 为什么说“每次调用都需要不同的对象”?
  • @GabrielBB,您是否尝试过第二种方法并获得了一些 SOAPHandlers 来查看您是否注意到任何不同之处?你在使用任何 JavaEE 容器吗?
  • @JoãoRebelo 我假设它会打开一个套接字来发送消息。如果我错了,请纠正我。
  • @11thdimension,我问的原因是我不知道。但我不认为这直接意味着打开一个新的 TCP 套接字。请参阅docs.oracle.com/javase/7/docs/api/javax/xml/ws/spi/…。我不知道每个实现的细节。但我假设除非他同时使用该对象,否则他将受益于不多次实例化它。
  • @JoãoRebelo 你说得对,不需要新的端口对象,一个实例可以重复使用。我刚刚验证过了。

标签: java web-services jax-ws


【解决方案1】:

是的,您可以多次重复使用端口对象而无需创建新对象。

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));
}

【讨论】:

猜你喜欢
  • 2015-03-29
  • 1970-01-01
  • 2018-04-04
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多