Skip to content

Commit e798802

Browse files
committed
updates per recommendations
Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent e5eaab5 commit e798802

File tree

3 files changed

+131
-79
lines changed

3 files changed

+131
-79
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ See the README.md file in each main sample directory for cut/paste Gradle comman
7979
- [**HelloDelayedStart**](/core/src/main/java/io/temporal/samples/hello/HelloDelayedStart.java): Demonstrates how to use delayed start config option when starting a Workflow Executions.
8080
- [**HelloSignalWithTimer**](/core/src/main/java/io/temporal/samples/hello/HelloSignalWithTimer.java): Demonstrates how to use collect signals for certain amount of time and then process last one.
8181
- [**HelloWorkflowTimer**](/core/src/main/java/io/temporal/samples/hello/HelloWorkflowTimer.java): Demonstrates how we can use workflow timer to restrict duration of workflow execution instead of workflow run/execution timeouts.
82+
- [**Auto-Heartbeating**](/core/src/main/java/io/temporal/samples/autoheartbeat/): Demonstrates use of Auto-heartbeating utility via activity interceptor.
8283

8384

8485
#### Scenario-based samples

core/src/main/java/io/temporal/samples/autoheartbeat/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ but then at times can take for example minutes. In this case you have to set lon
99
but you might not want first heartbeat to be sent right away but send it after the "shorter" duration of activity
1010
execution.
1111

12+
Warning: make sure to test this sample for your use case. This includes load testing. This sample was not
13+
tested on large scale workloads. In addition note that it is recommended to heartbeat from activity code itself. Using
14+
this type of autoheartbeating utility does have disatvantage that activity code itself can continue running after
15+
a handled activity cancelation. Please be aware of these warnings when applying this sample.
16+
1217
1. Start the Sample:
1318

