Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java & Spring - polling the http endpoint until the server finishes processing

I'm struggling with picking up the right way to poll server with constant interval (eg ~1 second).

The flow goes as follows

  • client application receives message, that indicates the polling could start with provided parameters (it doesn't poll when there is no need to)
  • client application starts polling the http endpoint every ~1second with parameters arrived with message (like query parameter)
  • server application responds with status pending so that indicates the client should continue polling
  • server application responds with status finished and returns the result - there is no need to keep polling.

We can have multiple threads, as the client application might receive multiple message in the short time - polling should start immediately

I don't want to reinvent the wheel, maybe there is a proper tool that works with java/spring that I can use?

Key features

  • poll only when there is a need to
  • poll with custom parameters (custom params in a query string)
  • scale polling as the application could poll multiple endpoints simultaneously with the same interval

I was going through various libs like Apache Camel or Spring Integration PollableChannel, but I feel like none of these is going to give me the right solution out of the box.

If there is no lib like this - I'm going to write it on my own using redis and simple loop, but maybe someone has faced similar problem.

like image 206
hopsey Avatar asked Sep 13 '25 13:09

hopsey


1 Answers

If I understand your architecture correctly, the point is to call the same HTTP endpoint from the client application until expected result. In this case I would suggest something like RequestHandlerRetryAdvice with an AlwaysRetryPolicy and a FixedBackOffPolicy (1 second by default).

To simulate an exception I would suggest an ExpressionEvaluatingRequestHandlerAdvice with the propagateOnSuccessEvaluationFailures = true option to re-throw an exception from the onSuccessExpression when reply from the server is pending.

Both of these advises (in the exact RequestHandlerRetryAdvice, ExpressionEvaluatingRequestHandlerAdvice) you need to apply to @ServiceActivator for the HttpRequestExecutingMessageHandler.

See more info in the Reference Manual: https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#message-handler-advice-chain

like image 120
Artem Bilan Avatar answered Sep 16 '25 04:09

Artem Bilan