CXF通过JaxWsDynamicClientFactory添加代理动态调用WSDL
JaxWsDynamicClientFactory通过bus添加代理
/**
* 生成JaxWsDynamicClientFactory
*/
public static JaxWsDynamicClientFactory getJaxWsDynamicClientFactory() {
IParamsConfigService paramsConfigService = SpringUtil.getBean(IParamsConfigService.class);
HTTPConduitConfigurer httpConduitConfigurer = (name, address, httpConduit) -> {
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
log.debug("address: " + address);
//从数据库中取出请求代理参数
String host = paramsConfigService.getConfigValue("PROXY.HOST");
int port = Integer.parseInt(paramsConfigService.getConfigValue("PROXY.PORT"));
log.info("外网地址添加代理{} {}", host, port);
httpClientPolicy.setProxyServer(host);
httpClientPolicy.setProxyServerPort(port);
httpClientPolicy.setAllowChunking(false);
httpClientPolicy.setConnectionTimeout(30000);
httpClientPolicy.setReceiveTimeout(30000);
httpConduit.setClient(httpClientPolicy);
};
Bus bus = BusFactory.getDefaultBus();
bus.setExtension(httpConduitConfigurer, HTTPConduitConfigurer.class);
JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance(bus);
log.debug("创建 JaxWsDynamicClientFactory");
return jaxWsDynamicClientFactory;
}
/**
* 获取wsdl客户端
*
* @param url 请求地址
* @return Client
*/
public static Client getWsdlClient(String url) {
Client client = null;
try {
client = getJaxWsDynamicClientFactory().createClient(url);
} catch (Exception e) {
log.error("创建Client出错: " + e.getMessage());
throw new ServiceException("创建Client出错: " + e.getMessage());
}
return client;
}
/**
*
* @param wsdl 请求地址
* @return 返回结果
*/
public String getRand(String wsdl) {
Object[] result;
try {
Client client = getWsdlClient(wsdl);
result = client.invoke("getRand");
client.destroy();
} catch (Exception e) {
throw new ServiceException("getRand请求异常");
}
String rand = result[0].toString();
return rand;
}