Skip to content

Commit b832d75

Browse files
committed
Adds apikey example
1 parent fd5bd1f commit b832d75

5 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.temporal.samples.apikey;
2+
3+
import io.temporal.client.WorkflowClient;
4+
import io.temporal.client.WorkflowClientOptions;
5+
import io.temporal.serviceclient.WorkflowServiceStubs;
6+
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
7+
import io.temporal.worker.Worker;
8+
import io.temporal.worker.WorkerFactory;
9+
10+
public class ApiKeyWorker {
11+
static final String TASK_QUEUE = "MyTaskQueue";
12+
13+
public static void main(String[] args) throws Exception {
14+
// For temporal cloud this would likely be ${namespace}.tmprl.cloud:7233
15+
String targetEndpoint = System.getenv("TEMPORAL_ENDPOINT");
16+
// Your registered namespace.
17+
String namespace = System.getenv("TEMPORAL_NAMESPACE");
18+
// Your API Key
19+
String apiKey = System.getenv("TEMPORAL_API_KEY");
20+
21+
if (targetEndpoint == null || namespace == null || apiKey == null) {
22+
throw new IllegalArgumentException(
23+
"TEMPORAL_ENDPOINT, TEMPORAL_NAMESPACE, and TEMPORAL_API_KEY environment variables must be set");
24+
}
25+
26+
// Create API Key enabled client
27+
WorkflowServiceStubs service =
28+
WorkflowServiceStubs.newServiceStubs(
29+
WorkflowServiceStubsOptions.newBuilder()
30+
.setTarget(targetEndpoint)
31+
.setEnableHttps(true)
32+
.addApiKey(() -> apiKey)
33+
.build());
34+
35+
// Now setup and start workflow worker
36+
WorkflowClient client =
37+
WorkflowClient.newInstance(
38+
service, WorkflowClientOptions.newBuilder().setNamespace(namespace).build());
39+
40+
// worker factory that can be used to create workers for specific task queues
41+
WorkerFactory factory = WorkerFactory.newInstance(client);
42+
43+
/*
44+
* Define the workflow worker. Workflow workers listen to a defined task queue and process
45+
* workflows and activities.
46+
*/
47+
Worker worker = factory.newWorker(TASK_QUEUE);
48+
49+
/*
50+
* Register our workflow implementation with the worker.
51+
* Workflow implementations must be known to the worker at runtime in
52+
* order to dispatch workflow tasks.
53+
*/
54+
worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class);
55+
56+
/*
57+
* Start all the workers registered for a specific task queue.
58+
* The started workers then start polling for workflows and activities.
59+
*/
60+
factory.start();
61+
62+
System.out.println("Worker started. Press Ctrl+C to exit.");
63+
// Keep the worker running
64+
Thread.currentThread().join();
65+
}
66+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.temporal.samples.apikey;
2+
3+
import io.temporal.workflow.WorkflowInterface;
4+
import io.temporal.workflow.WorkflowMethod;
5+
6+
@WorkflowInterface
7+
public interface MyWorkflow {
8+
@WorkflowMethod
9+
String execute();
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.temporal.samples.apikey;
2+
3+
public class MyWorkflowImpl implements MyWorkflow {
4+
@Override
5+
public String execute() {
6+
return "done";
7+
}
8+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Workflow execution with API Key
2+
3+
This example shows how to secure your Temporal application with API Key authentication.
4+
This is required to connect with Temporal Cloud or any production Temporal deployment that uses API Key authentication.
5+
6+
## Prerequisites
7+
8+
1. A Temporal Cloud account
9+
2. A namespace in Temporal Cloud
10+
3. An API Key for your namespace
11+
12+
## Getting your API Key
13+
14+
1. Log in to your Temporal Cloud account
15+
2. Navigate to your namespace
16+
3. Go to Namespace Settings > API Keys
17+
4. Click "Create API Key"
18+
5. Give your API Key a name and select the appropriate permissions
19+
6. Copy the API Key value (you won't be able to see it again)
20+
21+
## Export env variables
22+
23+
Before running the example you need to export the following env variables:
24+
25+
```bash
26+
# Your Temporal Cloud endpoint (e.g., us-east-1.aws.api.temporal.io:7233)
27+
export TEMPORAL_ENDPOINT="us-east-1.aws.api.temporal.io:7233"
28+
29+
# Your Temporal Cloud namespace
30+
export TEMPORAL_NAMESPACE="your-namespace"
31+
32+
# Your API Key from Temporal Cloud
33+
export TEMPORAL_API_KEY="your-api-key"
34+
```
35+
36+
## Running this sample
37+
38+
This sample consists of two components that need to be run in separate terminals:
39+
40+
1. First, start the worker:
41+
```bash
42+
./gradlew -q execute -PmainClass=io.temporal.samples.apikey.ApiKeyWorker
43+
```
44+
45+
2. Then, in a new terminal, run the starter:
46+
```bash
47+
./gradlew -q execute -PmainClass=io.temporal.samples.apikey.Starter
48+
```
49+
50+
## Expected result
51+
52+
When running the worker, you should see:
53+
```text
54+
[main] INFO i.t.s.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=us-east-1.aws.api.temporal.io:7233}}
55+
[main] INFO io.temporal.internal.worker.Poller - start: Poller{name=Workflow Poller taskQueue="MyTaskQueue", namespace="your-namespace"}
56+
Worker started. Press Ctrl+C to exit.
57+
```
58+
59+
When running the starter, you should see:
60+
```text
61+
[main] INFO i.t.s.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=us-east-1.aws.api.temporal.io:7233}}
62+
[main] INFO io.temporal.internal.worker.Poller - start: Poller{name=Workflow Poller taskQueue="MyTaskQueue", namespace="your-namespace"}
63+
done
64+
```
65+
66+
## Troubleshooting
67+
68+
If you encounter any issues:
69+
70+
1. Verify your environment variables are set correctly:
71+
```bash
72+
echo $TEMPORAL_ENDPOINT
73+
echo $TEMPORAL_NAMESPACE
74+
echo $TEMPORAL_API_KEY
75+
```
76+
77+
2. Check that your API Key has the correct permissions for your namespace
78+
79+
3. Ensure your namespace is active and accessible
80+
81+
4. If you get connection errors, verify your endpoint is correct and accessible from your network
82+
83+
5. Make sure you're running the commands from the correct directory (where the `gradlew` script is located)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.temporal.samples.apikey;
2+
3+
import io.temporal.client.WorkflowClient;
4+
import io.temporal.client.WorkflowClientOptions;
5+
import io.temporal.client.WorkflowOptions;
6+
import io.temporal.serviceclient.WorkflowServiceStubs;
7+
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
8+
import io.temporal.worker.Worker;
9+
import io.temporal.worker.WorkerFactory;
10+
11+
public class Starter {
12+
13+
static final String TASK_QUEUE = "MyTaskQueue";
14+
static final String WORKFLOW_ID = "HelloAPIKeyWorkflow";
15+
16+
public static void main(String[] args) throws Exception {
17+
// For temporal cloud this would likely be ${namespace}.tmprl.cloud:7233
18+
String targetEndpoint = System.getenv("TEMPORAL_ENDPOINT");
19+
// Your registered namespace.
20+
String namespace = System.getenv("TEMPORAL_NAMESPACE");
21+
// Your API Key
22+
String apiKey = System.getenv("TEMPORAL_API_KEY");
23+
24+
if (targetEndpoint == null || namespace == null || apiKey == null) {
25+
throw new IllegalArgumentException(
26+
"TEMPORAL_ENDPOINT, TEMPORAL_NAMESPACE, and TEMPORAL_API_KEY environment variables must be set");
27+
}
28+
29+
// Create API Key enabled client
30+
WorkflowServiceStubs service =
31+
WorkflowServiceStubs.newServiceStubs(
32+
WorkflowServiceStubsOptions.newBuilder()
33+
.setTarget(targetEndpoint)
34+
.setEnableHttps(true)
35+
.addApiKey(() -> apiKey)
36+
.build());
37+
38+
WorkflowClient client =
39+
WorkflowClient.newInstance(
40+
service, WorkflowClientOptions.newBuilder().setNamespace(namespace).build());
41+
42+
WorkerFactory factory = WorkerFactory.newInstance(client);
43+
44+
Worker worker = factory.newWorker(TASK_QUEUE);
45+
46+
worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class);
47+
48+
factory.start();
49+
50+
// Create the workflow client stub. It is used to start our workflow execution.
51+
MyWorkflow workflow =
52+
client.newWorkflowStub(
53+
MyWorkflow.class,
54+
WorkflowOptions.newBuilder()
55+
.setWorkflowId(WORKFLOW_ID)
56+
.setTaskQueue(TASK_QUEUE)
57+
.build());
58+
59+
String greeting = workflow.execute();
60+
61+
// Display workflow execution results
62+
System.out.println(greeting);
63+
System.exit(0);
64+
}
65+
}

0 commit comments

Comments
 (0)