Alex Rivera | Logout

Design pattern for "retrying" logic that failed?

Asked 2012-07-27T17:19:50.387
74

I'm writing some reconnect logic to periodically attempt to establish a connection to a remote endpoint which went down. Essentially, the code looks like this:

public void establishConnection() {
    try {
        this.connection = newConnection();
    } catch (IOException e) {
        // connection failed, try again.
        try { Thread.sleep(1000); } catch (InterruptedException e) {};

        establishConnection();
    }
}

I've solved this general problem with code similar to the above on many occasions, but I feel largely unsatisfied with the result. Is there a design pattern designed for dealing with this issue?

Edit
Report

1 Answer

30

You could try the Idempotent Retry Pattern.

enter image description here

answered 2012-07-27T17:26:36.607

Your Answer