CXF客户端-每个请求之间的TCP连接都关闭

问题描述 投票:1回答:1

我正在实现一个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 A1001 same socket

但是我使用“ AXIS P5534网络摄像机”,重新创建了套接字。

Multiples request against network camera

这是我的PC(10.110.0.106)正在关闭连接enter image description here

使用的版本:

  • cxf-rt-ws-security版本3.3.6
  • cxf-rt-rs-client版本3.3.6

我正在实现一个SOAP客户端,一般的工作都很好,但是我发现了一个陷阱,因为在每个请求上,TCP连接都是关闭的(对我来说)。这是次优的,当...

java cxf onvif cxf-client
1个回答
0
投票

最后我发现了问题,该代码是完全可用的,并且该连接已由远程设备关闭。

© www.soinside.com 2019 - 2024. All rights reserved.