1419
```bash

core/src/main/java/io/temporal/samples/autoheartbeat/Starter.java

Lines changed: 125 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.temporal.client.WorkflowClient;
2323
import io.temporal.client.WorkflowOptions;
2424
import io.temporal.client.WorkflowStub;
25+
import io.temporal.failure.CanceledFailure;
2526
import io.temporal.samples.autoheartbeat.activities.AutoActivitiesImpl;
2627
import io.temporal.samples.autoheartbeat.interceptor.AutoHeartbeatWorkerInterceptor;
2728
import io.temporal.samples.autoheartbeat.workflows.AutoWorkflow;
@@ -32,90 +33,135 @@
3233
import io.temporal.worker.WorkerFactoryOptions;
3334

3435
public class Starter {
35-
static final String TASK_QUEUE = "AutoheartbeatTaskQueue";
36-
static final String WORKFLOW_ID = "AutoHeartbeatWorkflow";
37-
38-
public static void main(String[] args) {
39-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
40-
WorkflowClient client = WorkflowClient.newInstance(service);
41-
42-
// Configure our auto heartbeat workflow interceptor which will apply
43-
// AutoHeartbeaterUtil to each activity workflow schedules which has a heartbeat
44-
// timeout configured
45-
WorkerFactoryOptions wfo =
46-
WorkerFactoryOptions.newBuilder()
47-
.setWorkerInterceptors(new AutoHeartbeatWorkerInterceptor())
48-
.build();
49-
50-
WorkerFactory factory = WorkerFactory.newInstance(client, wfo);
51-
Worker worker = factory.newWorker(TASK_QUEUE);
52-
53-
worker.registerWorkflowImplementationTypes(AutoWorkflowImpl.class);
54-
worker.registerActivitiesImplementations(new AutoActivitiesImpl());
55-
56-
factory.start();
57-
58-
System.out.println("**** First Run: run workflow to completion");
59-
60-
AutoWorkflow firstRun =
61-
client.newWorkflowStub(
62-
AutoWorkflow.class,
63-
WorkflowOptions.newBuilder()
64-
.setWorkflowId(WORKFLOW_ID)
65-
.setTaskQueue(TASK_QUEUE)
66-
.build());
67-
68-
try {
69-
String firstRunResult = firstRun.exec("Auto heartbeating is cool");
70-
System.out.println("First run result: " + firstRunResult);
71-
} catch (Exception e) {
72-
System.out.println("First run - Workflow exec exception: " + e.getClass().getName());
36+
static final String TASK_QUEUE = "AutoheartbeatTaskQueue";
37+
static final String WORKFLOW_ID = "AutoHeartbeatWorkflow";
38+
39+
public static void main(String[] args) {
40+
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
41+
WorkflowClient client = WorkflowClient.newInstance(service);
42+
43+
// Configure our auto heartbeat workflow interceptor which will apply
44+
// AutoHeartbeaterUtil to each activity workflow schedules which has a heartbeat
45+
// timeout configured
46+
WorkerFactoryOptions wfo =
47+
WorkerFactoryOptions.newBuilder()
48+
.setWorkerInterceptors(new AutoHeartbeatWorkerInterceptor())
49+
.build();
50+
51+
WorkerFactory factory = WorkerFactory.newInstance(client, wfo);
52+
Worker worker = factory.newWorker(TASK_QUEUE);
53+
54+
worker.registerWorkflowImplementationTypes(AutoWorkflowImpl.class);
55+
worker.registerActivitiesImplementations(new AutoActivitiesImpl());
56+
57+
factory.start();
58+
59+
// first run completes execution with autoheartbeat utils
60+
firstRun(client);
61+
// second run cancels running (pending) activity via signal (specific scope cancel)
62+
secondRun(client);
63+
// third run cancels running execution which cancels activity as well
64+
thirdRun(client);
65+
// fourth run turns off autoheartbeat for activities and lets activity time out on heartbeat
66+
// timeout
67+
fourthRun(client);
68+
69+
System.exit(0);
7370
}
7471

75-
System.out.println("\n\n**** Second Run: cancel activities");
76-
77-
AutoWorkflow secondRun =
78-
client.newWorkflowStub(
79-
AutoWorkflow.class,
80-
WorkflowOptions.newBuilder()
81-
.setWorkflowId(WORKFLOW_ID)
82-
.setTaskQueue(TASK_QUEUE)
83-
.build());
84-
WorkflowClient.start(secondRun::exec, "Auto heartbeating is cool");
85-
doSleeps(4);
86-
secondRun.cancelActivity();
87-
88-
try {
89-
String secondRunResult = WorkflowStub.fromTyped(secondRun).getResult(String.class);
90-
System.out.println("Second run result: " + secondRunResult);
91-
} catch (Exception e) {
92-
System.out.println("Second run - Workflow exec exception: " + e.getClass().getName());
72+
@SuppressWarnings("unused")
73+
private static void firstRun(WorkflowClient client) {
74+
System.out.println("**** First Run: run workflow to completion");
75+
AutoWorkflow firstRun =
76+
client.newWorkflowStub(
77+
AutoWorkflow.class,
78+
WorkflowOptions.newBuilder()
79+
.setWorkflowId(WORKFLOW_ID)
80+
.setTaskQueue(TASK_QUEUE)
81+
.build());
82+
83+
try {
84+
String firstRunResult = firstRun.exec("Auto heartbeating is cool");
85+
System.out.println("First run result: " + firstRunResult);
86+
} catch (Exception e) {
87+
System.out.println("First run - Workflow exec exception: " + e.getClass().getName());
88+
}
9389
}
9490

95-
System.out.println("\n\n**** Third Run: cause heartbeat timeout");
96-
// we disable autoheartbeat via env var
97-
System.setProperty("sample.disableAutoHeartbeat", "true");
98-
AutoWorkflow thirdRun =
99-
client.newWorkflowStub(
100-
AutoWorkflow.class,
101-
WorkflowOptions.newBuilder()
102-
.setWorkflowId(WORKFLOW_ID)
103-
.setTaskQueue(TASK_QUEUE)
104-
.build());
105-
106-
try {
107-
String thirdRunResult = thirdRun.exec("Auto heartbeating is cool");
108-
System.out.println("Third run result: " + thirdRunResult);
109-
} catch (Exception e) {
110-
System.out.println("Third run - Workflow exec exception: " + e.getClass().getName());
91+
@SuppressWarnings("unused")
92+
private static void secondRun(WorkflowClient client) {
93+
System.out.println("\n\n**** Second Run: cancel activities via signal");
94+
AutoWorkflow secondRun =
95+
client.newWorkflowStub(
96+
AutoWorkflow.class,
97+
WorkflowOptions.newBuilder()
98+
.setWorkflowId(WORKFLOW_ID)
99+
.setTaskQueue(TASK_QUEUE)
100+
.build());
101+
WorkflowClient.start(secondRun::exec, "Auto heartbeating is cool");
102+
doSleeps(4);
103+
secondRun.cancelActivity();
104+
105+
try {
106+
String secondRunResult = WorkflowStub.fromTyped(secondRun).getResult(String.class);
107+
System.out.println("Second run result: " + secondRunResult);
108+
} catch (Exception e) {
109+
System.out.println("Second run - Workflow exec exception: " + e.getClass().getName());
110+
}
111111
}
112-
}
113112

114-
private static void doSleeps(int seconds) {
115-
try {
116-
Thread.sleep(seconds * 1000L);
117-
} catch (Exception e) {
118-
System.out.println(e.getMessage());
113+
@SuppressWarnings("unused")
114+
private static void thirdRun(WorkflowClient client) {
115+
System.out.println("\n\n**** Third Run: cancel workflow execution");
116+
AutoWorkflow thirdRun =
117+
client.newWorkflowStub(
118+
AutoWorkflow.class,
119+
WorkflowOptions.newBuilder()
120+
.setWorkflowId(WORKFLOW_ID)
121+
.setTaskQueue(TASK_QUEUE)
122+
.build());
123+
WorkflowClient.start(thirdRun::exec, "Auto heartbeating is cool");
124+
doSleeps(10);
125+
try {
126+
WorkflowStub.fromTyped(thirdRun).cancel();
127+
String thirdRunResult = WorkflowStub.fromTyped(thirdRun).getResult(String.class);
128+
System.out.println("Third run result: " + thirdRunResult);
129+
} catch (Exception e) {
130+
// we are expecting workflow cancelation
131+
if (e.getCause() instanceof CanceledFailure) {
132+
System.out.println("Third run - Workflow execution canceled.");
133+
} else {
134+
System.out.println("Third run - Workflow exec exception: " + e.getMessage());
135+
}
136+
}
137+
}
138+
139+
@SuppressWarnings("unused")
140+
private static void fourthRun(WorkflowClient client) {
141+
System.out.println("\n\n**** Fourth Run: cause heartbeat timeout");
142+
// we disable autoheartbeat via env var
143+
System.setProperty("sample.disableAutoHeartbeat", "true");
144+
AutoWorkflow fourth =
145+
client.newWorkflowStub(
146+
AutoWorkflow.class,
147+
WorkflowOptions.newBuilder()
148+
.setWorkflowId(WORKFLOW_ID)
149+
.setTaskQueue(TASK_QUEUE)
150+
.build());
151+
152+
try {
153+
String fourthRunResult = fourth.exec("Auto heartbeating is cool");
154+
System.out.println("Fourth run result: " + fourthRunResult);
155+
} catch (Exception e) {
156+
System.out.println("Fourth run - Workflow exec exception: " + e.getClass().getName());
157+
}
158+
}
159+
160+
private static void doSleeps(int seconds) {
161+
try {
162+
Thread.sleep(seconds * 1000L);
163+
} catch (Exception e) {
164+
System.out.println(e.getMessage());
165+
}
119166
}
120-
}
121167
}

0 commit comments

Comments
 (0)