diff --git a/README.md b/README.md index 56c57a1..de943f4 100644 --- a/README.md +++ b/README.md @@ -153,22 +153,26 @@ Demonstrates a complex workflow with parallel execution and stateful logic. ### Temporal Connection -Edit `src/main/resources/application.yml`: +The application uses environment variables for configuration with sensible defaults: ```yaml temporal: - service-address: localhost:7233 # Temporal server address - namespace: default # Temporal namespace + service-address: ${TEMPORAL_ADDRESS:localhost:7233} # Temporal server address + namespace: ${TEMPORAL_NAMESPACE:default} # Temporal namespace + cert-path: ${TEMPORAL_CERT_PATH:} # Path to client certificate (for mTLS) + key-path: ${TEMPORAL_KEY_PATH:} # Path to private key (for mTLS) ``` +**For local development**, no configuration is needed. The application defaults to `localhost:7233` with namespace `default`. + ### Temporal Cloud (mTLS) -For production deployments using Temporal Cloud, the application automatically detects and configures mTLS when certificate paths are provided via environment variables. +For production deployments using Temporal Cloud, configure via environment variables: **Set environment variables:** ```bash -export TEMPORAL_SERVICE_ADDRESS=..tmprl.cloud:7233 +export TEMPORAL_ADDRESS=..tmprl.cloud:7233 export TEMPORAL_NAMESPACE= export TEMPORAL_CERT_PATH=/path/to/client.pem export TEMPORAL_KEY_PATH=/path/to/client.key @@ -181,12 +185,19 @@ export TEMPORAL_KEY_PATH=/path/to/client.key ./gradlew runCrawlerWorker ``` -The application will automatically: -- Detect the presence of `TEMPORAL_CERT_PATH` and `TEMPORAL_KEY_PATH` -- Configure mTLS for Temporal Cloud connection -- Fall back to local connection if certificates are not provided +The workers will automatically use the environment variables and: +- Connect to your Temporal Cloud namespace +- Use mTLS authentication if certificate paths are provided +- Display connection information on startup + +**Example startup output:** -**Note**: For local development, no configuration is needed. The application defaults to `localhost:7233` with namespace `default`. +``` +HTTP Worker started +Temporal Service: my-namespace.a2b3c.tmprl.cloud:7233 +Task Queue: http-task-queue +Press Ctrl+C to stop the worker. +``` ### Worker Configuration diff --git a/src/main/java/com/example/temporal/workflows/crawler/CrawlerWorker.java b/src/main/java/com/example/temporal/workflows/crawler/CrawlerWorker.java index 4cbd24f..335e61e 100644 --- a/src/main/java/com/example/temporal/workflows/crawler/CrawlerWorker.java +++ b/src/main/java/com/example/temporal/workflows/crawler/CrawlerWorker.java @@ -1,6 +1,7 @@ package com.example.temporal.workflows.crawler; import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowClientOptions; import io.temporal.serviceclient.WorkflowServiceStubs; import io.temporal.serviceclient.WorkflowServiceStubsOptions; import io.temporal.worker.Worker; @@ -18,8 +19,10 @@ public class CrawlerWorker { public static final String TASK_QUEUE = "crawler-task-queue"; - private static final String TEMPORAL_SERVICE_ADDRESS = "localhost:7233"; - private static final String NAMESPACE = "default"; + private static final String TEMPORAL_SERVICE_ADDRESS = + System.getenv().getOrDefault("TEMPORAL_ADDRESS", "localhost:7233"); + private static final String NAMESPACE = + System.getenv().getOrDefault("TEMPORAL_NAMESPACE", "default"); private static final int MAX_CONCURRENT_ACTIVITIES = 16; public static void main(String[] args) { @@ -28,7 +31,9 @@ public static void main(String[] args) { WorkflowServiceStubsOptions.newBuilder().setTarget(TEMPORAL_SERVICE_ADDRESS).build(); WorkflowServiceStubs service = WorkflowServiceStubs.newServiceStubs(options); - WorkflowClient client = WorkflowClient.newInstance(service); + WorkflowClientOptions clientOptions = + WorkflowClientOptions.newBuilder().setNamespace(NAMESPACE).build(); + WorkflowClient client = WorkflowClient.newInstance(service, clientOptions); // Create worker factory WorkerFactory factory = WorkerFactory.newInstance(client); @@ -50,7 +55,10 @@ public static void main(String[] args) { // Start the worker factory.start(); - System.out.println("Crawler Worker started. Polling task queue: " + TASK_QUEUE); + System.out.println("Crawler Worker started"); + System.out.println("Temporal Service: " + TEMPORAL_SERVICE_ADDRESS); + System.out.println("Namespace: " + NAMESPACE); + System.out.println("Task Queue: " + TASK_QUEUE); System.out.println("Max concurrent activities: " + MAX_CONCURRENT_ACTIVITIES); System.out.println("Press Ctrl+C to stop the worker."); } diff --git a/src/main/java/com/example/temporal/workflows/http/HttpWorker.java b/src/main/java/com/example/temporal/workflows/http/HttpWorker.java index 914e73e..08292ce 100644 --- a/src/main/java/com/example/temporal/workflows/http/HttpWorker.java +++ b/src/main/java/com/example/temporal/workflows/http/HttpWorker.java @@ -17,8 +17,8 @@ public class HttpWorker { public static final String TASK_QUEUE = "http-task-queue"; - private static final String TEMPORAL_SERVICE_ADDRESS = "localhost:7233"; - private static final String NAMESPACE = "default"; + private static final String TEMPORAL_SERVICE_ADDRESS = + System.getenv().getOrDefault("TEMPORAL_ADDRESS", "localhost:7233"); public static void main(String[] args) { // Create connection to Temporal service @@ -43,7 +43,9 @@ public static void main(String[] args) { // Start the worker factory.start(); - System.out.println("HTTP Worker started. Polling task queue: " + TASK_QUEUE); + System.out.println("HTTP Worker started"); + System.out.println("Temporal Service: " + TEMPORAL_SERVICE_ADDRESS); + System.out.println("Task Queue: " + TASK_QUEUE); System.out.println("Press Ctrl+C to stop the worker."); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5f18393..5212490 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,5 +1,5 @@ temporal: - service-address: ${TEMPORAL_SERVICE_ADDRESS:localhost:7233} + service-address: ${TEMPORAL_ADDRESS:localhost:7233} namespace: ${TEMPORAL_NAMESPACE:default} cert-path: ${TEMPORAL_CERT_PATH:} key-path: ${TEMPORAL_KEY_PATH:} diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index 337a38a..c72864a 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -1,6 +1,6 @@ temporal: - service-address: localhost:7233 - namespace: test + service-address: ${TEMPORAL_ADDRESS:localhost:7233} + namespace: ${TEMPORAL_NAMESPACE:test} spring: application: