Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds cloud worker config #30

Merged
merged 1 commit into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions src/main/java/moneytransferapp/MoneyTransferWorker.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,62 @@
package moneytransferapp;

// @@@SNIPSTART money-transfer-project-template-java-worker-import
import java.lang.System;
import java.io.FileInputStream;
import java.io.InputStream;
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowClientOptions;
import io.temporal.serviceclient.SimpleSslContextBuilder;
import io.temporal.serviceclient.WorkflowServiceStubs;
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
import io.temporal.worker.Worker;
import io.temporal.worker.WorkerFactory;
import java.io.IOException;
// @@@SNIPEND

// @@@SNIPSTART money-transfer-project-template-java-worker
public class MoneyTransferWorker {
public static void main(String[] args) throws IOException {
// @@@SNIPSTART money-transfer-project-template-java-worker-certs
// Get the key and certificate from your environment or local machine
String clientCertFile = System.getenv("TEMPORAL_MTLS_TLS_CERT");
String clientCertPrivateKey = System.getenv("TEMPORAL_MTLS_TLS_KEY");

// Open the key and certificate as Input Streams
InputStream clientCertInputStream = new FileInputStream(clientCertFile);
InputStream clientKeyInputStream = new FileInputStream(clientCertPrivateKey);

// Generate the sslContext using the Client Cert and Key
SslContext sslContext = SimpleSslContextBuilder.forPKCS8(clientCertInputStream, clientKeyInputStream).build();
// @@@SNIPEND
// @@@SNIPSTART money-transfer-project-template-java-worker-options
// Specify the host and port of your Temporal Cloud Namespace
// Host and port format: namespace.unique_id.tmprl.cloud:port
String namespace = System.getenv("TEMPORAL_NAMESPACE");
String hostPort = System.getenv("TEMPORAL_HOST_URL");

public static void main(String[] args) {
// Specify the IP address, port, and SSL Context for the Service Stubs options
WorkflowServiceStubsOptions stubsOptions = WorkflowServiceStubsOptions.newBuilder()
.setSslContext(sslContext)
.setTarget(hostPort)
.build();
// @@@SNIPEND
// Generate the gRPC stubs using the options provided
WorkflowServiceStubs service = WorkflowServiceStubs.newServiceStubs(stubsOptions);
// @@@SNIPSTART money-transfer-project-template-java-worker-client-options
// Specify the namespace in the Client options
WorkflowClientOptions options = WorkflowClientOptions.newBuilder()
.setNamespace(namespace)
.build();

// WorkflowServiceStubs is a gRPC stubs wrapper that talks to the local Docker instance of the Temporal server.
WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
WorkflowClient client = WorkflowClient.newInstance(service);
// Worker factory is used to create Workers that poll specific Task Queues.
WorkflowClient client = WorkflowClient.newInstance(service, options);
// @@@SNIPEND
WorkerFactory factory = WorkerFactory.newInstance(client);

Worker worker = factory.newWorker(Shared.MONEY_TRANSFER_TASK_QUEUE);
// This Worker hosts both Workflow and Activity implementations.
// Workflows are stateful so a type is needed to create instances.

worker.registerWorkflowImplementationTypes(MoneyTransferWorkflowImpl.class);
// Activities are stateless and thread safe so a shared instance is used.
worker.registerActivitiesImplementations(new AccountActivityImpl());
// Start listening to the Task Queue.

factory.start();
}
}
// @@@SNIPEND