04 November 2006

Java Retry Framework

One developer on my team this week needed to implement retry logic on a section of code so that it would retry after a deadlock. We spent some time discussing the options:
  • Add a "retry count" to the object (which implements Runnable). Increment the counter on failure and if the counter is less than N, then place the Runnable back in the queue. After N retries, log a failure and give up.
  • Set up a separate thread, and upon failure, queue of the object to run again after a period of time. Continue to retry every M seconds up to N times, then give up.
  • Similar to above; initially sleep for a long period of time (an hour), then try N times over M minutes before giving up.
  • Increase the wait interval, M, betwen retries; keep trying until the interval reaches a defined maximum.
  • ..etc..
Since the first option was the easiest and effectively accomplished what we wanted, it was selected. A discussion on how we have retry logic throughout our system led to a hunt to see if someone had written a Retry Framework that we could leverage.

As far as I could determine, there isn't one. Hasn't everyone at one point had to write some sort of retry logic? Why isn't there a framework of some sort for that?