20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
22
23
+ /**
24
+ * This class implements the logic to execute <a
25
+ * href="https://en.wikipedia.org/wiki/Compensating_transaction">compensation operations</a> that is
26
+ * often required in Saga applications. The following is a skeleton to show of how it is supposed to
27
+ * be used in workflow code:
28
+ *
29
+ * <pre><code>
30
+ * Saga saga = new Saga(options);
31
+ * try {
32
+ * String r = activity.foo();
33
+ * saga.addCompensation(activity::cleanupFoo, arg2, r);
34
+ * Promise<String> r2 = Async.function(activity::bar);
35
+ * r2.thenApply(r->saga.addCompensation(activity.cleanupBar(r));
36
+ * ...
37
+ * useR2(r2.get());
38
+ * } catch (Exception e) {
39
+ * saga.compensate();
40
+ * // Other error handling if needed.
41
+ * }
42
+ * </code></pre>
43
+ */
23
44
public final class Saga {
24
45
private final Options options ;
25
46
private final List <Functions .Func <Promise >> compensationOps = new ArrayList <>();
@@ -119,8 +140,7 @@ public void compensate() {
119
140
}
120
141
121
142
/**
122
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
123
- * Saga#compensate()} is called.
143
+ * Add compensation operation for saga.
124
144
*
125
145
* @param operation to be executed during compensation.
126
146
*/
@@ -129,8 +149,7 @@ public void addCompensation(Functions.Proc operation) {
129
149
}
130
150
131
151
/**
132
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
133
- * Saga#compensate()} is called.
152
+ * Add compensation operation for saga.
134
153
*
135
154
* @param operation to be executed during compensation.
136
155
* @param arg1 first operation function parameter
@@ -140,8 +159,7 @@ public <A1> void addCompensation(Functions.Proc1<A1> operation, A1 arg1) {
140
159
}
141
160
142
161
/**
143
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
144
- * Saga#compensate()} is called.
162
+ * Add compensation operation for saga.
145
163
*
146
164
* @param operation to be executed during compensation.
147
165
* @param arg1 first operation function parameter
@@ -152,8 +170,7 @@ public <A1, A2> void addCompensation(Functions.Proc2<A1, A2> operation, A1 arg1,
152
170
}
153
171
154
172
/**
155
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
156
- * Saga#compensate()} is called.
173
+ * Add compensation operation for saga.
157
174
*
158
175
* @param operation to be executed during compensation.
159
176
* @param arg1 first operation function parameter
@@ -166,8 +183,7 @@ public <A1, A2, A3> void addCompensation(
166
183
}
167
184
168
185
/**
169
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
170
- * Saga#compensate()} is called.
186
+ * Add compensation operation for saga.
171
187
*
172
188
* @param operation to be executed during compensation.
173
189
* @param arg1 first operation function parameter
@@ -181,8 +197,7 @@ public <A1, A2, A3, A4> void addCompensation(
181
197
}
182
198
183
199
/**
184
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
185
- * Saga#compensate()} is called.
200
+ * Add compensation operation for saga.
186
201
*
187
202
* @param operation to be executed during compensation.
188
203
* @param arg1 first operation function parameter
@@ -197,8 +212,7 @@ public <A1, A2, A3, A4, A5> void addCompensation(
197
212
}
198
213
199
214
/**
200
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
201
- * Saga#compensate()} is called.
215
+ * Add compensation operation for saga.
202
216
*
203
217
* @param operation to be executed during compensation.
204
218
* @param arg1 first operation function parameter
@@ -220,8 +234,7 @@ public <A1, A2, A3, A4, A5, A6> void addCompensation(
220
234
}
221
235
222
236
/**
223
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
224
- * Saga#compensate()} is called.
237
+ * Add compensation operation for saga.
225
238
*
226
239
* @param operation to be executed during compensation.
227
240
*/
@@ -230,8 +243,7 @@ public void addCompensation(Functions.Func<?> operation) {
230
243
}
231
244
232
245
/**
233
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
234
- * Saga#compensate()} is called.
246
+ * Add compensation operation for saga.
235
247
*
236
248
* @param operation to be executed during compensation.
237
249
* @param arg1 first operation function parameter
@@ -241,8 +253,7 @@ public <A1> void addCompensation(Functions.Func1<A1, ?> operation, A1 arg1) {
241
253
}
242
254
243
255
/**
244
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
245
- * Saga#compensate()} is called.
256
+ * Add compensation operation for saga.
246
257
*
247
258
* @param operation to be executed during compensation.
248
259
* @param arg1 first operation function parameter
@@ -253,8 +264,7 @@ public <A1, A2> void addCompensation(Functions.Func2<A1, A2, ?> operation, A1 ar
253
264
}
254
265
255
266
/**
256
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
257
- * Saga#compensate()} is called.
267
+ * Add compensation operation for saga.
258
268
*
259
269
* @param operation to be executed during compensation.
260
270
* @param operation to be executed during compensation.
@@ -268,8 +278,7 @@ public <A1, A2, A3> void addCompensation(
268
278
}
269
279
270
280
/**
271
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
272
- * Saga#compensate()} is called.
281
+ * Add compensation operation for saga.
273
282
*
274
283
* @param operation to be executed during compensation.
275
284
* @param operation to be executed during compensation.
@@ -284,8 +293,7 @@ public <A1, A2, A3, A4> void addCompensation(
284
293
}
285
294
286
295
/**
287
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
288
- * Saga#compensate()} is called.
296
+ * Add compensation operation for saga.
289
297
*
290
298
* @param operation to be executed during compensation.
291
299
* @param operation to be executed during compensation.
@@ -306,8 +314,7 @@ public <A1, A2, A3, A4, A5> void addCompensation(
306
314
}
307
315
308
316
/**
309
- * Add compensation operation for saga, which will be executed in the reverse order if {@link
310
- * Saga#compensate()} is called.
317
+ * Add compensation operation for saga.
311
318
*
312
319
* @param operation to be executed during compensation.
313
320
* @param arg1 first operation function parameter
0 commit comments