Nikhil
Nikhil

Reputation: 365

Spring Integration : Http Request -> Response -> Process -> If more data available -> Loop and Repeat until no data received

I have to query an http endpoint with offset and limit until no more data exist. For example,

POST 1 -> {"offset" : 0, "limit" : 50} : Returned 50 records -> continue to poll next offset

POST 2 -> {"offset" : 50, "limit" : 50} : Returned 25 records -> stop polling (25 < limit, so no more records)

How can I implement this loop with dynamic request body {"offset" : a, "limit" : b} using Service Activator and HTTP Outbound Gateway in spring-integration? I looked at Polling Consumer and Adding Behavior to endpoints section in the docs

One option is to add the loop logic in Service Activator and use a Gateway to bring the message into the channel.

Is there a better way of doing this including error handling?

Upvotes: 0

Views: 228

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

The loop could be done with the router component. So, you do some processing logic then forward message to the router to determine if you need to call an HTTP gateway again and just send a message with an appropriate offset in the payload or headers.

So, with pseudo code it may look like this:

send message with offset 0 -> httpInputChannel -> HTTP gateway -> process -> router.

The route function should decide to send a new offset back to the httpInputChannel or exit to the next channel.

See router component: https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#messaging-routing-chapter

The error handling could be done with the ExpressionEvaluatingRequestHandlerAdvice on that HTTP outbound gateway: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#expression-advice

Upvotes: 3

Related Questions