Hari Bajavada
Hari Bajavada

Reputation: 49

Logging SOAP message on http gateway spring integration

I am using spring integration (4.0.6) to make SOAP calls from rest service using int-http:outbound-gateway and int-ws:outbound-gateway. Is there a way to log SOAP request message only in case of exception.

<int:gateway id="myGateway" service-interface="myservice">
    <int:method name="getData" request-channel="reqChannel" reply-channel="repChannel">
</int-gateway>

my outbound-gateway configured as below

<int-http:outbound-gateway id="og1" request-channel="reqChannel" url="xxx" http-method="POST" reply-channel="repChannel" reply-timeout="10000" message-converters="converter" request-factory="reqFactory">

<bean id="reqFactory"
     class="org.springframework.http.client.SimpleClientHttpRequestFactory">
    <property name="cTimeout" value="20000"/>
    <property name="rTimeout"    value="20000"/>
</bean>

Upvotes: 1

Views: 867

Answers (2)

Yauza
Yauza

Reputation: 180

Ok, Try this method. Create your own error Handler which implements the spring Error Handler Interface. In your context file

<bean id="soapErrorHandlingSyncTaskExecutor"
    class="org.springframework.integration.util.ErrorHandlingTaskExecutor">
        <constructor-arg>
            <bean class="org.springframework.core.task.SyncTaskExecutor" />
        </constructor-arg>
        <constructor-arg>
            <bean class="ErrorHandle" 
p:errorSender-ref="errorSender"/>
        </constructor-arg>
    </bean>
<bean id="errorSender" class="org.springframework.integration.core.MessagingTemplate"
        p:defaultChannel-ref="errors" />

When ever your WS adapter throws any exception then org.springframework.integration.util.ErrorHandlingTaskExecutor catches this exception now its upto you how you handle the exception in your error handler.

The SOAP faultcode and faultstring is found in the MessagingException's cause (if a SoapFaultClientException).

Here i am sending the error messages to a channel for me to monitor.

Hope this helps.

Upvotes: 1

Artem Bilan
Artem Bilan

Reputation: 121177

Would be better to share some config and the point where would you like to get a log/exception.

Both, <int-http:outbound-gateway> and <int-ws:outbound-gateway> have <request-handler-advice-chain> ability, where you could use ExpressionEvaluatingRequestHandlerAdvice and send an ErrorMessage with the request (guilty?) message for any diagnostics.

You should configure the readTimeout on the appropriate ClientHttpRequestFactory, e.g. HttpComponentsClientHttpRequestFactory#setReadTimeout(int timeout).

Regarding converters and the wish to log their results in case of error...

How about to implement ClientHttpRequestInterceptor with the desired logic:

try {
   return execution.execute(request, body);
catch (Exception e) {
   this.errorChannel.send(...);
}

I'm prety sure that the byte[] body is your SOAP request.

See ClientHttpRequestInterceptor JavaDocs for more info.

The ClientHttpRequestInterceptor is part of RestTemplate, which can be injected to the http:outbound-gateway.

For the ws:outbound-gateway you should use ClientInterceptor with the handleFault implementation and extract MessageContext.getRequest() for your purpose.

Upvotes: 0

Related Questions