Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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<NexusService> 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<NexusService.HelloOutput> 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<NexusService.HelloOutput> handle =
((StartOperationResponse.Async<NexusService.HelloOutput>) 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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading