Skip to content

Commit 6516a63

Browse files
Add external caller example
1 parent 53b769b commit 6516a63

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ subprojects {
2626
ext {
2727
otelVersion = '1.30.1'
2828
otelVersionAlpha = "${otelVersion}-alpha"
29-
javaSDKVersion = '1.30.1'
29+
javaSDKVersion = '1.31.0-SNAPSHOT'
3030
camelVersion = '3.22.1'
3131
jarVersion = '1.0.0'
3232
}
3333

3434
repositories {
35+
mavenLocal()
3536
maven {
3637
url "https://oss.sonatype.org/content/repositories/snapshots/"
3738
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Nexus External Caller Sample
2+
3+
This sample shows how to start and interact with Nexus operations from outside of a workflow.
4+
5+
To run this sample, set up your environment following the instructions in the main [Nexus Sample](../nexus/README.md).
6+
7+
Next, in separate terminal windows:
8+
9+
### Nexus handler worker
10+
11+
```
12+
./gradlew -q execute -PmainClass=io.temporal.samples.nexusexternalcaller.handler.HandlerWorker \
13+
--args="-target-host localhost:7233 -namespace my-target-namespace"
14+
```
15+
16+
### Start Nexus Operation
17+
18+
```
19+
./gradlew -q execute -PmainClass=io.temporal.samples.nexusexternalcaller.caller.CallerStarter \
20+
--args="-target-host localhost:7233 -namespace my-caller-namespace"
21+
```
22+
23+
### Output
24+
25+
which should result in on the caller side:
26+
```
27+
21:18:31.282 { } [main] INFO i.t.s.n.caller.CallerStarter - Execute echo operation: Hello
28+
21:18:31.299 { } [main] INFO i.t.s.n.caller.CallerStarter - Started hello operation with token: eyJ0IjoxLCJucyI6Im15LXRhcmdldC1uYW1lc3BhY2UiLCJ3aWQiOiI3NWU0MTk2Zi05ODdiLTRhYzMtOTdiZS1hMjBiYjViOWZiNDUifQ
29+
21:18:31.299 { } [main] INFO i.t.s.n.caller.CallerStarter - Waiting for hello operation to complete...
30+
21:18:31.311 { } [main] INFO i.t.s.n.caller.CallerStarter - Hello operation result: Hello Hello 👋
31+
21:18:31.323 { } [main] INFO i.t.s.n.caller.CallerStarter - Operation state: SUCCEEDED
32+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.temporal.samples.nexusexternalcaller.caller;
2+
3+
import io.nexusrpc.OperationException;
4+
import io.nexusrpc.OperationStillRunningException;
5+
import io.nexusrpc.client.FetchOperationResultOptions;
6+
import io.nexusrpc.client.OperationHandle;
7+
import io.nexusrpc.client.ServiceClient;
8+
import io.nexusrpc.client.StartOperationResponse;
9+
import io.temporal.client.TemporalNexusServiceClientOptions;
10+
import io.temporal.client.WorkflowClient;
11+
import io.temporal.samples.nexus.options.ClientOptions;
12+
import io.temporal.samples.nexus.service.NexusService;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import java.time.Duration;
17+
18+
public class CallerStarter {
19+
private static final Logger logger = LoggerFactory.getLogger(CallerStarter.class);
20+
21+
public static void main(String[] args) throws OperationStillRunningException, OperationException {
22+
WorkflowClient client = ClientOptions.getWorkflowClient(args);
23+
24+
ServiceClient<NexusService> serviceClient =
25+
client.newNexusServiceClient(
26+
NexusService.class,
27+
TemporalNexusServiceClientOptions.newBuilder()
28+
.setEndpoint("my-nexus-endpoint-name")
29+
.build());
30+
31+
// Execute a synchronous operation
32+
NexusService.EchoOutput result =
33+
serviceClient.executeOperation(NexusService::echo, new NexusService.EchoInput("Hello"));
34+
logger.info("Execute echo operation: {}", result.getMessage());
35+
// Start an asynchronous operation
36+
StartOperationResponse<NexusService.HelloOutput> response =
37+
serviceClient.startOperation(
38+
NexusService::hello, new NexusService.HelloInput("Hello", NexusService.Language.EN));
39+
if (!(response instanceof StartOperationResponse.Async)) {
40+
throw new IllegalStateException("Expected an asynchronous operation response");
41+
}
42+
OperationHandle<NexusService.HelloOutput> handle =
43+
((StartOperationResponse.Async<NexusService.HelloOutput>) response).getHandle();
44+
logger.info("Started hello operation with token: {}", handle.getOperationToken());
45+
// Wait for the operation to complete
46+
logger.info("Waiting for hello operation to complete...");
47+
NexusService.HelloOutput helloResult =
48+
handle.fetchResult(
49+
FetchOperationResultOptions.newBuilder().setTimeout(Duration.ofSeconds(5)).build());
50+
logger.info("Hello operation result: {}", helloResult.getMessage());
51+
// We can also get the status of an operation
52+
logger.info("Operation state: {}", handle.getInfo().getState());
53+
}
54+
}

core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/HandlerWorker.java renamed to core/src/main/java/io/temporal/samples/nexusexternalcaller/handler/HandlerWorker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package io.temporal.samples.nexusmultipleargs.handler;
1+
package io.temporal.samples.nexusexternalcaller.handler;
22

33
import io.temporal.client.WorkflowClient;
4+
import io.temporal.samples.nexus.handler.HelloHandlerWorkflowImpl;
5+
import io.temporal.samples.nexus.handler.NexusServiceImpl;
46
import io.temporal.samples.nexus.options.ClientOptions;
57
import io.temporal.worker.Worker;
68
import io.temporal.worker.WorkerFactory;

0 commit comments

Comments
 (0)