Webservice客户端,我应该保留服务或端口实例吗?

我正在使用cxf codegen开发Web服务客户端,并且它生成类MyService extends Service了客户端部分的MyService extends Service
我现在的问题是,当我创建客户端时,应该是每次我想发送请求时创建的MyService对象还是保留它并且每次创建端口? 或者我可以保留港口吗? 制作客户的最佳方式是什么?

谢谢

保持港口周围绝对是最好的选择,但请记住线程安全方面:

http://cxf.apache.org/faq.html#FAQ-AreJAXWSclientproxiesthreadsafe%3F

每次发送请求时创建Service类都是非常低效的方式。 创建Web服务客户端的正确方法是首次启动应用程序。 例如,我从Web应用程序调用Web服务并使用ServletContextListener初始化Web服务。 可以像这样创建CXF Web服务客户端:

 private SecurityService proxy; /** * Security wrapper constructor. * * @throws SystemException if error occurs */ public SecurityWrapper() throws SystemException { try { final String username = getBundle().getString("wswrappers.security.username"); final String password = getBundle().getString("wswrappers.security.password"); Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( username, password.toCharArray()); } }); URL url = new URL(getBundle().getString("wswrappers.security.url")); QName qname = new QName("http://hltech.lt/ws/security", "Security"); Service service = Service.create(url, qname); proxy = service.getPort(SecurityService.class); Map requestContext = ((BindingProvider) proxy).getRequestContext(); requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString()); requestContext.put(BindingProvider.USERNAME_PROPERTY, username); requestContext.put(BindingProvider.PASSWORD_PROPERTY, password); Map> headers = new HashMap>(); headers.put("Timeout", Collections.singletonList(getBundle().getString("wswrappers.security.timeout"))); requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers); } catch (Exception e) { LOGGER.error("Error occurred in security web service client initialization", e); throw new SystemException("Error occurred in security web service client initialization", e); } } 

在应用程序启动时,我创建了这个类的实例并将其设置为应用程序上下文。 还有一种使用spring创建客户端的好方法。 看看这里: http : //cxf.apache.org/docs/writing-a-service-with-spring.html

希望这可以帮助。