diff --git a/build.gradle b/build.gradle index a7ece2dce..dec7d6401 100644 --- a/build.gradle +++ b/build.gradle @@ -26,12 +26,13 @@ subprojects { ext { otelVersion = '1.30.1' otelVersionAlpha = "${otelVersion}-alpha" - javaSDKVersion = '1.30.1' + javaSDKVersion = '1.31.0-SNAPSHOT' camelVersion = '3.22.1' jarVersion = '1.0.0' } repositories { + mavenLocal() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } diff --git a/core/src/main/java/io/temporal/samples/nexusexternalcaller/README.MD b/core/src/main/java/io/temporal/samples/nexusexternalcaller/README.MD new file mode 100644 index 000000000..6b584fe43 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/nexusexternalcaller/README.MD @@ -0,0 +1,32 @@ +# Nexus External Caller Sample + +This sample shows how to start and interact with Nexus operations from outside of a workflow. + +To run this sample, set up your environment following the instructions in the main [Nexus Sample](../nexus/README.md). + +Next, in separate terminal windows: + +### Nexus handler worker + +``` +./gradlew -q execute -PmainClass=io.temporal.samples.nexusexternalcaller.handler.HandlerWorker \ + --args="-target-host localhost:7233 -namespace my-target-namespace" +``` + +### Start Nexus Operation + +``` +./gradlew -q execute -PmainClass=io.temporal.samples.nexusexternalcaller.caller.CallerStarter \ + --args="-target-host localhost:7233 -namespace my-caller-namespace" +``` + +### Output + +which should result in on the caller side: +``` +21:18:31.282 { } [main] INFO i.t.s.n.caller.CallerStarter - Execute echo operation: Hello +21:18:31.299 { } [main] INFO i.t.s.n.caller.CallerStarter - Started hello operation with token: eyJ0IjoxLCJucyI6Im15LXRhcmdldC1uYW1lc3BhY2UiLCJ3aWQiOiI3NWU0MTk2Zi05ODdiLTRhYzMtOTdiZS1hMjBiYjViOWZiNDUifQ +21:18:31.299 { } [main] INFO i.t.s.n.caller.CallerStarter - Waiting for hello operation to complete... +21:18:31.311 { } [main] INFO i.t.s.n.caller.CallerStarter - Hello operation result: Hello Hello 👋 +21:18:31.323 { } [main] INFO i.t.s.n.caller.CallerStarter - Operation state: SUCCEEDED +``` diff --git a/core/src/main/java/io/temporal/samples/nexusexternalcaller/caller/CallerStarter.java b/core/src/main/java/io/temporal/samples/nexusexternalcaller/caller/CallerStarter.java new file mode 100644 index 000000000..279642953 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/nexusexternalcaller/caller/CallerStarter.java @@ -0,0 +1,54 @@ +package io.temporal.samples.nexusexternalcaller.caller; + +import io.nexusrpc.OperationException; +import io.nexusrpc.OperationStillRunningException; +import io.nexusrpc.client.FetchOperationResultOptions; +import io.nexusrpc.client.OperationHandle; +import io.nexusrpc.client.ServiceClient; +import io.nexusrpc.client.StartOperationResponse; +import io.temporal.client.TemporalNexusServiceClientOptions; +import io.temporal.client.WorkflowClient; +import io.temporal.samples.nexus.options.ClientOptions; +import io.temporal.samples.nexus.service.NexusService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; + +public class CallerStarter { + private static final Logger logger = LoggerFactory.getLogger(CallerStarter.class); + + public static void main(String[] args) throws OperationStillRunningException, OperationException { + WorkflowClient client = ClientOptions.getWorkflowClient(args); + + ServiceClient serviceClient = + client.newNexusServiceClient( + NexusService.class, + TemporalNexusServiceClientOptions.newBuilder() + .setEndpoint("my-nexus-endpoint-name") + .build()); + + // Execute a synchronous operation + NexusService.EchoOutput result = + serviceClient.executeOperation(NexusService::echo, new NexusService.EchoInput("Hello")); + logger.info("Execute echo operation: {}", result.getMessage()); + // Start an asynchronous operation + StartOperationResponse response = + serviceClient.startOperation( + NexusService::hello, new NexusService.HelloInput("Hello", NexusService.Language.EN)); + if (!(response instanceof StartOperationResponse.Async)) { + throw new IllegalStateException("Expected an asynchronous operation response"); + } + OperationHandle handle = + ((StartOperationResponse.Async) response).getHandle(); + logger.info("Started hello operation with token: {}", handle.getOperationToken()); + // Wait for the operation to complete + logger.info("Waiting for hello operation to complete..."); + NexusService.HelloOutput helloResult = + handle.fetchResult( + FetchOperationResultOptions.newBuilder().setTimeout(Duration.ofSeconds(5)).build()); + logger.info("Hello operation result: {}", helloResult.getMessage()); + // We can also get the status of an operation + logger.info("Operation state: {}", handle.getInfo().getState()); + } +} diff --git a/core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/HandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusexternalcaller/handler/HandlerWorker.java similarity index 79% rename from core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/HandlerWorker.java rename to core/src/main/java/io/temporal/samples/nexusexternalcaller/handler/HandlerWorker.java index 0ec77889c..220d0ddd9 100644 --- a/core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/HandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusexternalcaller/handler/HandlerWorker.java @@ -1,6 +1,8 @@ -package io.temporal.samples.nexusmultipleargs.handler; +package io.temporal.samples.nexusexternalcaller.handler; import io.temporal.client.WorkflowClient; +import io.temporal.samples.nexus.handler.HelloHandlerWorkflowImpl; +import io.temporal.samples.nexus.handler.NexusServiceImpl; import io.temporal.samples.nexus.options.ClientOptions; import io.temporal.worker.Worker; import io.temporal.worker.WorkerFactory;