简体   繁体   中英

Can I Message Spring Integration Channel from within My Inbound Channel Adapter Handler?

I'm looking to message another channel, if certain conditions merit, from within the handler of my file inbound-channel-adapter. Sometimes the inbound file has out of specification values and I'd like to send the file to an error directory, but not interrupt the flow of the inbound to outbound since even the out of specification file still needs to goto a secondary location.

Example of my configuration:

<int-file:inbound-channel-adapter id="filesIn"
                                  directory="${input.path}"
                                  filename-regex="\\d{8}-\\d+\\.txt">
    <int:poller id="poller" fixed-rate="500"/>
</int-file:inbound-channel-adapter>

<int-file:file-to-string-transformer input-channel="filesIn"
                                     output-channel="strings"
                                     delete-files="true" />

<int:channel id="strings"/>
<int:channel id="errorChannel" />

<int:exception-type-router input-channel="strings"
                           default-output-channel="output">
    <int:mapping exception-type="ca.uhn.hl7v2.HL7Exception"
                 channel="errorChannel"/>
</int:exception-type-router>

<int:service-activator input-channel="output"
                               output-channel="archive"
                               ref="handler" method="handleString"/>

<int:service-activator input-channel="errorChannel"
                       output-channel="errorOutput"
                       ref="handler" method="handleError" />

<int-file:outbound-channel-adapter id="archive"
                                   directory="${archive.path}"
                                   delete-source-files="true"/>

<int-file:outbound-channel-adapter id="errorOutput"
                                   directory="${hl7.error.path}"/>

<bean id="handler" class="com.giotta.service.Hl7MessageConsumer"/>

粗图

From a terminology perspective, inbound adapters don't have "handlers". Depending on the type, they are either message sources or message producers.

That said, you can add a router immediately after the inbound adapter and route to different channels based on whatever criteria you want.

EDIT

The exception is thrown within the Hl7MessageConsumer while trying to parse the String into a HL7 message

But you currently have

...>transformer->etr->handler->...

ie the etr is before the handler.

You need something like...

<int-file:inbound-channel-adapter id="filesIn"
                                  directory="${input.path}"
                                  filename-regex="\\d{8}-\\d+\\.txt">
    <int:poller id="poller" fixed-rate="500" error-channel="errorChannel" />
</int-file:inbound-channel-adapter>

<int-file:file-to-string-transformer input-channel="filesIn"
                                     output-channel="strings"
                                     delete-files="true" />

<int:channel id="strings"/>

<int:service-activator input-channel="strings"
                               output-channel="archive"
                               ref="handler" method="handleString"/>

<int-file:outbound-channel-adapter id="archive"
                                   directory="${archive.path}"
                                   delete-source-files="true"/>

<int:channel id="errorChannel" />

<int:service-activator input-channel="errorChannel"
                       output-channel="errorOutput"
                       ref="handler" method="handleError" />

<int-file:outbound-channel-adapter id="errorOutput"
                                   directory="${hl7.error.path}"/>

<bean id="handler" class="com.giotta.service.Hl7MessageConsumer"/>

ie the poller catches the exception and routes an ErrorMessage to the error channel. The error message payload is a MessagingException with two properties ( cause and failedMessage ).

EDIT2

I still want the string to goto the "archive" adapter in addition to the "errorOutput" adapter.

In that case...

<int:publish-subscribe-channel id="errorChannel" />

<int:service-activator input-channel="errorChannel"
                       output-channel="errorOutput"
                       ref="handler" method="handleError" />

<int:transformer input-channel="errorChannel"
            output-channel="archive" expression="payload.failedMessage" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM