Webservice client, should i keep service or port instance?

こ雲淡風輕ζ 提交于 2019-12-20 02:13:20

问题



i am developing web service client with cxf codegen and it generating class MyService extends Service for client part.
My question now is when i am creating client, should be MyService object created each time i want send request or keep it and each time create Port? Or can i keep Port as well? What is the best way to make client?

Thanks


回答1:


Keeping the Port around is definitely the best performing option, but keep in mind the thread safety aspects:

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




回答2:


To create Service class each time the request is sent would be very inefficient way. Proper way to create web service client would be on first application start up. For e.g. I call web services from web application and using ServletContextListener to initialize web service. CXF web service client can be created like this:

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<String, Object> 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<String, List<String>> headers = new HashMap<String, List<String>>();
        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);
    }
}

And on application start up I create this class' instance and set it to application context. Also there is a nice way to create client using spring. Take a look here: http://cxf.apache.org/docs/writing-a-service-with-spring.html

Hope this helps.



来源:https://stackoverflow.com/questions/11428849/webservice-client-should-i-keep-service-or-port-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!