2020import java .util .ArrayList ;
2121import java .util .List ;
2222
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+ */
2344public final class Saga {
2445 private final Options options ;
2546 private final List <Functions .Func <Promise >> compensationOps = new ArrayList <>();
@@ -119,8 +140,7 @@ public void compensate() {
119140 }
120141
121142 /**
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.
124144 *
125145 * @param operation to be executed during compensation.
126146 */
@@ -129,8 +149,7 @@ public void addCompensation(Functions.Proc operation) {
129149 }
130150
131151 /**
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.
134153 *
135154 * @param operation to be executed during compensation.
136155 * @param arg1 first operation function parameter
@@ -140,8 +159,7 @@ public <A1> void addCompensation(Functions.Proc1<A1> operation, A1 arg1) {
140159 }
141160
142161 /**
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.
145163 *
146164 * @param operation to be executed during compensation.
147165 * @param arg1 first operation function parameter
@@ -152,8 +170,7 @@ public <A1, A2> void addCompensation(Functions.Proc2<A1, A2> operation, A1 arg1,
152170 }
153171
154172 /**
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.
157174 *
158175 * @param operation to be executed during compensation.
159176 * @param arg1 first operation function parameter
@@ -166,8 +183,7 @@ public <A1, A2, A3> void addCompensation(
166183 }
167184
168185 /**
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.
171187 *
172188 * @param operation to be executed during compensation.
173189 * @param arg1 first operation function parameter
@@ -181,8 +197,7 @@ public <A1, A2, A3, A4> void addCompensation(
181197 }
182198
183199 /**
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.
186201 *
187202 * @param operation to be executed during compensation.
188203 * @param arg1 first operation function parameter
@@ -197,8 +212,7 @@ public <A1, A2, A3, A4, A5> void addCompensation(
197212 }
198213
199214 /**
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.
202216 *
203217 * @param operation to be executed during compensation.
204218 * @param arg1 first operation function parameter
@@ -220,8 +234,7 @@ public <A1, A2, A3, A4, A5, A6> void addCompensation(
220234 }
221235
222236 /**
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.
225238 *
226239 * @param operation to be executed during compensation.
227240 */
@@ -230,8 +243,7 @@ public void addCompensation(Functions.Func<?> operation) {
230243 }
231244
232245 /**
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.
235247 *
236248 * @param operation to be executed during compensation.
237249 * @param arg1 first operation function parameter
@@ -241,8 +253,7 @@ public <A1> void addCompensation(Functions.Func1<A1, ?> operation, A1 arg1) {
241253 }
242254
243255 /**
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.
246257 *
247258 * @param operation to be executed during compensation.
248259 * @param arg1 first operation function parameter
@@ -253,8 +264,7 @@ public <A1, A2> void addCompensation(Functions.Func2<A1, A2, ?> operation, A1 ar
253264 }
254265
255266 /**
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.
258268 *
259269 * @param operation to be executed during compensation.
260270 * @param operation to be executed during compensation.
@@ -268,8 +278,7 @@ public <A1, A2, A3> void addCompensation(
268278 }
269279
270280 /**
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.
273282 *
274283 * @param operation to be executed during compensation.
275284 * @param operation to be executed during compensation.
@@ -284,8 +293,7 @@ public <A1, A2, A3, A4> void addCompensation(
284293 }
285294
286295 /**
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.
289297 *
290298 * @param operation to be executed during compensation.
291299 * @param operation to be executed during compensation.
@@ -306,8 +314,7 @@ public <A1, A2, A3, A4, A5> void addCompensation(
306314 }
307315
308316 /**
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.
311318 *
312319 * @param operation to be executed during compensation.
313320 * @param arg1 first operation function parameter
0 commit comments