Apache cxf 端口通用创建
Apache cxf port generic creation
在我的项目中,我必须创建一些 Soap Web 服务客户端我正在使用 apache cxf 创建端口并使用 Web 服务我注意到创建端口我复制了很多代码,所以我创建了一个像这样抽象 class
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);
}
}
}
然后我要创建特定的class来创建端口:
例如,我有 WebService1 方法 method1() 和 method2() 和 WebService2 方法 method3() 和 method4()
我的消费者看起来像:
public class WebService1Consumer extends WebServiceConsumerPort<WebService1Interface> {
public void consumeMethod1() {
port.method1();
}
public void consumeMethod2() {
port.method2();
}
}
另一个class看起来像
public class WebService2Consumer extends WebServiceConsumerPort<WebService2Interface> {
public void consumeMethod1() {
port.method3();
}
public void consumeMethod2() {
port.method4();
}
}
但是有两个 classes 说无法解析端口中的方法。
终于成功了,代码如下:
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
中的回答
在我的项目中,我必须创建一些 Soap Web 服务客户端我正在使用 apache cxf 创建端口并使用 Web 服务我注意到创建端口我复制了很多代码,所以我创建了一个像这样抽象 class
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);
}
}
}
然后我要创建特定的class来创建端口: 例如,我有 WebService1 方法 method1() 和 method2() 和 WebService2 方法 method3() 和 method4()
我的消费者看起来像:
public class WebService1Consumer extends WebServiceConsumerPort<WebService1Interface> {
public void consumeMethod1() {
port.method1();
}
public void consumeMethod2() {
port.method2();
}
}
另一个class看起来像
public class WebService2Consumer extends WebServiceConsumerPort<WebService2Interface> {
public void consumeMethod1() {
port.method3();
}
public void consumeMethod2() {
port.method4();
}
}
但是有两个 classes 说无法解析端口中的方法。
终于成功了,代码如下:
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
中的回答