Spring integration errorChannel, error handle

时间:2016-08-31 18:00:30

标签: spring error-handling spring-integration

Scenario could be: my expectation could be 10 datapoint in batch, and I want to give response for {failed 5, pass 5} or sth.

my logic is to split the batch into data element and do validation.
successful validation will send to aggreagtor,
failed validation will throw error and pick up by error channel.

recipient-list-router take the errorChannel as inputChannel and 2 filter connect to it, the purpose is to filter some type of error to send response directly(eception unrelated to user input - server error or etc) and some type of client side error will go to aggregator to build response.

Is any problem with the logic? My problem is I keep getting "Reply message received but the receiving thread has already received a reply" when build up the Result using service-activator after aggregator. this service-activator connect to replyChannel and it seems like there are some message already sent to this channel?

I checked my integration work flow only this service-activator and server error branch after "error filter" connect to replyChannel(but the handle is never called.)

Something wrong? BTW, can recipient-list-router or other type of endpoint connect to errorChannel? or it has to be service-activator as what I saw in all the example online?(but they are really simple example..)

Sample XML

<int:gateway id="myGateway" service-interface="someGateway" default-request-channel="splitChannel" error-channel="errorChannel"  default-reply-channel="replyChannel" async-executor="MyThreadPoolTaskExecutor"/>

<int:splitter input-channel="splitChannel" output-channel="transformChannel" method="split">
    <bean class="Splitter" />
</int:splitter>

<int:transformer id="transformer" input-channel="transformChannel" method="transform" output-channel="aggregateChannel">
    <bean class="Transformer"/>  // this may throw the validation error (filter_ErrorType_1), if it cannot transform
</int:transformer>

<int:aggregator id="aggregator"
    input-channel="aggregateChannel"
    output-channel="createAnswerChannel"
    method="aggregate">
    <bean class="MyAggregator" />
</int:aggregator>

<int:recipient-list-router id="myErrorRouter" input-channel="errorChannel">
    <int:recipient channel="filter_ErrorType_1"/> 
    <int:recipient channel="filter_ErrorType_2"/> 
    <int:recipient channel="filter_ErrorType_3"/> 
</int:recipient-list-router>

<int:filter input-channel="filter_ErrorType_1" output-channel="aggregateChannel" method="accept"></int:filter>
<int:filter input-channel="filter_ErrorType_2" output-channel="createErrorAnswerChannel" method="accept"></int:filter>
<int:filter input-channel="filter_ErrorType_3" output-channel="createErrorAnswerChannel" method="accept"></int:filter>

<int:service-activator input-channel='createErrorAnswerChannel' output-channel="replyChannel" method='buildError'>
    <bean class="AnswerBuilder"/>
</int:service-activator>

<int:service-activator input-channel='createAnswerChannel' output-channel="replyChannel" method='build'>
    <bean class="AnswerBuilder"/>
</int:service-activator>

Follow up:

<int:gateway id="myGateway" service-interface="someGateway" default-request-channel="splitChannel" error-channel="errorChannel"  default-reply-channel="replyChannel" async-executor="MyThreadPoolTaskExecutor"/>

<int:splitter input-channel="splitChannel" output-channel="transformChannel" method="split">
    <bean class="Splitter" />
</int:splitter>

<int:transformer id="transformer1" input-channel="toTransformer1" method="transform" output-channel="toTransformer2">
    <bean class="Transformer1"/>  // this may throw the validation error (filter_ErrorType_1), if it cannot transform
</int:transformer>

<int:transformer id="transformer2" input-channel="toTransformer2" method="transform" output-channel="toTransformer3">
    <bean class="Transformer2"/>  // this may throw the validation error (filter_ErrorType_2), if it cannot transform
</int:transformer>

<int:transformer id="transformer3" input-channel="toTransformer3" method="transform" output-channel="aggregateChannel">
    <bean class="Transformer3"/>  // this may throw the validation error (filter_ErrorType_3), if it cannot transform
</int:transformer>

???
// seems like you are proposing to have one gateway for each endpoint that may throw error.
// but in this case, take transfomer 1 for example, I cannot output the gateway directly to aggregate channel since for valid data it has to go to transformer 2
// but the failed message throwed by the error handler cannot pass transformer 2 because of afterall this is a error message not a valid data for transformer 2
// <int:service-activator input-channel="toTransformer1" output-channel="toTransformer2" ref="gateway1"/>

// <int:gateway id="gateway1" default-request-channel="toTransformer1" error-channel="errorChannel1"/>

// <int:transformer id="transformer" input-channel="toTransformer1" method="transform">
//     <bean class="Transformer"/>  // this may throw the validation error (filter_ErrorType_1), if it cannot transform
// </int:transformer>

