Sample code for 30+ languages & platforms
CkJava Requires Chilkat v11.6.0+

Azure Table: Retry a Read with Bounded Backoff

See more Azure Table Service Examples

Retries only this GET for selected transient HTTP statuses, at most five attempts. Uses exponential backoff plus jitter and honors numeric or HTTP-date Retry-After without shortening a requested wait. Stops when the server asks for more than 30 seconds, on invalid Retry-After, authentication failures or transport errors. Each connection/read also has a timeout. This is not a generic write-retry policy; uncertain mutation outcomes require reconciliation.

Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.

HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.

Microsoft reference.

Chilkat CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  StringBuilder compares text by value in every generated language.
    CkStringBuilder textCompare = new CkStringBuilder();
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkJsonObject config = new CkJsonObject();
    config.put_Strict(true);
    success = config.LoadFile("qa_data/azure/storage.json");
    if (success != true) {
        System.out.println("Could not load private Azure configuration.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("account"));
    boolean sameText1 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
        System.out.println("Supply the storage account name.");
        return;
        }

    //  access_key is the base64 account key, not a connection string or SAS.
    textCompare.Clear();
    textCompare.SetString(config.stringOf("access_key"));
    boolean sameText2 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
        System.out.println("Supply the base64 account access key.");
        return;
        }

    CkStringBuilder accountText = new CkStringBuilder();
    success = accountText.Append(config.stringOf("account"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
        System.out.println("Storage account names need 3-24 lowercase letters or digits.");
        return;
        }

    CkStringBuilder allowedAccountChars = new CkStringBuilder();
    success = allowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    int charIndex = 0;
    while ((charIndex < accountText.get_Length())) {
        if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
            System.out.println("Storage account names need lowercase letters or digits.");
            return;
            }

        charIndex = charIndex+1;
        }

    CkStringBuilder endpoint = new CkStringBuilder();
    success = endpoint.Append(config.stringOf("account"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = endpoint.Append(".table.core.windows.net");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    //  Table names contain only letters and digits and are case-insensitive.
    //  Entity PartitionKey and RowKey values are case-sensitive.
    String resourcePath = "/ExampleCustomers()";
    CkRest rest = new CkRest();
    rest.put_ConnectTimeoutMs(20000);
    rest.put_IdleTimeoutMs(60000);
    CkAuthAzureStorage azAuth = new CkAuthAzureStorage();
    azAuth.put_Account(config.stringOf("account"));
    azAuth.put_AccessKey(config.stringOf("access_key"));
    azAuth.put_Service("Table");
    azAuth.put_Scheme("SharedKey");
    azAuth.put_XMsVersion("2026-04-06");
    success = rest.SetAuthAzureStorage(azAuth);
    if (success != true) {
        System.out.println("Could not configure Shared Key signing.");
        return;
        }

    CkSocket connection = new CkSocket();
    connection.put_RequireSslCertVerify(true);
    connection.put_MaxReadIdleMs(60000);
    connection.put_MaxSendIdleMs(60000);
    success = connection.Connect(endpoint.getAsString(),443,true,20000);
    if (success != true) {
        System.out.println("TLS connection failed; inspect diagnostics privately.");
        return;
        }

    //  Do not replay a write automatically after losing its response.
    success = rest.UseConnection(connection,false);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    String responseText = "";
    CkJsonObject result = new CkJsonObject();
    result.put_Strict(true);
    success = rest.AddHeader("DataServiceVersion","3.0;NetFx");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("MaxDataServiceVersion","3.0;NetFx");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("Content-Type","application/json");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("Accept","application/json;odata=minimalmetadata");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    String partitionKey = "customers-west";
    String rowKey = "customer-001";
    CkStringBuilder encodedPk = new CkStringBuilder();
    success = encodedPk.Append(partitionKey);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    int escapedencodedPk = encodedPk.Replace("'","''");
    success = encodedPk.Encode("url_rfc3986","utf-8");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    CkStringBuilder encodedRk = new CkStringBuilder();
    success = encodedRk.Append(rowKey);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    int escapedencodedRk = encodedRk.Replace("'","''");
    success = encodedRk.Encode("url_rfc3986","utf-8");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    //  Escape apostrophes for OData first, then percent-encode each key once.
    CkStringBuilder entityPath = new CkStringBuilder();
    success = entityPath.Append("/ExampleCustomers(PartitionKey='");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append(encodedPk.getAsString());
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append("',RowKey='");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append(encodedRk.getAsString());
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append("')");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    resourcePath = entityPath.getAsString();
    int attempt = 0;
    int baseDelayMs = 1000;
    boolean finished = false;
    CkHttp timer = new CkHttp();
    CkPrng jitter = new CkPrng();
    CkStringBuilder retryText = new CkStringBuilder();
    CkStringBuilder digits = new CkStringBuilder();
    success = digits.Append("0123456789");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    CkDateTime retryDate = new CkDateTime();
    CkDateTime currentTime = new CkDateTime();
    while ((finished != true)) {
        attempt = attempt+1;
        responseText = rest.fullRequestNoBody("GET",resourcePath);
        if (rest.get_LastMethodSuccess() != true) {
            System.out.println("Transport failure; this example does not retry incomplete reads.");
            return;
            }

        int httpStatus = rest.get_ResponseStatusCode();
        if (httpStatus == 200) {
            finished = true;
            }
        else {
            if ((httpStatus != 408) && (httpStatus != 429) && (httpStatus != 500) && (httpStatus != 502) && (httpStatus != 503) && (httpStatus != 504)) {
                System.out.println("Non-retryable HTTP status; inspect request ID privately.");
                return;
                }

            if (attempt >= 5) {
                System.out.println("Retry limit reached.");
                return;
                }

            int delayMs = baseDelayMs+jitter.RandomInt(0,250);
            if (jitter.get_LastMethodSuccess() != true) {
                System.out.println("Could not generate retry jitter.");
                return;
                }

            success = retryText.SetString(rest.responseHdrByName("Retry-After"));
            if (success != true) {
                System.out.println("Could not prepare the request.");
                return;
                }

            success = retryText.Trim();
            if (success != true) {
                System.out.println("Could not prepare the request.");
                return;
                }

            if (retryText.get_Length() > 0) {
                boolean numericDelay = true;
                int digitIndex = 0;
                while ((digitIndex < retryText.get_Length())) {
                    if (digits.Contains(retryText.getRange(digitIndex,1,false),true) != true) {
                        numericDelay = false;
                        }

                    digitIndex = digitIndex+1;
                    }

                int requestedSeconds = 0;
                if (numericDelay == true) {
                    if (retryText.get_Length() > 2) {
                        System.out.println("Retry-After exceeds this example wait budget.");
                        return;
                        }

                    requestedSeconds = retryText.get_IntValue();
                    }
                else {
                    success = retryDate.SetFromRfc822(retryText.getAsString());
                    if (success != true) {
                        System.out.println("Invalid Retry-After date.");
                        return;
                        }

                    success = currentTime.SetFromCurrentSystemTime();
                    if (success != true) {
                        System.out.println("Could not prepare the request.");
                        return;
                        }

                    requestedSeconds = retryDate.DiffSeconds(currentTime)+1;
                    }

                if (requestedSeconds > 30) {
                    System.out.println("Server requested a longer wait; stop instead of retrying too soon.");
                    return;
                    }

                if (requestedSeconds < 0) {
                    requestedSeconds = 0;
                    }

                if (requestedSeconds * 1000 > delayMs) {
                    delayMs = requestedSeconds * 1000;
                    }

                }

            //  Retry only this GET; never copy this loop blindly to mutation requests.
            timer.SleepMs(delayMs);
            baseDelayMs = baseDelayMs * 2;
            rest.Disconnect(10);
            success = connection.Connect(endpoint.getAsString(),443,true,20000);
            if (success != true) {
                System.out.println("TLS reconnection failed.");
                return;
                }

            success = rest.UseConnection(connection,false);
            if (success != true) {
                System.out.println("Could not prepare the request.");
                return;
                }

            }

        }

    success = result.Load(responseText);
    if (success != true) {
        System.out.println("Expected well-formed JSON.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("PartitionKey"));
    boolean sameText3 = textCompare.ContentsEqual("",true);
    if ((result.JsonTypeOf("PartitionKey") != 1) || (sameText3 == true)) {
        System.out.println("Missing string PartitionKey.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("RowKey"));
    boolean sameText4 = textCompare.ContentsEqual("",true);
    if ((result.JsonTypeOf("RowKey") != 1) || (sameText4 == true)) {
        System.out.println("Missing string RowKey.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("PartitionKey"));
    boolean sameText5 = textCompare.ContentsEqual(partitionKey,true);
    textCompare.Clear();
    textCompare.SetString(result.stringOf("RowKey"));
    boolean sameText6 = textCompare.ContentsEqual(rowKey,true);
    if ((sameText5 != true) || (sameText6 != true)) {
        System.out.println("Returned entity keys do not match.");
        return;
        }

    System.out.println("Entity read completed with bounded retries.");
  }
}