简体   繁体   中英

Set Http Conduit timeout dynamically

I am facing an issue where I need to set timeout for one particular webservice to a value other than the default value for all other services. Now I need to find a way where I can somehow programmatically override the http Conduit timeout for my service. Can someone please guide me how to achieve this? This is my current configuration and service:

<http:conduit name="*.http-conduit">
        <http:client ConnectionTimeout="${httpConduit.connectionTimeout:30000}" ReceiveTimeout="${httpConduit.receiveTimeout:30000}" />
        <http:tlsClientParameters disableCNCheck="${httpConduit.ssl.disableCNCheck:false}">
            <sec:keyManagers keyPassword="${httpConduit.ssl.keyPassword}">
                <sec:keyStore type="${httpConduit.ssl.keyStoreType}" password="${httpConduit.ssl.keyStorePassword}" file="${httpConduit.ssl.keyStoreFile}" />
            </sec:keyManagers>
            <sec:trustManagers>
                <sec:keyStore type="${httpConduit.ssl.trustStoreType}" password="${httpConduit.ssl.trustStorePassword}" file="${httpConduit.ssl.trustStoreFile}" />
            </sec:trustManagers>
            <sec:cipherSuitesFilter>
                <sec:include>.*_EXPORT_.*</sec:include>
                <sec:include>.*_EXPORT1024_.*</sec:include>
                <sec:include>.*_WITH_DES_.*</sec:include>
                <sec:include>.*_WITH_AES_.*</sec:include>
                <sec:include>.*_WITH_NULL_.*</sec:include>
                <sec:exclude>.*_DH_anon_.*</sec:exclude>
            </sec:cipherSuitesFilter>
        </http:tlsClientParameters>
    </http:conduit>



<jaxrs:client id="testProxy" address="${test.endpoint}" threadSafe="true" serviceClass="foo.TestProxy">
        <jaxrs:headers>
            <entry key="Accept-Encoding" value="gzip,deflate" />
            <entry key="Content-Type" value="application/json;charset=UTF-8" />
            <entry key="Content-Length" value="92" />
            <entry key="Connection" value="Keep-Alive" />
        </jaxrs:headers>
        <jaxrs:providers>
            <ref bean="jsonProvider" />
        </jaxrs:providers>
        <jaxrs:features>
            <!-- Enables logging of the 'on-the-wire' request/response -->
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxrs:features>
    </jaxrs:client>

Issue resolved: I created an out interceptor bean in the client and a CustomINterceptor class to update the values:

        <jaxrs:headers>
            <entry key="Accept-Encoding" value="gzip,deflate" />
            <entry key="Content-Type" value="application/json;charset=UTF-8" />
            <entry key="Content-Length" value="92" />
            <entry key="Connection" value="Keep-Alive" />
        </jaxrs:headers>
        <jaxrs:providers>
            <ref bean="jsonProvider" />
        </jaxrs:providers>
        <jaxrs:features>
            <!-- Enables logging of the 'on-the-wire' request/response -->
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxrs:features>
       <jaxrs:outInterceptors>
            <ref bean="customInterceptor" />
        </jaxrs:outInterceptors>
    </jaxrs:client>

<bean id="customInterceptor" class="com.bmo.channel.alert.interceptor.CustomInterceptor" ></bean>


public class CustomInterceptor extends  AbstractPhaseInterceptor<Message>{

    
    
    public CustomInterceptor () {
        super(Phase.SETUP);
    }
    @Override
    public void handleMessage(Message message) {
        System.out.println("Inside handle message");
        try {
            final Conduit conduit = message.getExchange().getConduit(message);
            if (conduit instanceof HTTPConduit) {
                final HTTPConduit httpConduit = (HTTPConduit) conduit;
                HTTPClientPolicy policy = httpConduit.getClient();
                policy.setReceiveTimeout(timeout);
                policy.setConnectionTimeout(timeout);
                httpConduit.setClient(policy);
                System.out.println("ConnectionTimeout()  -- " + policy.getConnectionTimeout());
                System.out.println("ReceiveTimeout -- " + policy.getReceiveTimeout());
                System.out.println("HTTPClientPolicy -- " + policy.getHost());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM