Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.UnsupportedOperationException when set connection timeout to HttpComponentsMessageSender

I have a code to send messages to a server using SOAP. My code look like this:

private WebServiceTemplate makeTemplate() throws Exception {
  WebServiceTemplate localTemplate = new WebServiceTemplate();
  Jaxb2Marshaller marshaller = makeMarshaller();

  localTemplate.setMarshaller(marshaller);
  localTemplate.setUnmarshaller(marshaller);

  localTemplate.setMessageSender(httpComponentsMessageSender()); //<-- Add HttpComponentsMessageSender

  return localTemplate;
}

I create a HttpComponentsMessageSender to add a Trust Store to the request ( and add a timeout )

public HttpComponentsMessageSender httpComponentsMessageSender() throws Exception {
        HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
        httpComponentsMessageSender.setHttpClient(httpClient());
        httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
        httpComponentsMessageSender.setReadTimeout(30 * 1000);

        return httpComponentsMessageSender;
    }

    public HttpClient httpClient() throws Exception {
        return HttpClientBuilder.create().setSSLSocketFactory(sslConnectionSocketFactory())
                .addInterceptorFirst(new RemoveSoapHeadersInterceptor()).build();
    }

    public SSLConnectionSocketFactory sslConnectionSocketFactory() throws Exception {
        // NoopHostnameVerifier essentially turns hostname verification off as otherwise following error
        // is thrown: java.security.cert.CertificateException: No name matching localhost found
        return new SSLConnectionSocketFactory(sslContext() /*, NoopHostnameVerifier.INSTANCE  */);
    }

    public SSLContext sslContext() throws Exception {
        return SSLContextBuilder.create()
                .loadTrustMaterial(MyTrustStore, MyPasswd.toCharArray()).build();
    }

But when i use my template to send a message to server, i get this error:

SEVERE: null
java.lang.UnsupportedOperationException
        at org.apache.http.impl.client.InternalHttpClient.getParams(InternalHttpClient.java:211)
        at org.springframework.ws.transport.http.HttpComponentsMessageSender.setConnectionTimeout(HttpComponentsMessageSender.java:137)
        at 

    cl.newit.wstgr.main.WStgr.httpComponentsMessageSender(WStgr.java:308)
            at cl.newit.wstgr.main.WStgr.makeTemplate(WStgr.java:192)
            at cl.newit.wstgr.main.WStgr.genericSend(WStgr.java:165)
            at cl.newit.wstgr.main.WStgr.sendM1(WStgr.java:84)
            at cl.newit.wstgr.main.tester.TestM1(tester.java:236)
            at cl.newit.wstgr.main.tester.main(tester.java:67)

If i remove the lines with the timeouts:

// httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
// httpComponentsMessageSender.setReadTimeout(30 * 1000);

All works fine. Why the timeout not work? Someone can help me? Thanks in advance.

like image 303
Conde Avatar asked Apr 10 '18 03:04

Conde


1 Answers

The reason is you are setting the httpClient before timeouts, make it in this order

httpComponentsMessageSender.setReadTimeout(3*1000);       
httpComponentsMessageSender.setConnectionTimeout(3*1000);
httpComponentsMessageSender.setHttpClient(httpClient());
like image 177
Tayab Hussain Avatar answered Oct 23 '22 13:10

Tayab Hussain