keycloak
クラス | 静的公開メンバ関数 | 静的非公開メンバ関数 | 全メンバ一覧
org.keycloak.common.util.Retry クラス
org.keycloak.common.util.Retry 連携図
Collaboration graph

クラス

interface  AdvancedRunnable
 
interface  Supplier
 

静的公開メンバ関数

static int execute (Runnable runnable, int attemptsCount, long intervalMillis)
 
static int executeWithBackoff (AdvancedRunnable runnable, int attemptsCount, int intervalBaseMillis)
 
static< T > T call (Supplier< T > supplier, int attemptsCount, long intervalMillis)
 

静的非公開メンバ関数

static int computeBackoffInterval (int base, int iteration)
 

詳解

著者
Stian Thorgersen

関数詳解

◆ call()

static <T> T org.keycloak.common.util.Retry.call ( Supplier< T >  supplier,
int  attemptsCount,
long  intervalMillis 
)
inlinestatic

Runs the given

runnable

at most

attemptsCount

times until it passes, leaving

intervalMillis

milliseconds between the invocations. The runnable is reexecuted if it throws a RuntimeException or AssertionError.

引数
supplier
attemptsCountTotal number of attempts to execute the
runnable
intervalMillis
戻り値
Value generated by the
supplier
.
119  {
120  int iteration = 0;
121  while (true) {
122  try {
123  return supplier.get(iteration);
124  } catch (RuntimeException | AssertionError e) {
125  attemptsCount--;
126  iteration++;
127  if (attemptsCount > 0) {
128  try {
129  if (intervalMillis > 0) {
130  Thread.sleep(intervalMillis);
131  }
132  } catch (InterruptedException ie) {
133  ie.addSuppressed(e);
134  throw new RuntimeException(ie);
135  }
136  } else {
137  throw e;
138  }
139  }
140  }
141  }

◆ computeBackoffInterval()

static int org.keycloak.common.util.Retry.computeBackoffInterval ( int  base,
int  iteration 
)
inlinestaticprivate
104  {
105  int iterationBase = base * (int)Math.pow(2, iteration);
106  return new Random().nextInt(iterationBase);
107  }

◆ execute()

static int org.keycloak.common.util.Retry.execute ( Runnable  runnable,
int  attemptsCount,
long  intervalMillis 
)
inlinestatic

Runs the given

runnable

at most

attemptsCount

times until it passes, leaving

intervalMillis

milliseconds between the invocations. The runnable is reexecuted if it throws a RuntimeException or AssertionError.

引数
runnable
attemptsCountTotal number of attempts to execute the
runnable
intervalMillis
戻り値
Index of the first successful invocation, starting from 0.
37  {
38  int iteration = 0;
39  while (true) {
40  try {
41  runnable.run();
42  return iteration;
43  } catch (RuntimeException | AssertionError e) {
44  attemptsCount--;
45  iteration++;
46  if (attemptsCount > 0) {
47  try {
48  if (intervalMillis > 0) {
49  Thread.sleep(intervalMillis);
50  }
51  } catch (InterruptedException ie) {
52  ie.addSuppressed(e);
53  throw new RuntimeException(ie);
54  }
55  } else {
56  throw e;
57  }
58  }
59  }
60  }

◆ executeWithBackoff()

static int org.keycloak.common.util.Retry.executeWithBackoff ( AdvancedRunnable  runnable,
int  attemptsCount,
int  intervalBaseMillis 
)
inlinestatic

Runs the given

runnable

at most

attemptsCount

times until it passes, leaving some increasing random delay milliseconds between the invocations. It uses Exponential backoff + jitter algorithm to compute the delay. More details https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/

The base for delay is specified by

intervalBaseMillis

number.

The runnable is reexecuted if it throws a RuntimeException or AssertionError.

引数
runnable
attemptsCountTotal number of attempts to execute the
runnable
intervalBaseMillisbase for the exponential backoff + jitter
戻り値
Index of the first successful invocation, starting from 0.
78  {
79  int iteration = 0;
80  while (true) {
81  try {
82  runnable.run(iteration);
83  return iteration;
84  } catch (RuntimeException | AssertionError e) {
85  attemptsCount--;
86  iteration++;
87  if (attemptsCount > 0) {
88  try {
89  if (intervalBaseMillis > 0) {
90  int delay = computeBackoffInterval(intervalBaseMillis, iteration);
91  Thread.sleep(delay);
92  }
93  } catch (InterruptedException ie) {
94  ie.addSuppressed(e);
95  throw new RuntimeException(ie);
96  }
97  } else {
98  throw e;
99  }
100  }
101  }
102  }
static int computeBackoffInterval(int base, int iteration)
Definition: Retry.java:104

このクラス詳解は次のファイルから抽出されました: