【发布时间】: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