antacerod
antacerod

Reputation: 1420

Spring Integration flow (Outbound and inbound)

I have a situation I don't know how to manage it.

The flow I neeed is the following one:

  1. The first service makes its job and creates a message that needs to be retrieved in the chain's end.

  2. When the first service finishes, I need to invoke a push notification server via a new service with a particular message but with some info related to the one created in step 1.

  3. Finally, I the push notification has been sent successfully, I have to retrieve the message created in step 1.

The question is, how can I keep message created in step 1 when the outbound-gateway calling was produced and retrieved me the message from notification push server?

<int:chain input-channel="v1.inputChannel.input" output-channel="v1.inputChannel.output" send-timeout="50000">

    <int:header-enricher>
        <int:error-channel ref="v1.inputChannel.error" />
    </int:header-enricher>

    <int:service-activator ref="v1.input.service" method="methodName"/>

    <int:service-activator ref="v1.notificationPusher.service" method="pushNotification"/>

    <int-http:outbound-gateway url="http://example.com/api/elements/:element_id/objects" http-method="POST">
        <int-http:uri-variable name="element_id" expression="#pathVariables.elementId"/>
    </int-http:outbound-gateway>
    <!-- here the transformer needs to get the messsage from v1.input.service -->
    <int:object-to-json-transformer/>

</int:chain>

Upvotes: 1

Views: 4711

Answers (3)

Diego Urenia
Diego Urenia

Reputation: 1630

I think you could achieve this with something like that:

  1. Dupĺicate your message before send it to the outbound
  2. Send one to the outbound and the another one to some channel
  3. Use an Aggregator with the same timeout of your outbound to "join" them, but you could, in fact, only pass the message from the first step through (I haven't tested it)
  4. Send it to the json transformer

To use this approach, I believe, you have to put the outbound and the json-transformer outside the chain as well as your logic to duplicate the message.

Upvotes: 3

antacerod
antacerod

Reputation: 1420

After some researching in 'Spring Integration in Action' I think the best option for resolving this situation is to use the wire tap pattern, having the push notification service as secondary flow.

Book's example

Here you can see the book's example where, If I have understood it in a correct way, the auditChannel acts as a secondary flow separated from the main one.

<channel id="debitChannel">
    <interceptors>
    <wire-tap channel="auditChannel"/>
    </interceptors>
</channel>

<service-activator input-channel="debitChannel" method="process">
    <beans:bean class="siia.monitoring.wiretap.DebitService"/>
</service-activator>

<filter input-channel="auditChannel" expression="payload.amount > 10000" output-channel="logger"/>

<logging-channel-adapter id="logger" expression="'auditing debit: ' + payload"/>

Upvotes: 3

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

I can think of one way, which is to squirrel away the payload into a header after step 1, then retrieve and repopulate the payload right before you invoke step 3:

<int:service-activator ref="v1.input.service" method="methodName"/>
<int:header-enricher>
  <int:header name="originalpayload" expression="payload"/>
</int:header-enricher>

<int:service-activator ref="v1.notificationPusher.service" method="pushNotification"/>
<int:enricher expression="headers.originalpayload"/>

<int-http:outbound-gateway url="http://xxx.com/api/elements/:element_id/objects" http-method="POST">
    <int-http:uri-variable name="element_id" expression="#pathVariables.elementId"/>
</int-http:outbound-gateway>

Upvotes: 0

Related Questions