Webservice client, should i keep service or port instance?

后端 未结 2 861
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 22:17


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 cre

相关标签:
2条回答
  • 2021-01-19 22:55

    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.

    0 讨论(0)
  • 2021-01-19 22:56

    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

    0 讨论(0)
提交回复
热议问题