Search code examples
javaspring-amqpmessage-listener

Spring AMQP take action on message timeout


I am using Spring AMQP with asynchronous messaging. My model assumes that there are two applications A and B, both producers and consumers.

  • A sends job request to B and starts listening.
  • B are listening for job request, and when it comes, starts job and periodically sends progress messages to A.
  • B sends job finish message to A after job is finished.
  • A consumes progress messages until job finish message comes, then A exists.

I am using @RabbitListener on class level and @RabbitHandler on method level, for message consuming. Everything works nice and design is clean, I like Spring's solution.

My problem is - I have no idea how to detect, and how to act, when A is expecting to receive progress message from B (any message) and it's not coming in. Is there any timeout for such cases? If so, how to implement callback method?

I found some timeout settings, but they usually works for connection itself, or only when using RPC pattern (one request one response).

Desired solution is - A should receive progress message every minute. If no progress message is consumed for, say, 3 minutes, I want to cancel the job.


Solution

  • When using async consumers, there's no mechanism to generate an event if a message is not received within some time period.

    You can schedule your own task though and cancel/reschedule the task when a message arrives.

    Use a TaskScheduler with

    future = schedule(myRunnable, new Date(System.currentTimeMillis() + 180000));

    use future.cancel() when a message arrives.