我正在实现一个SOAP客户端,一般的工作都很好,但是我发现一个陷阱,因为在每个请求上,TCP连接都是关闭的(对我来说)。
这不是最佳选择,使用HTTPS时更糟,因为每个请求都交换了证书。
httpconduit的创建和SOAP服务的调用
public static void test(final String destination) {
System.setProperty("javax.xml.soap.SAAJMetaFactory", "com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl");
TLSClientParametersFactory tlsClientParametersFactory = new TLSClientParametersFactory
.Builder()
.acceptAllCerts()
.build();
JAXRSClientFactoryBean clientFactoryBean = new JAXRSClientFactoryBean();
clientFactoryBean.setAddress(destination);
WebClient webClient = clientFactoryBean.createWebClient();
ClientConfiguration config = WebClient.getConfig(webClient);
HTTPConduit conduit = config.getHttpConduit();
HTTPClientPolicy client = conduit.getClient();
client.setConnection(ConnectionType.KEEP_ALIVE);
client.setConnectionTimeout(TimeUnit.MINUTES.toMillis(60));
conduit.setTlsClientParameters(tlsClientParametersFactory.createTlsClient());
Media media = createFactory(Media.class, conduit, tlsClientParametersFactory, destination);
// The call of the soap service, with the expectation to have the same TCP connection used.
List<Profile> profiles = media.getProfiles();
List<Profile> profiles2 = media.getProfiles();
List<Profile> profiles3 = media.getProfiles();
List<Profile> profiles4 = media.getProfiles();
List<Profile> profiles5 = media.getProfiles();
创建SOAP端口
private static <T> T createFactory(final Class<T> clazz,
final Conduit conduit,
final TLSClientParametersFactory factory,
final String destination) {
Map<String, Object> props = new HashMap<>();
props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
props.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
props.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordCallback.class.getName());
props.put(WSHandlerConstants.USER, "test");
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(props);
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
proxyFactory.setAddress(destination);
proxyFactory.setServiceClass(clazz);
SoapBindingConfiguration config = new SoapBindingConfiguration();
config.setVersion(Soap12.getInstance());
proxyFactory.setBindingConfig(config);
proxyFactory.setConduitSelector(new PreexistingConduitSelector(conduit));
Map<String, Object> properties = proxyFactory.getProperties();
if (properties == null) {
properties = Maps.newHashMap();
}
properties.put(Client.KEEP_CONDUIT_ALIVE, true);
proxyFactory.setProperties(properties);
T t = proxyFactory.create(clazz);
TLSClientParametersFactory.setSsClientParametersToPort(factory, t);
return t;
}
我做错了什么?
注意:完成新请求后,连接将关闭。否则,TCP连接将保持活动状态。
另一个输入 并非所有目标目标都存在该问题。例如,如果我使用设备“ AXIS A1001网络门控制器”,则系统无需重新创建tcp套接字即可工作(tcp对话为彩色)
但是我使用“ AXIS P5534网络摄像机”,重新创建了套接字。
使用的版本: