Skip to content

Commit d3b90e6

Browse files
authored
Update samples to use env config (#756)
* Update samples to use env config * fix envconfig import for springboot * update javaSDKVersion to 1.32.1
1 parent c93875b commit d3b90e6

File tree

97 files changed

+1313
-317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1313
-317
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ subprojects {
2626
ext {
2727
otelVersion = '1.30.1'
2828
otelVersionAlpha = "${otelVersion}-alpha"
29-
javaSDKVersion = '1.32.0'
29+
javaSDKVersion = '1.32.1'
3030
camelVersion = '3.22.1'
3131
jarVersion = '1.0.0'
3232
}

core/src/main/java/io/temporal/samples/apikey/ApiKeyWorker.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
import io.temporal.client.WorkflowClient;
44
import io.temporal.client.WorkflowClientOptions;
5+
import io.temporal.envconfig.ClientConfigProfile;
56
import io.temporal.serviceclient.WorkflowServiceStubs;
67
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
78
import io.temporal.worker.Worker;
89
import io.temporal.worker.WorkerFactory;
10+
import java.io.IOException;
911

1012
public class ApiKeyWorker {
1113
static final String TASK_QUEUE = "MyTaskQueue";
1214

1315
public static void main(String[] args) throws Exception {
16+
// Load configuration from environment and files
17+
ClientConfigProfile profile;
18+
try {
19+
profile = ClientConfigProfile.load();
20+
} catch (IOException e) {
21+
throw new RuntimeException("Failed to load client configuration", e);
22+
}
23+
1424
// For temporal cloud this would be ${cloud-region}.{cloud}.api.temporal.io:7233
1525
// Example us-east-1.aws.api.temporal.io:7233
1626
String targetEndpoint = System.getenv("TEMPORAL_ENDPOINT");
@@ -24,10 +34,10 @@ public static void main(String[] args) throws Exception {
2434
"TEMPORAL_ENDPOINT, TEMPORAL_NAMESPACE, and TEMPORAL_API_KEY environment variables must be set");
2535
}
2636

27-
// Create API Key enabled client
37+
// Create API Key enabled client with environment config as base
2838
WorkflowServiceStubs service =
2939
WorkflowServiceStubs.newServiceStubs(
30-
WorkflowServiceStubsOptions.newBuilder()
40+
WorkflowServiceStubsOptions.newBuilder(profile.toWorkflowServiceStubsOptions())
3141
.setTarget(targetEndpoint)
3242
.setEnableHttps(true)
3343
.addApiKey(() -> apiKey)
@@ -36,7 +46,10 @@ public static void main(String[] args) throws Exception {
3646
// Now setup and start workflow worker
3747
WorkflowClient client =
3848
WorkflowClient.newInstance(
39-
service, WorkflowClientOptions.newBuilder().setNamespace(namespace).build());
49+
service,
50+
WorkflowClientOptions.newBuilder(profile.toWorkflowClientOptions())
51+
.setNamespace(namespace)
52+
.build());
4053

4154
// worker factory that can be used to create workers for specific task queues
4255
WorkerFactory factory = WorkerFactory.newInstance(client);

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@
33
import io.temporal.client.WorkflowClient;
44
import io.temporal.client.WorkflowClientOptions;
55
import io.temporal.client.WorkflowOptions;
6+
import io.temporal.envconfig.ClientConfigProfile;
67
import io.temporal.serviceclient.WorkflowServiceStubs;
78
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
89
import io.temporal.worker.Worker;
910
import io.temporal.worker.WorkerFactory;
11+
import java.io.IOException;
1012

1113
public class Starter {
1214

1315
static final String TASK_QUEUE = "MyTaskQueue";
1416
static final String WORKFLOW_ID = "HelloAPIKeyWorkflow";
1517

1618
public static void main(String[] args) throws Exception {
19+
// Load configuration from environment and files
20+
ClientConfigProfile profile;
21+
try {
22+
profile = ClientConfigProfile.load();
23+
} catch (IOException e) {
24+
throw new RuntimeException("Failed to load client configuration", e);
25+
}
26+
1727
// For temporal cloud this would be ${cloud-region}.{cloud}.api.temporal.io:7233
1828
// Example us-east-1.aws.api.temporal.io:7233
1929
String targetEndpoint = System.getenv("TEMPORAL_ENDPOINT");
@@ -27,18 +37,21 @@ public static void main(String[] args) throws Exception {
2737
"TEMPORAL_ENDPOINT, TEMPORAL_NAMESPACE, and TEMPORAL_API_KEY environment variables must be set");
2838
}
2939

30-
// Create API Key enabled client
40+
// Create API Key enabled client with environment config as base
3141
WorkflowServiceStubs service =
3242
WorkflowServiceStubs.newServiceStubs(
33-
WorkflowServiceStubsOptions.newBuilder()
43+
WorkflowServiceStubsOptions.newBuilder(profile.toWorkflowServiceStubsOptions())
3444
.setTarget(targetEndpoint)
3545
.setEnableHttps(true)
3646
.addApiKey(() -> apiKey)
3747
.build());
3848

3949
WorkflowClient client =
4050
WorkflowClient.newInstance(
41-
service, WorkflowClientOptions.newBuilder().setNamespace(namespace).build());
51+
service,
52+
WorkflowClientOptions.newBuilder(profile.toWorkflowClientOptions())
53+
.setNamespace(namespace)
54+
.build());
4255

4356
WorkerFactory factory = WorkerFactory.newInstance(client);
4457

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@
66
import io.temporal.api.workflowservice.v1.DescribeWorkflowExecutionResponse;
77
import io.temporal.client.WorkflowClient;
88
import io.temporal.client.WorkflowOptions;
9+
import io.temporal.envconfig.ClientConfigProfile;
910
import io.temporal.serviceclient.WorkflowServiceStubs;
1011
import io.temporal.worker.Worker;
1112
import io.temporal.worker.WorkerFactory;
13+
import java.io.IOException;
1214
import java.util.concurrent.TimeUnit;
1315

1416
public class Starter {
1517

1618
public static final String TASK_QUEUE = "asyncChildTaskQueue";
17-
private static final WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
18-
private static final WorkflowClient client = WorkflowClient.newInstance(service);
19-
private static final WorkerFactory factory = WorkerFactory.newInstance(client);
2019

2120
public static void main(String[] args) {
22-
createWorker();
21+
// Load configuration from environment and files
22+
ClientConfigProfile profile;
23+
try {
24+
profile = ClientConfigProfile.load();
25+
} catch (IOException e) {
26+
throw new RuntimeException("Failed to load client configuration", e);
27+
}
28+
29+
WorkflowServiceStubs service =
30+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
31+
WorkflowClient client = WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
32+
WorkerFactory factory = WorkerFactory.newInstance(client);
33+
34+
createWorker(factory);
2335

2436
WorkflowOptions parentWorkflowOptions =
2537
WorkflowOptions.newBuilder()
@@ -33,25 +45,28 @@ public static void main(String[] args) {
3345
WorkflowExecution childWorkflowExecution = parentWorkflowStub.executeParent();
3446

3547
// Get the child workflow execution status (after parent completed)
36-
System.out.println("Child execution status: " + getStatusAsString(childWorkflowExecution));
48+
System.out.println(
49+
"Child execution status: " + getStatusAsString(childWorkflowExecution, client, service));
3750

3851
// Wait for child workflow to complete
3952
sleep(4);
4053

4154
// Check the status of the child workflow again
42-
System.out.println("Child execution status: " + getStatusAsString(childWorkflowExecution));
55+
System.out.println(
56+
"Child execution status: " + getStatusAsString(childWorkflowExecution, client, service));
4357

4458
System.exit(0);
4559
}
4660

47-
private static void createWorker() {
61+
private static void createWorker(WorkerFactory factory) {
4862
Worker worker = factory.newWorker(TASK_QUEUE);
4963
worker.registerWorkflowImplementationTypes(ParentWorkflowImpl.class, ChildWorkflowImpl.class);
5064

5165
factory.start();
5266
}
5367

54-
private static String getStatusAsString(WorkflowExecution execution) {
68+
private static String getStatusAsString(
69+
WorkflowExecution execution, WorkflowClient client, WorkflowServiceStubs service) {
5570
DescribeWorkflowExecutionRequest describeWorkflowExecutionRequest =
5671
DescribeWorkflowExecutionRequest.newBuilder()
5772
.setNamespace(client.getOptions().getNamespace())

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import io.temporal.client.WorkflowClient;
44
import io.temporal.client.WorkflowOptions;
5+
import io.temporal.envconfig.ClientConfigProfile;
56
import io.temporal.serviceclient.WorkflowServiceStubs;
67
import io.temporal.worker.Worker;
78
import io.temporal.worker.WorkerFactory;
9+
import java.io.IOException;
810

911
/**
1012
* Sample Temporal Workflow Definition that demonstrates the execution of a Child Workflow. Child
@@ -24,12 +26,21 @@ public class Starter {
2426
public static void main(String[] args) {
2527

2628
// Get a Workflow service stub.
27-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
29+
// Load configuration from environment and files
30+
ClientConfigProfile profile;
31+
try {
32+
profile = ClientConfigProfile.load();
33+
} catch (IOException e) {
34+
throw new RuntimeException("Failed to load client configuration", e);
35+
}
36+
37+
WorkflowServiceStubs service =
38+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
2839

2940
/*
3041
* Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
3142
*/
32-
WorkflowClient client = WorkflowClient.newInstance(service);
43+
WorkflowClient client = WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
3344

3445
/*
3546
* Define the workflow factory. It is used to create workflow workers for a specific task queue.

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

Lines changed: 13 additions & 2 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.envconfig.ClientConfigProfile;
2526
import io.temporal.failure.CanceledFailure;
2627
import io.temporal.samples.autoheartbeat.activities.AutoActivitiesImpl;
2728
import io.temporal.samples.autoheartbeat.interceptor.AutoHeartbeatWorkerInterceptor;
@@ -31,14 +32,24 @@
3132
import io.temporal.worker.Worker;
3233
import io.temporal.worker.WorkerFactory;
3334
import io.temporal.worker.WorkerFactoryOptions;
35+
import java.io.IOException;
3436

3537
public class Starter {
3638
static final String TASK_QUEUE = "AutoheartbeatTaskQueue";
3739
static final String WORKFLOW_ID = "AutoHeartbeatWorkflow";
3840

3941
public static void main(String[] args) {
40-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
41-
WorkflowClient client = WorkflowClient.newInstance(service);
42+
// Load configuration from environment and files
43+
ClientConfigProfile profile;
44+
try {
45+
profile = ClientConfigProfile.load();
46+
} catch (IOException e) {
47+
throw new RuntimeException("Failed to load client configuration", e);
48+
}
49+
50+
WorkflowServiceStubs service =
51+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
52+
WorkflowClient client = WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
4253

4354
// Configure our auto heartbeat workflow interceptor which will apply
4455
// AutoHeartbeaterUtil to each activity workflow schedules which has a heartbeat

core/src/main/java/io/temporal/samples/batch/heartbeatingactivity/HeartbeatingActivityBatchStarter.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@
55
import io.temporal.api.common.v1.WorkflowExecution;
66
import io.temporal.client.WorkflowClient;
77
import io.temporal.client.WorkflowOptions;
8+
import io.temporal.envconfig.ClientConfigProfile;
89
import io.temporal.serviceclient.WorkflowServiceStubs;
10+
import java.io.IOException;
911

1012
/** Starts a single execution of HeartbeatingActivityBatchWorkflow. */
1113
public class HeartbeatingActivityBatchStarter {
1214

1315
public static void main(String[] args) {
14-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
15-
WorkflowClient workflowClient = WorkflowClient.newInstance(service);
16+
// Load configuration from environment and files
17+
ClientConfigProfile profile;
18+
try {
19+
profile = ClientConfigProfile.load();
20+
} catch (IOException e) {
21+
throw new RuntimeException("Failed to load client configuration", e);
22+
}
23+
24+
WorkflowServiceStubs service =
25+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
26+
WorkflowClient workflowClient =
27+
WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
1628
WorkflowOptions options = WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).build();
1729
HeartbeatingActivityBatchWorkflow batchWorkflow =
1830
workflowClient.newWorkflowStub(HeartbeatingActivityBatchWorkflow.class, options);

core/src/main/java/io/temporal/samples/batch/heartbeatingactivity/HeartbeatingActivityBatchWorker.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.temporal.samples.batch.heartbeatingactivity;
22

33
import io.temporal.client.WorkflowClient;
4+
import io.temporal.envconfig.ClientConfigProfile;
45
import io.temporal.serviceclient.WorkflowServiceStubs;
56
import io.temporal.worker.Worker;
67
import io.temporal.worker.WorkerFactory;
8+
import java.io.IOException;
79

810
/**
911
* A worker process that hosts implementations of HeartbeatingActivityBatchWorkflow and
@@ -14,8 +16,17 @@ public final class HeartbeatingActivityBatchWorker {
1416
static final String TASK_QUEUE = "HeartbeatingActivityBatch";
1517

1618
public static void main(String[] args) {
17-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
18-
WorkflowClient client = WorkflowClient.newInstance(service);
19+
// Load configuration from environment and files
20+
ClientConfigProfile profile;
21+
try {
22+
profile = ClientConfigProfile.load();
23+
} catch (IOException e) {
24+
throw new RuntimeException("Failed to load client configuration", e);
25+
}
26+
27+
WorkflowServiceStubs service =
28+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
29+
WorkflowClient client = WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
1930

2031
WorkerFactory factory = WorkerFactory.newInstance(client);
2132
Worker worker = factory.newWorker(TASK_QUEUE);

core/src/main/java/io/temporal/samples/batch/iterator/IteratorBatchStarter.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@
55
import io.temporal.api.common.v1.WorkflowExecution;
66
import io.temporal.client.WorkflowClient;
77
import io.temporal.client.WorkflowOptions;
8+
import io.temporal.envconfig.ClientConfigProfile;
89
import io.temporal.serviceclient.WorkflowServiceStubs;
10+
import java.io.IOException;
911

1012
/** Starts a single execution of IteratorBatchWorkflow. */
1113
public class IteratorBatchStarter {
1214

1315
public static void main(String[] args) {
14-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
15-
WorkflowClient workflowClient = WorkflowClient.newInstance(service);
16+
// Load configuration from environment and files
17+
ClientConfigProfile profile;
18+
try {
19+
profile = ClientConfigProfile.load();
20+
} catch (IOException e) {
21+
throw new RuntimeException("Failed to load client configuration", e);
22+
}
23+
24+
WorkflowServiceStubs service =
25+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
26+
WorkflowClient workflowClient =
27+
WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
1628
WorkflowOptions options = WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).build();
1729
IteratorBatchWorkflow batchWorkflow =
1830
workflowClient.newWorkflowStub(IteratorBatchWorkflow.class, options);

core/src/main/java/io/temporal/samples/batch/iterator/IteratorBatchWorker.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.temporal.samples.batch.iterator;
22

33
import io.temporal.client.WorkflowClient;
4+
import io.temporal.envconfig.ClientConfigProfile;
45
import io.temporal.serviceclient.WorkflowServiceStubs;
56
import io.temporal.worker.Worker;
67
import io.temporal.worker.WorkerFactory;
8+
import java.io.IOException;
79

810
/**
911
* A worker process that hosts implementations of IteratorBatchWorkflow and RecordProcessorWorkflow
@@ -14,8 +16,17 @@ public final class IteratorBatchWorker {
1416
static final String TASK_QUEUE = "IteratorBatch";
1517

1618
public static void main(String[] args) {
17-
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
18-
WorkflowClient client = WorkflowClient.newInstance(service);
19+
// Load configuration from environment and files
20+
ClientConfigProfile profile;
21+
try {
22+
profile = ClientConfigProfile.load();
23+
} catch (IOException e) {
24+
throw new RuntimeException("Failed to load client configuration", e);
25+
}
26+
27+
WorkflowServiceStubs service =
28+
WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions());
29+
WorkflowClient client = WorkflowClient.newInstance(service, profile.toWorkflowClientOptions());
1930

2031
WorkerFactory factory = WorkerFactory.newInstance(client);
2132
Worker worker = factory.newWorker(TASK_QUEUE);

0 commit comments

Comments
 (0)