Question

I have a single threaded message listener that listens for the incoming messages.The received messages are persisted in a database as they are recieved.

There is a message A and associated message B follows it with a reference it.In Some Odd occurences, B arrives before A. Now in this case, There has to be 3retries after some 'x' equal intervals to see if A has arrived and then persists the association.

As the message listener is single threaded if we put the thread to sleep the whole system would be affected. So there has to be a separate thread retrying.

Can we use Quartz Job Scheduler for this purpose to avoid handling multiThreading issues and to have a PERSISTENT STORE of the in any of the following 2 ways,

  1. Schedule a Job in Quartz for 3 times and keep track of a flag in the JobDataMap to check if previous retries succeeds then return without doing anything

OR

2.Schedule a Job to retry once and then if the retry fails schedule the same job after a few seconds.

Can quartz be used only for repetitive jobs and not have some state information span across job or Is there any other better way to do this.

Was it helpful?

Solution

You should configure your JMS provider to set the redelivery delay on your message queue. In your code, you call context.setRollbackOnly to abort a message that does not pass prerequisites check.

In that case, the code execution scenario becomes:

  • consume "B", check for prerequisite and detect A is lacking
  • roll back the transaction and the message returns to the queue, it will be re-delivered after the configured delay
  • consume and process next message "A"
  • after the delay, the MDB consumes and processes again "B" with success
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top