1
1
package com .uber .cadence .samples .hello ;
2
2
3
+ import static com .uber .cadence .samples .common .SampleConstants .DOMAIN ;
4
+
3
5
import com .uber .cadence .activity .ActivityMethod ;
4
6
import com .uber .cadence .client .WorkflowClient ;
5
7
import com .uber .cadence .client .WorkflowOptions ;
6
8
import com .uber .cadence .worker .Worker ;
7
9
import com .uber .cadence .workflow .*;
8
-
9
10
import java .time .Duration ;
10
11
11
- import static com .uber .cadence .samples .common .SampleConstants .DOMAIN ;
12
-
13
- /**
14
- * Demonstrates implementing saga transaction and compensation logic using Cadence.
15
- */
12
+ /** Demonstrates implementing saga transaction and compensation logic using Cadence. */
16
13
public class HelloSaga {
17
14
static final String TASK_LIST = "HelloSaga" ;
18
15
@@ -24,6 +21,7 @@ public interface ChildWorkflowOperation {
24
21
public static class ChildWorkflowOperationImpl implements ChildWorkflowOperation {
25
22
ActivityOperation activity = Workflow .newActivityStub (ActivityOperation .class );
26
23
24
+ @ Override
27
25
public void execute (int amount ) {
28
26
activity .execute (amount );
29
27
}
@@ -37,6 +35,7 @@ public interface ChildWorkflowCompensation {
37
35
public static class ChildWorkflowCompensationImpl implements ChildWorkflowCompensation {
38
36
ActivityOperation activity = Workflow .newActivityStub (ActivityOperation .class );
39
37
38
+ @ Override
40
39
public void compensate (int amount ) {
41
40
activity .compensate (amount );
42
41
}
@@ -52,22 +51,23 @@ public interface ActivityOperation {
52
51
53
52
public static class ActivityOperationImpl implements ActivityOperation {
54
53
54
+ @ Override
55
55
public void execute (int amount ) {
56
56
System .out .println ("ActivityOperationImpl.execute() is called with amount " + amount );
57
57
}
58
58
59
+ @ Override
59
60
public void compensate (int amount ) {
60
61
System .out .println ("ActivityCompensationImpl.compensate() is called with amount " + amount );
61
62
}
62
63
}
63
64
64
65
public interface SagaWorkflow {
65
66
/**
66
- * Main saga workflow.
67
- * Here we execute activity operation twice (first from a child workflow, second directly using
68
- * activity stub), add three compensation functions, and then throws some exception in workflow code.
69
- * When we catch the exception, saga.compensate will run the compensation functions according
70
- * to the policy specified in SagaOptions.
67
+ * Main saga workflow. Here we execute activity operation twice (first from a child workflow,
68
+ * second directly using activity stub), add three compensation functions, and then throws some
69
+ * exception in workflow code. When we catch the exception, saga.compensate will run the
70
+ * compensation functions according to the policy specified in SagaOptions.
71
71
*/
72
72
@ WorkflowMethod
73
73
void execute ();
@@ -83,7 +83,8 @@ public void execute() {
83
83
// The following demonstrate how to compensate sync invocations.
84
84
ChildWorkflowOperation op1 = Workflow .newChildWorkflowStub (ChildWorkflowOperation .class );
85
85
op1 .execute (10 );
86
- ChildWorkflowCompensation c1 = Workflow .newChildWorkflowStub (ChildWorkflowCompensation .class );
86
+ ChildWorkflowCompensation c1 =
87
+ Workflow .newChildWorkflowStub (ChildWorkflowCompensation .class );
87
88
saga .addCompensation (c1 ::compensate , -10 );
88
89
89
90
// The following demonstrate how to compensate async invocations.
@@ -94,7 +95,8 @@ public void execute() {
94
95
// The following demonstrate the ability of supplying arbitrary lambda as a saga
95
96
// compensation function. In production code please always use Workflow.getLogger
96
97
// to log messages in workflow code.
97
- saga .addCompensation (() -> System .out .println ("Other compensation logic in main workflow." ));
98
+ saga .addCompensation (
99
+ () -> System .out .println ("Other compensation logic in main workflow." ));
98
100
throw new RuntimeException ("some error" );
99
101
100
102
} catch (Exception e ) {
0 commit comments