3

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.

hopsey
  • 1,383
  • 1
  • 13
  • 23

1 Answers1

2

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

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Yes, the architecture is designed exactly as you understood. Is it going to support multiple pollers at the same time? I mean, when I receive event with id 1, I need to poll service.com/result-1.xml, If I receive event with id 2 I need to poll service.com/result-2.xml independently at the same time, until both satisfied. Thanks! – hopsey Jan 04 '19 at 18:27
  • 1
    That's correct. Both of them are going to work in their own caller's threads, therefore this is fine to have concurrent requests for different parameters. Only the problem with mentioned solution that callers are going to be blocked until the proper `finished` response. – Artem Bilan Jan 04 '19 at 18:48