{"id":15501788,"url":"https://github.com/alexpanov/retries","last_synced_at":"2025-06-25T08:39:38.463Z","repository":{"id":36227774,"uuid":"40532082","full_name":"alexpanov/retries","owner":"alexpanov","description":"Forget about your retry boilerplate","archived":false,"fork":false,"pushed_at":"2017-07-07T21:28:02.000Z","size":62,"stargazers_count":13,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T23:06:32.904Z","etag":null,"topics":["failure","retries","retry","wait"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexpanov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-11T09:01:37.000Z","updated_at":"2024-07-03T23:30:20.000Z","dependencies_parsed_at":"2022-08-30T11:30:18.735Z","dependency_job_id":null,"html_url":"https://github.com/alexpanov/retries","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/alexpanov/retries","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpanov%2Fretries","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpanov%2Fretries/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpanov%2Fretries/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpanov%2Fretries/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexpanov","download_url":"https://codeload.github.com/alexpanov/retries/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpanov%2Fretries/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261837946,"owners_count":23217542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["failure","retries","retry","wait"],"created_at":"2024-10-02T09:05:43.854Z","updated_at":"2025-06-25T08:39:38.440Z","avatar_url":"https://github.com/alexpanov.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/alexpanov/retries.svg)](https://travis-ci.org/alexpanov/retries)\n[![Windows Build Status](https://ci.appveyor.com/api/projects/status/c7dnnthq4ksq3960/branch/master?svg=true)](https://ci.appveyor.com/project/alexpanov/retries/branch/master)\n[![Coverage](https://coveralls.io/repos/alexpanov/retries/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/alexpanov/retries?branch=master)\n\n# Retries\nYou need to make a call that may fail. You need to wait for a value that meets your criteria. You write your own retry boilerplate. **Why**?\nThrow it away. You need not to support it anymore.\n\n## Motivation\nHave you ever written a retry/wait code that looks like this?\n```java\nprivate String computeWithRetries() {\n    int numberOfTries = 10;\n    int sleepTimeout = 1000; // one sec\n    for (int i = 0; i \u003c numberOfTries; i++) {\n        try {\n            String value = computeValue();\n        } catch (Exception e) {\n            try {\n                Thread.sleep(sleepTimeout);\n            } catch (InterruptedException ie) {\n                ie.printStackTrace();\n            }\n            continue;\n        }\n        if (value != null \u0026\u0026 !value.startsWith(\"B\")) {\n            return value;\n        }\n    }\n    throw new RuntimeException(\"Could not compute anything\");\n}\n```\n**Ugh**. That's terrible and you know it. It's hard to figure out what's going on, hard to extend, hard to reuse and easy to get it wrong. Luckily, **there is an alternative**.\n\n## Build tools\nAdd **retries** to your dependencies. Note that *Guava is automatically added too*.\n### Maven:\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eme.alexpanov\u003c/groupId\u003e\n    \u003cartifactId\u003eretries\u003c/artifactId\u003e\n    \u003cversion\u003e0.0.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n### Gradle:\n```groovy\ncompile \"me.alexpanov:retries:0.0.3\"\n```\n\n## Getting started\nCreate a retryable:\n```java\n//Nasty call that returns so much nulls\nRetryable\u003cString\u003e retryable = new Retryable\u003cString\u003e() {\n            @Override\n            public String tryOnce() throws Exception {\n                return null;\n            }\n        };\n```\n### One call, one failure, ```RetryException``` is thrown\n```java\nString resultAfterRetries = new Retries\u003cString\u003e(retryable).stopOnMaxFailures(1).perform();\n```\nCode above will throw a subclass of ```RetryException```:\n```\nme.alexpanov.retries.FailedToComputeAValueException\n```\n\n### Same thing asynchronously, ```java.util.concurrent.ExecutionException``` is thrown\n```java\nFuture\u003cString\u003e resultAfterRetries = new Retries\u003cString\u003e(retryable).stopOnMaxFailures(1).performAsync();\nString result = resultAfterRetries.get();\n```\nCode above will throw an ```ExecutionException``` with a subclass of ```RetryException``` as a cause:\n```\nme.alexpanov.retries.FailedToComputeAValueException\n```\n\n### One call, one failure, default value is returned\n```java\nString resultAfterRetries = new Retries\u003cString\u003e(retryable).stopOnMaxFailures(1)\n                                                          .orElse(\"default value\")\n                                                          .perform();\nresultAfterRetries.equals(\"default value\"); //true\n```\n\n### Several calls, configured wait timeout\n```java\nString resultAfterRetries = new Retries\u003cString\u003e(retryable).stopOnMaxFailures(10)\n                                                          .waitAfterFailureAtLeast(10, TimeUnit.SECONDS)\n                                                          .orElse(\"default value\")\n                                                          .perform();\n```\n\n### Ignoring results\n```java\nPredicate\u003cString\u003e startsWithLetterB = new Predicate\u003cString\u003e() {\n            @Override\n            public boolean apply(String input) {\n                return input.startsWith(\"B\");\n            }\n        };\nString resultAfterRetries = new Retries\u003cString\u003e(retryable).stopOnMaxFailures(2)\n                                                          .ignoreIfResult(startsWithLetterB)\n                                                          .perform();\n```\n\n### Subscribing to failures for logging etc.\n```java\nFailureSubscriber\u003cString\u003e logTheError = new FailureSubscriber\u003cString\u003e() {\n            @Override\n            public void onFailure(RetryFailure\u003cString\u003e details) {\n                LOG.info(\"Failure\");\n            }\n        };\nString resultAfterRetries = new Retries\u003cString\u003e(retryable).stopOnMaxFailures(10)\n                                                          .onEachFailureDo(logTheError)\n                                                          .perform();\n```\nThe code above will append a log message each time a call failed (exception, null or skipped result).\n                    \n## But what about my Callable(s)!?\nI got you covered, man.\n```java\nRetryable\u003cString\u003e retryable = new CallableToRetryable\u003cString\u003e(yourCallable);\n```\n\n## Licence\n[The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)\n\n## Issues\nPlease feel free to add any issues regarding new functionality, improvements etc. in the [Issues section](https://github.com/alexpanov/retries/issues)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpanov%2Fretries","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexpanov%2Fretries","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpanov%2Fretries/lists"}