0
votes

When I used two HTTP endpoints in a flow, Mule throws this exception. I've found the way to deal with this problem: use the second HTTP endpoint in asynchronous mode, but it is not a good way.

ERROR DefaultSystemExceptionStrategy [[testdemo].connector.http.mule.default.receiver.03]: Caught exception in Exception Strategy: Attempted read on closed stream.
java.io.IOException: Attempted read on closed stream.
    at org.apache.commons.httpclient.AutoCloseInputStream.isReadAllowed(AutoCloseInputStream.java:183)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:126)
    at org.mule.model.streaming.DelegatingInputStream.read(DelegatingInputStream.java:58)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025)
    at org.mule.transformer.simple.ObjectToOutputHandler$3.write(ObjectToOutputHandler.java:76)
    at org.mule.transport.http.HttpServerConnection.writeResponse(HttpServerConnection.java:315)
    at org.mule.transport.http.HttpMessageReceiver$HttpWorker.run(HttpMessageReceiver.java:164)
    at org.mule.work.WorkerContext.run(WorkerContext.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

my code :

<flow doc:name="httpPost" name="httpPost">
    <http:inbound-endpoint doc:name="HTTP" keep-alive="true"
        exchange-pattern="request-response" host="localhost" path="service"
        port="8082" />

    <http:body-to-parameter-map-transformer />

    <!-- This component is just set to show the message accecpted from the request -->
    <scripting:component>
        <scripting:script engine="groovy">
            println payload['name']
            return payload['name']+'123'
        </scripting:script>
    </scripting:component>
</flow>

<flow doc:name="httpPost" name="client">
    <http:inbound-endpoint doc:name="HTTP" name="httpClient"
        exchange-pattern="request-response" host="localhost" path="client"
        port="8082" encoding="UTF-8" />

    <http:body-to-parameter-map-transformer />

    <scripting:component>
        <scripting:script engine="groovy">
            payload['name'] = 'opasso'
            def paramstr = ""
                for( param in payload){
                paramstr = paramstr + "&amp;" + param.key+ "=" + param.value
            }
            println "querystr:$paramstr"
            return paramstr.substring(1)
        </scripting:script>
    </scripting:component>

    <http:outbound-endpoint address="http://localhost:8082/service"
        exchange-pattern="request-response" contentType="application/x-www-form-urlencoded"
        method="POST" encoding="UTF-8" />

    <!-- This component is just set to show the message accecpted from the request -->
    <scripting:component>
        <scripting:script engine="groovy">
            def msg = "return payload:$payload;".toString()
            println msg
            return payload
        </scripting:script>
    </scripting:component>
</flow>
1

1 Answers

0
votes

The problem is related to the funky business you're doing in the scripting:component with the streaming message payload generated by the http:outbound-endpoint. The script probably consumes the input stream, leaving it in a state where it can't used anymore.

Try adding a <object-to-string-transformer /> right after the http:outbound-endpoint to deserialize the stream into a string so the payload can be used both in the scripting:component and by Mule.