我正在尝试配置 CXF 总线,为所有客户端设置常见的超时。为此,我使用 HttpConduitFeature 并在初始配置期间将其添加到总线,如下所示:
@Configuration
public class CxfContext {
@Autowired
private SoapConfigurations soapConfigurations;
@Bean
public Bus bus() {
SpringBus b = new SpringBus();
List<Feature> features = ImmutableList.<Feature> builder() //
.add(loggingFeature()) //
.add(conduitFeature()) //
.build();
b.setFeatures(features);
return b;
}
private LoggingFeature loggingFeature() {
LoggingFeature f = new LoggingFeature();
f.setPrettyLogging(true);
f.setVerbose(true);
f.setLimit(-1);
return f;
}
private HttpConduitFeature conduitFeature() {
HttpConduitFeature feature = new HttpConduitFeature();
HttpConduitConfig conduitConfig = new HttpConduitConfig();
HTTPClientPolicy clientPolicy = new HTTPClientPolicy();
clientPolicy.setConnectionTimeout(10000);
clientPolicy.setReceiveTimeout(10000);
conduitConfig.setClientPolicy(clientPolicy);
feature.setConduitConfig(conduitConfig);
return feature;
}
}
之后,在生成 Soap 客户端时,我使用 JaxWsProxyFactoryBean 并将总线设置为它:
@Configuration
public class SoapClientContext {
@Autowired
private Bus bus;
@Bean
public IService myService(SoapConfigurations soapConfigurations) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(soapConfigurations.getUrl());
factory.setBus(bus);
return factory.create(IService.class);
}
}
客户端正确使用总线中配置的日志记录功能,但未考虑 HttpConduitFeature。所以超时时间是默认的1分钟,而不是配置的10秒。
我现在看到的唯一解决方案是直接在工厂中设置 HttpConduitFeature:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress(soapConfigurations.getUrl());
factory.setBus(bus);
factory.getFeatures().add(conduitFeature());
我是否需要一些额外的配置,以便生成的客户端直接使用总线中定义的 HttpConduitFeature?
我用下面的方法做到了
@Bean(name = "mltfClient")
public Mltf mltf(MltfInMessageInterceptor in,MltfInMessageInterceptor out) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(Mltf.class);
factoryBean.setAddress(address);
factoryBean.getOutInterceptors().add(out);
factoryBean.getInInterceptors().add(in);
return (Mltf) factoryBean.create();
}
@Bean HTTPConduit conduit(Mltf mtlf)
{
Client client = ClientProxy.getClient(mtlf);
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setReceiveTimeout(30000);
policy.setConnectionTimeout(10000);
policy.setConnection(ConnectionType.CLOSE);
policy.setAllowChunking(false);
HTTPConduit conduit=(HTTPConduit) client.getConduit();
conduit.setClient(policy);
return conduit;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)