Skip to content

Commit 62a35ce

Browse files
Show mapping Nexus operation to multiple input args (#744)
Show mapping Nexus operation to multiple input args
1 parent c4217c9 commit 62a35ce

File tree

12 files changed

+304
-0
lines changed

12 files changed

+304
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ See the README.md file in each main sample directory for cut/paste Gradle comman
155155

156156
- [**Getting Started**](/core/src/main/java/io/temporal/samples/nexus): Demonstrates how to get started with Temporal and Nexus.
157157

158+
- [**Mapping Multiple Arguments**](/core/src/main/java/io/temporal/samples/nexus): Demonstrates how map a Nexus operation to a Workflow that takes multiple arguments.
159+
158160
- [**Cancellation**](/core/src/main/java/io/temporal/samples/nexuscancellation): Demonstrates how to cancel an async Nexus operation.
159161

160162
- [**Context/Header Propagation**](/core/src/main/java/io/temporal/samples/nexuscontextpropagation): Demonstrates how to propagate context through Nexus operation headers.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Nexus Multiple Arguments Sample
2+
3+
This sample shows how to map a Nexus operation to a caller workflow that takes multiple input arguments using [WorkflowRunOperation.fromWorkflowHandle](https://javadoc.io/doc/io.temporal/temporal-sdk/latest/io/temporal/nexus/WorkflowRunOperation.html#fromWorkflowHandle(io.temporal.nexus.WorkflowHandleFactory)).
4+
5+
To run this sample, set up your environment following the instructions in the main [Nexus Sample](../nexus/README.md).
6+
7+
In separate terminal windows:
8+
9+
### Nexus handler worker
10+
11+
```
12+
./gradlew -q execute -PmainClass=io.temporal.samples.nexusmultipleargs.handler.HandlerWorker \
13+
--args="-target-host localhost:7233 -namespace my-target-namespace"
14+
```
15+
16+
### Nexus caller worker
17+
18+
```
19+
./gradlew -q execute -PmainClass=io.temporal.samples.nexusmultipleargs.caller.CallerWorker \
20+
--args="-target-host localhost:7233 -namespace my-caller-namespace"
21+
```
22+
23+
### Start caller workflow
24+
25+
```
26+
./gradlew -q execute -PmainClass=io.temporal.samples.nexusmultipleargs.caller.CallerStarter \
27+
--args="-target-host localhost:7233 -namespace my-caller-namespace"
28+
```
29+
30+
### Output
31+
32+
which should result in:
33+
```
34+
[main] INFO i.t.s.nexus.caller.CallerStarter - Workflow result: Nexus Echo 👋
35+
[main] INFO i.t.s.nexus.caller.CallerStarter - Workflow result: ¡Hola! Nexus 👋
36+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.temporal.samples.nexusmultipleargs.caller;
2+
3+
import io.temporal.api.common.v1.WorkflowExecution;
4+
import io.temporal.client.WorkflowClient;
5+
import io.temporal.client.WorkflowOptions;
6+
import io.temporal.samples.nexus.caller.CallerWorker;
7+
import io.temporal.samples.nexus.caller.EchoCallerWorkflow;
8+
import io.temporal.samples.nexus.caller.HelloCallerWorkflow;
9+
import io.temporal.samples.nexus.options.ClientOptions;
10+
import io.temporal.samples.nexus.service.NexusService;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
public class CallerStarter {
15+
private static final Logger logger = LoggerFactory.getLogger(CallerStarter.class);
16+
17+
public static void main(String[] args) {
18+
WorkflowClient client = ClientOptions.getWorkflowClient(args);
19+
20+
WorkflowOptions workflowOptions =
21+
WorkflowOptions.newBuilder().setTaskQueue(CallerWorker.DEFAULT_TASK_QUEUE_NAME).build();
22+
EchoCallerWorkflow echoWorkflow =
23+
client.newWorkflowStub(EchoCallerWorkflow.class, workflowOptions);
24+
WorkflowExecution execution = WorkflowClient.start(echoWorkflow::echo, "Nexus Echo 👋");
25+
logger.info(
26+
"Started EchoCallerWorkflow workflowId: {} runId: {}",
27+
execution.getWorkflowId(),
28+
execution.getRunId());
29+
logger.info("Workflow result: {}", echoWorkflow.echo("Nexus Echo 👋"));
30+
HelloCallerWorkflow helloWorkflow =
31+
client.newWorkflowStub(HelloCallerWorkflow.class, workflowOptions);
32+
execution = WorkflowClient.start(helloWorkflow::hello, "Nexus", NexusService.Language.EN);
33+
logger.info(
34+
"Started HelloCallerWorkflow workflowId: {} runId: {}",
35+
execution.getWorkflowId(),
36+
execution.getRunId());
37+
logger.info("Workflow result: {}", helloWorkflow.hello("Nexus", NexusService.Language.ES));
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.temporal.samples.nexusmultipleargs.caller;
2+
3+
import io.temporal.client.WorkflowClient;
4+
import io.temporal.samples.nexus.options.ClientOptions;
5+
import io.temporal.worker.Worker;
6+
import io.temporal.worker.WorkerFactory;
7+
import io.temporal.worker.WorkflowImplementationOptions;
8+
import io.temporal.workflow.NexusServiceOptions;
9+
import java.util.Collections;
10+
11+
public class CallerWorker {
12+
public static final String DEFAULT_TASK_QUEUE_NAME = "my-caller-workflow-task-queue";
13+
14+
public static void main(String[] args) {
15+
WorkflowClient client = ClientOptions.getWorkflowClient(args);
16+
17+
WorkerFactory factory = WorkerFactory.newInstance(client);
18+
19+
Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME);
20+
worker.registerWorkflowImplementationTypes(
21+
WorkflowImplementationOptions.newBuilder()
22+
.setNexusServiceOptions(
23+
Collections.singletonMap(
24+
"NexusService",
25+
NexusServiceOptions.newBuilder().setEndpoint("my-nexus-endpoint-name").build()))
26+
.build(),
27+
EchoCallerWorkflowImpl.class,
28+
HelloCallerWorkflowImpl.class);
29+
30+
factory.start();
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.temporal.samples.nexusmultipleargs.caller;
2+
3+
import io.temporal.workflow.WorkflowInterface;
4+
import io.temporal.workflow.WorkflowMethod;
5+
6+
@WorkflowInterface
7+
public interface EchoCallerWorkflow {
8+
@WorkflowMethod
9+
String echo(String message);
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.temporal.samples.nexusmultipleargs.caller;
2+
3+
import io.temporal.samples.nexus.caller.EchoCallerWorkflow;
4+
import io.temporal.samples.nexus.service.NexusService;
5+
import io.temporal.workflow.NexusOperationOptions;
6+
import io.temporal.workflow.NexusServiceOptions;
7+
import io.temporal.workflow.Workflow;
8+
import java.time.Duration;
9+
10+
public class EchoCallerWorkflowImpl implements EchoCallerWorkflow {
11+
NexusService nexusService =
12+
Workflow.newNexusServiceStub(
13+
NexusService.class,
14+
NexusServiceOptions.newBuilder()
15+
.setOperationOptions(
16+
NexusOperationOptions.newBuilder()
17+
.setScheduleToCloseTimeout(Duration.ofSeconds(10))
18+
.build())
19+
.build());
20+
21+
@Override
22+
public String echo(String message) {
23+
return nexusService.echo(new NexusService.EchoInput(message)).getMessage();
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.temporal.samples.nexusmultipleargs.caller;
2+
3+
import io.temporal.samples.nexus.service.NexusService;
4+
import io.temporal.workflow.WorkflowInterface;
5+
import io.temporal.workflow.WorkflowMethod;
6+
7+
@WorkflowInterface
8+
public interface HelloCallerWorkflow {
9+
@WorkflowMethod
10+
String hello(String message, NexusService.Language language);
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.temporal.samples.nexusmultipleargs.caller;
2+
3+
import io.temporal.samples.nexus.caller.HelloCallerWorkflow;
4+
import io.temporal.samples.nexus.service.NexusService;
5+
import io.temporal.workflow.NexusOperationHandle;
6+
import io.temporal.workflow.NexusOperationOptions;
7+
import io.temporal.workflow.NexusServiceOptions;
8+
import io.temporal.workflow.Workflow;
9+
import java.time.Duration;
10+
11+
public class HelloCallerWorkflowImpl implements HelloCallerWorkflow {
12+
NexusService nexusService =
13+
Workflow.newNexusServiceStub(
14+
NexusService.class,
15+
NexusServiceOptions.newBuilder()
16+
.setOperationOptions(
17+
NexusOperationOptions.newBuilder()
18+
.setScheduleToCloseTimeout(Duration.ofSeconds(10))
19+
.build())
20+
.build());
21+
22+
@Override
23+
public String hello(String message, NexusService.Language language) {
24+
NexusOperationHandle<NexusService.HelloOutput> handle =
25+
Workflow.startNexusOperation(
26+
nexusService::hello, new NexusService.HelloInput(message, language));
27+
// Optionally wait for the operation to be started. NexusOperationExecution will contain the
28+
// operation token in case this operation is asynchronous.
29+
handle.getExecution().get();
30+
return handle.getResult().get().getMessage();
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.temporal.samples.nexusmultipleargs.handler;
2+
3+
import io.temporal.client.WorkflowClient;
4+
import io.temporal.samples.nexus.options.ClientOptions;
5+
import io.temporal.worker.Worker;
6+
import io.temporal.worker.WorkerFactory;
7+
8+
public class HandlerWorker {
9+
public static final String DEFAULT_TASK_QUEUE_NAME = "my-handler-task-queue";
10+
11+
public static void main(String[] args) {
12+
WorkflowClient client = ClientOptions.getWorkflowClient(args);
13+
14+
WorkerFactory factory = WorkerFactory.newInstance(client);
15+
16+
Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME);
17+
worker.registerWorkflowImplementationTypes(HelloHandlerWorkflowImpl.class);
18+
worker.registerNexusServiceImplementation(new NexusServiceImpl());
19+
20+
factory.start();
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.temporal.samples.nexusmultipleargs.handler;
2+
3+
import io.temporal.samples.nexus.service.NexusService;
4+
import io.temporal.workflow.WorkflowInterface;
5+
import io.temporal.workflow.WorkflowMethod;
6+
7+
@WorkflowInterface
8+
public interface HelloHandlerWorkflow {
9+
@WorkflowMethod
10+
NexusService.HelloOutput hello(String name, NexusService.Language language);
11+
}

0 commit comments

Comments
 (0)