55import com .uber .cadence .client .WorkflowOptions ;
66import com .uber .cadence .worker .Worker ;
77import com .uber .cadence .workflow .*;
8- import org .slf4j .Logger ;
9- import org .slf4j .LoggerFactory ;
108
119import java .time .Duration ;
12- import java .util .ArrayList ;
13- import java .util .List ;
14- import java .util .concurrent .atomic .AtomicInteger ;
1510
1611import static com .uber .cadence .samples .common .SampleConstants .DOMAIN ;
1712
2015 */
2116public class HelloSaga {
2217 static final String TASK_LIST = "HelloSaga" ;
23- static final List <String > transactions = new ArrayList <>();
2418
2519 public interface ChildWorkflowOperation {
2620 @ WorkflowMethod
2721 void execute (int amount );
2822 }
2923
3024 public static class ChildWorkflowOperationImpl implements ChildWorkflowOperation {
31- Logger log = Workflow .getLogger ( ChildWorkflowOperationImpl .class );
25+ ActivityOperation activity = Workflow .newActivityStub ( ActivityOperation .class );
3226
3327 public void execute (int amount ) {
34- log .info ("ChildWorkflowOperationImpl.execute() is called." );
35- transactions .add ("child workflow execution: " + amount );
28+ activity .execute (amount );
3629 }
3730 }
3831
@@ -42,39 +35,29 @@ public interface ChildWorkflowCompensation {
4235 }
4336
4437 public static class ChildWorkflowCompensationImpl implements ChildWorkflowCompensation {
45- Logger log = Workflow .getLogger ( ChildWorkflowCompensationImpl .class );
38+ ActivityOperation activity = Workflow .newActivityStub ( ActivityOperation .class );
4639
4740 public void compensate (int amount ) {
48- log .info ("ChildWorkflowCompensationImpl.compensate() is called." );
49- transactions .add ("child workflow compensation: " + amount );
41+ activity .compensate (amount );
5042 }
5143 }
5244
5345 public interface ActivityOperation {
5446 @ ActivityMethod (scheduleToCloseTimeoutSeconds = 2 )
5547 void execute (int amount );
48+
49+ @ ActivityMethod (scheduleToCloseTimeoutSeconds = 2 )
50+ void compensate (int amount );
5651 }
5752
5853 public static class ActivityOperationImpl implements ActivityOperation {
59- Logger log = LoggerFactory .getLogger (ActivityOperationImpl .class );
6054
6155 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 );
6457 }
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 );
7458
7559 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 );
7861 }
7962 }
8063
@@ -83,39 +66,37 @@ public interface SagaWorkflow {
8366 * Main saga workflow.
8467 */
8568 @ WorkflowMethod
86- List < String > execute ();
69+ void execute ();
8770 }
8871
8972 public static class SagaWorkflowImpl implements SagaWorkflow {
73+ ActivityOperation activity = Workflow .newActivityStub (ActivityOperation .class );
74+
9075 @ Override
91- public List < String > execute () {
76+ public void execute () {
9277 Saga saga = new Saga (new Saga .Options .Builder ().setParallelCompensation (false ).build ());
9378 try {
9479 ChildWorkflowOperation op1 = Workflow .newChildWorkflowStub (ChildWorkflowOperation .class );
9580 op1 .execute (10 );
9681 ChildWorkflowCompensation c1 = Workflow .newChildWorkflowStub (ChildWorkflowCompensation .class );
9782 saga .addCompensation (c1 ::compensate , -10 );
9883
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 );
10185 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 );
10787
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." ));
10892 throw new RuntimeException ("some error" );
10993
11094 } catch (Exception e ) {
11195 saga .compensate ();
11296 }
113-
114- return transactions ;
11597 }
11698 }
11799
118-
119100 public static void main (String [] args ) {
120101 // Start a worker that hosts the workflow implementation.
121102 Worker .Factory factory = new Worker .Factory (DOMAIN );
@@ -124,7 +105,7 @@ public static void main(String[] args) {
124105 HelloSaga .SagaWorkflowImpl .class ,
125106 HelloSaga .ChildWorkflowOperationImpl .class ,
126107 HelloSaga .ChildWorkflowCompensationImpl .class );
127- worker .registerActivitiesImplementations (new ActivityOperationImpl (), new ActivityCompensationImpl () );
108+ worker .registerActivitiesImplementations (new ActivityOperationImpl ());
128109 factory .start ();
129110
130111 // Start a workflow execution. Usually this is done from another program.
@@ -137,7 +118,7 @@ public static void main(String[] args) {
137118 .build ();
138119 HelloSaga .SagaWorkflow workflow =
139120 workflowClient .newWorkflowStub (HelloSaga .SagaWorkflow .class , workflowOptions );
140- System . out . println ( workflow .execute () );
121+ workflow .execute ();
141122 System .exit (0 );
142123 }
143124}
0 commit comments