{"id":18573727,"url":"https://github.com/lowzj/java-retrying","last_synced_at":"2025-04-10T07:32:28.181Z","repository":{"id":57721149,"uuid":"121336090","full_name":"lowzj/java-retrying","owner":"lowzj","description":"java retry module, based on guava-retrying, support sync/async retry","archived":false,"fork":false,"pushed_at":"2018-06-02T00:38:25.000Z","size":51,"stargazers_count":22,"open_issues_count":1,"forks_count":13,"subscribers_count":3,"default_branch":"master","last_synced_at":"2023-07-26T21:59:57.916Z","etag":null,"topics":["async-retry","java","java-retry","retry"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lowzj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-13T03:53:46.000Z","updated_at":"2023-05-26T13:34:14.000Z","dependencies_parsed_at":"2022-08-26T20:12:06.127Z","dependency_job_id":null,"html_url":"https://github.com/lowzj/java-retrying","commit_stats":null,"previous_names":[],"tags_count":3,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lowzj%2Fjava-retrying","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lowzj%2Fjava-retrying/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lowzj%2Fjava-retrying/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lowzj%2Fjava-retrying/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lowzj","download_url":"https://codeload.github.com/lowzj/java-retrying/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223430251,"owners_count":17143625,"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":["async-retry","java","java-retry","retry"],"created_at":"2024-11-06T23:12:07.668Z","updated_at":"2024-11-06T23:12:08.300Z","avatar_url":"https://github.com/lowzj.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# java-retrying\n\n[![License](https://img.shields.io/badge/License-Apache%202-brightgreen.svg)](https://github.com/lowzj/java-retrying/blob/master/LICENSE)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Flowzj%2Fjava-retrying.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Flowzj%2Fjava-retrying?ref=badge_shield)\n[![Build Status](https://travis-ci.org/lowzj/java-retrying.svg?branch=master)](https://travis-ci.org/lowzj/java-retrying)\n[![codecov](https://codecov.io/gh/lowzj/java-retrying/branch/master/graph/badge.svg)](https://codecov.io/gh/lowzj/java-retrying)\n\njava重试, 支持同步/异步, 简单灵活可配, 不依赖第三方库.\n\n基于[guava-retrying](https://github.com/rholder/guava-retrying)改造, 增加了异步重试, 同时去掉了第三方依赖, 使用方法基本一致.\n\n名称 | JDK | 第三方依赖 | 同步重试 | 异步重试\n---- | --- | ---- | -------- | --------\nguava-retrying | 大于等于6 | guava,findbugs | Y | N\njava-retrying | 大于等于8 | 无 | Y | Y\n\n## Quickstart\n\n* 依赖\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.lowzj\u003c/groupId\u003e\n    \u003cartifactId\u003ejava-retrying\u003c/artifactId\u003e\n    \u003cversion\u003e1.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n* 同步重试\n```java\nRetryer\u003cInteger\u003e retryer = RetryerBuilder.\u003cInteger\u003enewBuilder()\n    .withWaitStrategy(WaitStrategies.fixedWait(100L, TimeUnit.MILLISECONDS))\n    .retryIfResult(num -\u003e num != 5)\n    .retryIfExceptionOfType(RuntimeException.class)\n    .withStopStrategy(StopStrategies.stopAfterAttempt(7))\n    .build();\n\ntry {\n    retryer.call(noRuntimeExceptionAfter(4));\n} catch (ExecutionException | RetryException e) {\n    e.printStackTrace();\n}\n```\n\n* 异步重试\n```java\nAsyncRetryer\u003cInteger\u003e asyncRetryer = RetryerBuilder.\u003cInteger\u003enewBuilder()\n    .withWaitStrategy(WaitStrategies.fixedWait(100L, TimeUnit.MILLISECONDS))\n    .retryIfResult(num -\u003e num != 4)\n    .retryIfExceptionOfType(RuntimeException.class)\n    .withStopStrategy(StopStrategies.stopAfterAttempt(7))\n    .withExecutor(ExecutorsUtil.scheduledExecutorService(\"example\", 1))\n    .buildAsyncRetryer();\n\nCompletableFuture\u003cInteger\u003e future = asyncRetryer.call(noRuntimeExceptionAfter(3));\n\n// get the result asynchronously\nfuture.whenComplete((result, error) -\u003e System.out.println(result));\n\n// or get the result synchronously\ntry {\n    System.out.println(future.get());\n} catch (InterruptedException | ExecutionException e) {\n    e.printStackTrace();\n}\n```\n\n其中函数`noRuntimeExceptionAfter`如下:\n```java\nprivate Callable\u003cInteger\u003e noRuntimeExceptionAfter(final int attemptNumber) {\n    return new Callable\u003cInteger\u003e() {\n        private int count = 0;\n        @Override\n        public Integer call() throws Exception {\n            if (count++ \u003c attemptNumber) {\n                throw new RuntimeException(\"count[\" + (count - 1) + \"] \u003c attemptNumber[\" + attemptNumber + \"]\");\n            }\n            return count;\n        }\n    };\n}\n```\n\n## LICENSE\n\nJava-retrying is licensed under the Apache License, Version 2.0. See [LICENSE](https://github.com/lowzj/java-retrying/blob/master/LICENSE) for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flowzj%2Fjava-retrying","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flowzj%2Fjava-retrying","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flowzj%2Fjava-retrying/lists"}