5
5
import com .uber .cadence .client .WorkflowOptions ;
6
6
import com .uber .cadence .worker .Worker ;
7
7
import com .uber .cadence .workflow .*;
8
- import org .slf4j .Logger ;
9
- import org .slf4j .LoggerFactory ;
10
8
11
9
import java .time .Duration ;
12
- import java .util .ArrayList ;
13
- import java .util .List ;
14
- import java .util .concurrent .atomic .AtomicInteger ;
15
10
16
11
import static com .uber .cadence .samples .common .SampleConstants .DOMAIN ;
17
12
20
15
*/
21
16
public class HelloSaga {
22
17
static final String TASK_LIST = "HelloSaga" ;
23
- static final List <String > transactions = new ArrayList <>();
24
18
25
19
public interface ChildWorkflowOperation {
26
20
@ WorkflowMethod
27
21
void execute (int amount );
28
22
}
29
23
30
24
public static class ChildWorkflowOperationImpl implements ChildWorkflowOperation {
31
- Logger log = Workflow .getLogger ( ChildWorkflowOperationImpl .class );
25
+ ActivityOperation activity = Workflow .newActivityStub ( ActivityOperation .class );
32
26
33
27
public void execute (int amount ) {
34
- log .info ("ChildWorkflowOperationImpl.execute() is called." );
35
- transactions .add ("child workflow execution: " + amount );
28
+ activity .execute (amount );
36
29
}
37
30
}
38
31
@@ -42,39 +35,29 @@ public interface ChildWorkflowCompensation {
42
35
}
43
36
44
37
public static class ChildWorkflowCompensationImpl implements ChildWorkflowCompensation {
45
- Logger log = Workflow .getLogger ( ChildWorkflowCompensationImpl .class );
38
+ ActivityOperation activity = Workflow .newActivityStub ( ActivityOperation .class );
46
39
47
40
public void compensate (int amount ) {
48
- log .info ("ChildWorkflowCompensationImpl.compensate() is called." );
49
- transactions .add ("child workflow compensation: " + amount );
41
+ activity .compensate (amount );
50
42
}
51
43
}
52
44
53
45
public interface ActivityOperation {
54
46
@ ActivityMethod (scheduleToCloseTimeoutSeconds = 2 )
55
47
void execute (int amount );
48
+
49
+ @ ActivityMethod (scheduleToCloseTimeoutSeconds = 2 )
50
+ void compensate (int amount );
56
51
}
57
52
58
53
public static class ActivityOperationImpl implements ActivityOperation {
59
- Logger log = LoggerFactory .getLogger (ActivityOperationImpl .class );
60
54
61
55
public void execute (int amount ) {
62
- log .info ("ActivityOperationImpl.execute() is called." );
63
- transactions .add ("activity execution: " + amount );
56
+ System .out .println ("ActivityOperationImpl.execute() is called with amount " + amount );
64
57
}
65
- }
66
-
67
- public interface ActivityCompensation {
68
- @ ActivityMethod (scheduleToCloseTimeoutSeconds = 2 )
69
- void compensate (int amount );
70
- }
71
-
72
- public static class ActivityCompensationImpl implements ActivityCompensation {
73
- Logger log = LoggerFactory .getLogger (ActivityCompensationImpl .class );
74
58
75
59
public void compensate (int amount ) {
76
- log .info ("ActivityCompensationImpl.execute() is called." );
77
- transactions .add ("activity compensation: " + amount );
60
+ System .out .println ("ActivityCompensationImpl.compensate() is called with amount " + amount );
78
61
}
79
62
}
80
63
@@ -83,39 +66,37 @@ public interface SagaWorkflow {
83
66
* Main saga workflow.
84
67
*/
85
68
@ WorkflowMethod
86
- List < String > execute ();
69
+ void execute ();
87
70
}
88
71
89
72
public static class SagaWorkflowImpl implements SagaWorkflow {
73
+ ActivityOperation activity = Workflow .newActivityStub (ActivityOperation .class );
74
+
90
75
@ Override
91
- public List < String > execute () {
76
+ public void execute () {
92
77
Saga saga = new Saga (new Saga .Options .Builder ().setParallelCompensation (false ).build ());
93
78
try {
94
79
ChildWorkflowOperation op1 = Workflow .newChildWorkflowStub (ChildWorkflowOperation .class );
95
80
op1 .execute (10 );
96
81
ChildWorkflowCompensation c1 = Workflow .newChildWorkflowStub (ChildWorkflowCompensation .class );
97
82
saga .addCompensation (c1 ::compensate , -10 );
98
83
99
- ActivityOperation op2 = Workflow .newActivityStub (ActivityOperation .class );
100
- Promise <Void > result = Async .procedure (op2 ::execute , 20 );
84
+ Promise <Void > result = Async .procedure (activity ::execute , 20 );
101
85
result .get ();
102
- ActivityCompensation c2 = Workflow .newActivityStub (ActivityCompensation .class );
103
- saga .addCompensation (c2 ::compensate , -20 );
104
-
105
- transactions .add ("main workflow: " + 30 );
106
- saga .addCompensation (()->transactions .add ("main workflow compensation: " + -30 ));
86
+ saga .addCompensation (activity ::compensate , -20 );
107
87
88
+ // The following is just to demonstrate the ability of supplying arbitrary lambda as a
89
+ // saga compensation function. In production code please always use Workflow.getLogger
90
+ // to log messages in workflow code.
91
+ saga .addCompensation (() -> System .out .println ("Other compensation logic in main workflow." ));
108
92
throw new RuntimeException ("some error" );
109
93
110
94
} catch (Exception e ) {
111
95
saga .compensate ();
112
96
}
113
-
114
- return transactions ;
115
97
}
116
98
}
117
99
118
-
119
100
public static void main (String [] args ) {
120
101
// Start a worker that hosts the workflow implementation.
121
102
Worker .Factory factory = new Worker .Factory (DOMAIN );
@@ -124,7 +105,7 @@ public static void main(String[] args) {
124
105
HelloSaga .SagaWorkflowImpl .class ,
125
106
HelloSaga .ChildWorkflowOperationImpl .class ,
126
107
HelloSaga .ChildWorkflowCompensationImpl .class );
127
- worker .registerActivitiesImplementations (new ActivityOperationImpl (), new ActivityCompensationImpl () );
108
+ worker .registerActivitiesImplementations (new ActivityOperationImpl ());
128
109
factory .start ();
129
110
130
111
// Start a workflow execution. Usually this is done from another program.
@@ -137,7 +118,7 @@ public static void main(String[] args) {
137
118
.build ();
138
119
HelloSaga .SagaWorkflow workflow =
139
120
workflowClient .newWorkflowStub (HelloSaga .SagaWorkflow .class , workflowOptions );
140
- System . out . println ( workflow .execute () );
121
+ workflow .execute ();
141
122
System .exit (0 );
142
123
}
143
124
}
0 commit comments