【问题标题】:Apache cxf port generic creationApache cxf 端口泛型创建
【发布时间】:2018-08-07 01:12:33
【问题描述】:

在我的项目中,我必须创建一些 Soap Web 服务客户端我正在使用 apache cxf 创建端口并使用 Web 服务我注意到端口创建我复制了很多代码,所以我创建了一个抽象类像这样

public abstract class WebServiceConsumerPort<P>{

    protected Class<P> port;

    private static Logger log = LoggerFactory.getLogger(WebServiceConsumerPort.class);

    public void createPort(String url, String timeoutS) {

        try {
            Long timeout = Long.parseLong(timeoutS);
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.setServiceClass(port.getClass());
            factory.setAddress(url);

            port = (Class<P>) factory.create();

            Client client = ClientProxy.getClient(port);

            if (client != null) {

                log.info("Timeout WEB WERVICES: {}", timeout);

                HTTPConduit conduit = (HTTPConduit) client.getConduit();
                HTTPClientPolicy policy = new HTTPClientPolicy();
                policy.setAllowChunking(false);
                policy.setConnectionTimeout(timeout * 1000);
                policy.setReceiveTimeout(timeout * 1000);
                conduit.setClient(policy);
            }
        } catch (Exception e) {
            log.error("Peoblem creating the port", e);
        }
    }
}

然后我想创建特定的类来创建端口: 例如,我有带有方法 method1() 和 method2() 的 WebService1 和带有方法 method3() 和 method4() 的 WebService2

我的消费者看起来像:

public class WebService1Consumer extends WebServiceConsumerPort<WebService1Interface> {
        public void consumeMethod1() {
            port.method1();
        }

        public void consumeMethod2() {
            port.method2();
        }
    }

另一个类看起来像

public class WebService2Consumer extends WebServiceConsumerPort<WebService2Interface> {
        public void consumeMethod1() {
            port.method3();
        }

        public void consumeMethod2() {
            port.method4();
        }
    }

但是在两个类中说不能解析端口中的方法。

【问题讨论】:

    标签: java generics cxf hierarchy


    【解决方案1】:

    终于成功了,代码如下:

    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.frontend.ClientProxy;
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.apache.cxf.transport.http.HTTPConduit;
    import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public abstract class WebServiceConsumerPort<P>{
    
    
        protected P puerto;
        private static Logger log = LoggerFactory.getLogger(WebServiceConsumerPort.class);
    
        public void creaPuerto(String url, String timeoutS, Class<P> tipoClase) {
    
            try {
                Long timeout = Long.parseLong(timeoutS);
                JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
                factory.setServiceClass(tipoClase);
                factory.setAddress(url);
                puerto = (P) factory.create();
    
                Client client = ClientProxy.getClient(puerto);
    
                if (client != null) {
                    HTTPConduit conduit = (HTTPConduit) client.getConduit();
                    HTTPClientPolicy policy = new HTTPClientPolicy();
                    policy.setAllowChunking(false);
                    policy.setConnectionTimeout(timeout * 1000);
                    policy.setReceiveTimeout(timeout * 1000);
                    conduit.setClient(policy);
                }
                nIntento = 0;
            } catch (Exception e) {
                puerto = null;
                nIntento++;
            }
        }
    }
    

    感谢@ben-thurley 在post 中的回答

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      相关资源
      最近更新 更多