0

While calling soap service from spring integration how to add timeout ? Below is the code where I'm calling a soap service using Ws.marshallingOutboundGateway().

@Bean
  public IntegrationFlow flow() {
    return flow ->
        flow.handle(Ws.marshallingOutboundGateway(webServiceTemplate()).uri(someSOAPUrl));
    };
  }

  @Bean
  public WebServiceTemplate webServiceTemplate() throws Exception {
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    R123Marshaller marshaller = new R123Marshaller();
    marshaller.setContextPath("com.example.request.soap123");
    webServiceTemplate.setMarshaller(marshaller);

    Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
    unmarshaller.setContextPath("com.example.request.soap123");
    webServiceTemplate.setUnmarshaller(unmarshaller);

    return webServiceTemplate;
  }

Is there a way I can do something like below -

.handle(Http.outboundGateway(someURL, restTemplateConfig.restTemplate())

Here I have added timeout in the resttemplate that I've passed.

1 Answers1

0

Use the HttpComponentsMessageSender in the template, instead of the default

https://docs.spring.io/spring-ws/docs/current/reference/html/#_using_the_client_side_api

/**
 * Sets the timeout until a connection is established. A value of 0 means <em>never</em> timeout.
 *
 * @param timeout the timeout value in milliseconds
 * @see org.apache.http.params.HttpConnectionParams#setConnectionTimeout(org.apache.http.params.HttpParams, int)
 */
public void setConnectionTimeout(int timeout) {
    if (timeout < 0) {
        throw new IllegalArgumentException("timeout must be a non-negative value");
    }
    org.apache.http.params.HttpConnectionParams.setConnectionTimeout(getHttpClient().getParams(), timeout);
}

/**
 * Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
 *
 * @param timeout the timeout value in milliseconds
 * @see org.apache.http.params.HttpConnectionParams#setSoTimeout(org.apache.http.params.HttpParams, int)
 */
public void setReadTimeout(int timeout) {
    if (timeout < 0) {
        throw new IllegalArgumentException("timeout must be a non-negative value");
    }
    org.apache.http.params.HttpConnectionParams.setSoTimeout(getHttpClient().getParams(), timeout);
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks @Gary. If it is not able to get response within the given timeout then what will happen? – Somnath Mukherjee Aug 23 '22 at 05:39
  • Gary, I have just added these lines in webServiceTemplate method before returning the webServiceTemplate instance ? - HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender(); httpComponentsMessageSender.setConnectionTimeout(500); httpComponentsMessageSender.setReadTimeout(500); webServiceTemplate.setMessageSender(httpComponentsMessageSender); is it fine? – Somnath Mukherjee Aug 23 '22 at 06:50