@@ -43,7 +43,20 @@ public class HelloPeriodic {
43
43
private static final String TASK_LIST = "HelloPeriodic" ;
44
44
45
45
public interface GreetingWorkflow {
46
- @ WorkflowMethod
46
+ /**
47
+ * Use single fixed ID to ensure that there is at most one instance running.
48
+ * To run multiple instances set different ids through
49
+ * WorkflowOptions passed to WorkflowClient.newWorkflowStub call.
50
+ */
51
+ @ WorkflowMethod (
52
+ // At most one instance
53
+ workflowId = "HelloPeriodic" ,
54
+ // To allow starting workflow with the same ID after the previous one has terminated.
55
+ workflowIdReusePolicy = WorkflowIdReusePolicy .AllowDuplicate ,
56
+ // Adjust this value to the maximum time workflow is expected to run
57
+ // It usually depends on number of repetitions and interval between them.
58
+ executionStartToCloseTimeoutSeconds = 300 ,
59
+ taskList = TASK_LIST )
47
60
void greetPeriodically (String name , Duration delay );
48
61
}
49
62
@@ -123,22 +136,8 @@ public static void main(String[] args) throws InterruptedException {
123
136
System .out .println ("Previous instance failed:\n " + Throwables .getStackTraceAsString (e ));
124
137
}
125
138
}
126
- // Get a workflow stub using the same task list the worker uses.
127
- WorkflowOptions workflowOptions = new WorkflowOptions .Builder ()
128
- .setTaskList (TASK_LIST )
129
- // Adjust this value to the maximum time workflow is expected to run
130
- // It usually depends on number of repetitions and interval between them.
131
- .setExecutionStartToCloseTimeout (Duration .ofMinutes (10 ))
132
- // Use single fixed ID to ensure that there is at most one instance running.
133
- // Obviously if there is need to run multiple instances adjust the ID appropriately to have one
134
- // per periodic business process.
135
- .setWorkflowId ("HelloPeriodic" )
136
- // To allow starting workflow with the same ID after the previous one has terminated.
137
- .setWorkflowIdReusePolicy (WorkflowIdReusePolicy .AllowDuplicate )
138
- .build ();
139
139
// New stub instance should be created for each new workflow start.
140
- GreetingWorkflow workflow = workflowClient .newWorkflowStub (GreetingWorkflow .class ,
141
- workflowOptions );
140
+ GreetingWorkflow workflow = workflowClient .newWorkflowStub (GreetingWorkflow .class );
142
141
try {
143
142
execution = WorkflowClient .asyncStart (workflow ::greetPeriodically ,
144
143
"World" , Duration .ofSeconds (1 ));
0 commit comments