// how to deal with this problem?




<int:service-activator input-channel='createErrorAnswerChannel' output-channel="replyChannel" method='buildError'>
    <bean class="AnswerBuilder"/>
</int:service-activator>

<int:service-activator input-channel='createAnswerChannel' output-channel="replyChannel" method='build'>
    <bean class="AnswerBuilder"/>
</int:service-activator>

I actually have complicated logic inside but I use transformer 123 to represent here.

1 个答案:

答案 0 :(得分:3)

Your question isn't clear. Please,be sure in the future provide more concrete info. Some config and StackTrace or logs are useful, too.

I guess that you have in the beginning of your flow <gateway> with configured error-channel. That's why you are receiving Reply message received but the receiving thread has already received a reply.

But I can't be sure, because there is no those words in your question. Right?

You can't rely on the errorChannel header there and come back for the reply eventually because the errorChannel header in case of Gateway is the same as replyChannel, and it is TemporaryReplyChannel - one-shot-usage channel and only for receive() operation.

Since we don't have any configuration or code from you we can't help you properly.

I suppose you need a middle-flow gateway via service-activator:

<service-activator id="gatewayTestService" input-channel="inputChannel" 
            output-channel="outputChannel" ref="gateway"/>

<gateway id="gateway" default-request-channel="requestChannel" error-channel="myErrorChannel"/>

or <chain> with <gateway> to perform validation and catch errors only there and return a desired reply for failures. That service-activator will be able to send them all to the <aggregator> afterward. In this case the <aggregator> can reply back to the gateway properly.

EDIT

  1. errorChannel is a name for default global bean to catch any error messages from any Integration place. See more info in the http://docs.spring.io/spring-integration/reference/html/configuration.html#namespace-errorhandler.

So, that name is fully bad for your use case , because that myErrorRouter is going to handle ALL the errors!

  1. The <int:recipient-list-router> sends message to all its recipients if it passes a selector or if there is no selector.

  2. You don't need default-reply-channel on the <gateway> if it isn't publish-subscribe. You can just rely on the replyChannel header, which works if there is no output-channel defined.

  3. What I'm talking about <service-activator> and <gateway> is pretty straight forward in front of your <transformer> after <splitter>:

    <int:service-activator input-channel="transformChannel" 
                output-channel="aggregateChannel" ref="gateway"/>
    
    <int:gateway id="gateway" default-request-channel="transformChannel" error-channel="validationErrorChannel"/>
    
    <int:transformer id="transformer" input-channel="transformChannel" method="transform">
        <bean class="Transformer"/>  // this may throw the validation error (filter_ErrorType_1), if it cannot transform
    </int:transformer>
    

So, splitter sends items to the service-activator. That one proceeds with the message to the gateway around transformer with custom error-channel exactly for one item. The transformer answers to the replyChannel exactly to that previous gateway. If it throws some Exception, it is handled by the validationErrorChannel process. Which should reply with some compensation message. That message is bubbled to the service-activator. Finally service-activator sends a result to the aggregateChannel. And it is black box for the service-activator if validation was good or not.

Hope that helps a bit.

EDIT2

I'm disappointed that you haven't accepted my advises in your code, but nevertheless that is how I see it:

<int:gateway id="myGateway" service-interface="someGateway" default-request-channel="splitChannel"
             async-executor="MyThreadPoolTaskExecutor" />

<int:splitter input-channel="splitChannel" output-channel="transformChannel" method="split">
    <bean class="Splitter" />
</int:splitter>

<int:service-activator input-channel="validateChannel" output-channel="aggregateChannel"
      ref="validateGateway"/>

<gateway id="validateGateway" default-request-channel="toTransformer1" error-channel="myErrorChannel"/>

<chain input-channel="toTransformer1">
    <int:transformer method="transform">
        <bean class="Transformer1" />
    </int:transformer>
    <int:transformer method="transform">
        <bean class="Transformer2" />
    </int:transformer>
    <int:transformer method="transform">
        <bean class="Transformer3" />
    </int:transformer>
</chain>


<int:service-activator input-channel="myErrorChannel" method="buildError">
    <bean class="AnswerBuilder" />
</int:service-activator>

<int:aggregator id="aggregator"
                input-channel="aggregateChannel"
                output-channel="createAnswerChannel"
                method="aggregate">
    <bean class="MyAggregator" />
</int:aggregator>

<int:service-activator input-channel='createAnswerChannel' method='build'>
    <bean class="AnswerBuilder" />
</int:service-activator>

Pay attention, how I chain transformers. So, you have one gateway for all your transformers and any error on any of them will be thrown to the gateway for error handling on the myErrorChannel.