Skip to content

Commit fffb0c8

Browse files
Evanthxjsundai
andauthored
Updating Java Nexus docs with testing samples (#4347)
* Updating Java Nexus docs with testing samples * Adding snippets --------- Co-authored-by: Jwahir Sundai <jwahir.sundai@temporal.io>
1 parent 47b489b commit fffb0c8

2 files changed

Lines changed: 260 additions & 34 deletions

File tree

docs/develop/java/nexus/feature-guide.mdx

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ In a polyglot environment, that is where more than one language and SDK is being
9797
This example uses Java classes serialized into JSON.
9898

9999
<!--SNIPSTART samples-java-nexus-service-->
100-
[core/src/main/java/io/temporal/samples/nexus/service/NexusService.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/service/NexusService.java)
100+
[core/src/main/java/io/temporal/samples/nexus/service/SampleNexusService.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/service/SampleNexusService.java)
101101
```java
102102
@Service
103-
public interface NexusService {
103+
public interface SampleNexusService {
104104
enum Language {
105105
EN,
106106
FR,
@@ -204,23 +204,23 @@ Use `Nexus.getOperationContext().getWorkflowClient(ctx)` to get the Temporal Cli
204204
Implementations can also make other calls, but handlers should be reliable to avoid tripping the [circuit breaker](/nexus/operations#circuit-breaking).
205205

206206
{/* SNIPSTART samples-java-nexus-handler {"selectedLines": ["1-16", "43"]} */}
207-
[core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java)
207+
[core/src/main/java/io/temporal/samples/nexus/handler/SampleNexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/handler/SampleNexusServiceImpl.java)
208208
```java
209209
// To create a service implementation, annotate the class with @ServiceImpl and provide the
210210
// interface that the service implements. The service implementation class should have methods that
211211
// return OperationHandler that correspond to the operations defined in the service interface.
212-
@ServiceImpl(service = NexusService.class)
213-
public class NexusServiceImpl {
212+
@ServiceImpl(service = SampleNexusService.class)
213+
public class SampleNexusServiceImpl {
214214
@OperationImpl
215-
public OperationHandler<NexusService.EchoInput, NexusService.EchoOutput> echo() {
215+
public OperationHandler<SampleNexusService.EchoInput, SampleNexusService.EchoOutput> echo() {
216216
// OperationHandler.sync is a meant for exposing simple RPC handlers.
217217
return OperationHandler.sync(
218218
// The method is for making arbitrary short calls to other services or databases, or
219219
// perform simple computations such as this one. Users can also access a workflow client by
220220
// calling
221221
// Nexus.getOperationContext().getWorkflowClient(ctx) to make arbitrary calls such as
222222
// signaling, querying, or listing workflows.
223-
(ctx, details, input) -> new NexusService.EchoOutput(input.getMessage()));
223+
(ctx, details, input) -> new SampleNexusService.EchoOutput(input.getMessage()));
224224
}
225225
// ...
226226
}
@@ -238,16 +238,16 @@ All calls must complete within the [Nexus request timeout](/cloud/limits#nexus-o
238238
Use the `WorkflowRunOperation.fromWorkflowMethod` method, which is the easiest way to expose a Workflow as an operation.
239239

240240
<!--SNIPSTART samples-java-nexus-handler {"selectedLines": ["1-5", "18-40"]}-->
241-
[core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java)
241+
[core/src/main/java/io/temporal/samples/nexus/handler/SampleNexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexus/handler/SampleNexusServiceImpl.java)
242242
```java
243243
// To create a service implementation, annotate the class with @ServiceImpl and provide the
244244
// interface that the service implements. The service implementation class should have methods that
245245
// return OperationHandler that correspond to the operations defined in the service interface.
246-
@ServiceImpl(service = NexusService.class)
247-
public class NexusServiceImpl {
246+
@ServiceImpl(service = SampleNexusService.class)
247+
public class SampleNexusServiceImpl {
248248
// ...
249249
@OperationImpl
250-
public OperationHandler<NexusService.HelloInput, NexusService.HelloOutput> hello() {
250+
public OperationHandler<SampleNexusService.HelloInput, SampleNexusService.HelloOutput> hello() {
251251
// Use the WorkflowRunOperation.fromWorkflowMethod constructor, which is the easiest
252252
// way to expose a workflow as an operation. To expose a workflow with a different input
253253
// parameters then the operation or from an untyped stub, use the
@@ -285,27 +285,27 @@ Workflow IDs should typically be business-meaningful IDs and are used to dedupe
285285
A Nexus Operation can only take one input parameter. If you want a Nexus Operation to start a Workflow that takes multiple arguments use the `WorkflowRunOperation.fromWorkflowHandle` method.
286286
287287
<!--SNIPSTART samples-java-nexus-handler-multiargs-->
288-
[core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/NexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/NexusServiceImpl.java)
288+
[core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/SampleNexusServiceImpl.java](https://github.com/temporalio/samples-java/blob/nexus-snip-sync/core/src/main/java/io/temporal/samples/nexusmultipleargs/handler/SampleNexusServiceImpl.java)
289289
```java
290290
// To create a service implementation, annotate the class with @ServiceImpl and provide the
291291
// interface that the service implements. The service implementation class should have methods that
292292
// return OperationHandler that correspond to the operations defined in the service interface.
293-
@ServiceImpl(service = NexusService.class)
294-
public class NexusServiceImpl {
293+
@ServiceImpl(service = SampleNexusService.class)
294+
public class SampleNexusServiceImpl {
295295
@OperationImpl
296-
public OperationHandler<NexusService.EchoInput, NexusService.EchoOutput> echo() {
296+
public OperationHandler<SampleNexusService.EchoInput, SampleNexusService.EchoOutput> echo() {
297297
// OperationHandler.sync is a meant for exposing simple RPC handlers.
298298
return OperationHandler.sync(
299299
// The method is for making arbitrary short calls to other services or databases, or
300300
// perform simple computations such as this one. Users can also access a workflow client by
301301
// calling
302302
// Nexus.getOperationContext().getWorkflowClient(ctx) to make arbitrary calls such as
303303
// signaling, querying, or listing workflows.
304-
(ctx, details, input) -> new NexusService.EchoOutput(input.getMessage()));
304+
(ctx, details, input) -> new SampleNexusService.EchoOutput(input.getMessage()));
305305
}
306306

307307
@OperationImpl
308-
public OperationHandler<NexusService.HelloInput, NexusService.HelloOutput> hello() {
308+
public OperationHandler<SampleNexusService.HelloInput, SampleNexusService.HelloOutput> hello() {
309309
// If the operation input parameters are different from the workflow input parameters,
310310
// use the WorkflowRunOperation.fromWorkflowHandler constructor and the appropriate constructor
311311
// method on WorkflowHandle to map the Nexus input to the workflow parameters.
@@ -363,7 +363,7 @@ public class HandlerWorker {
363363

364364
Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME);
365365
worker.registerWorkflowImplementationTypes(HelloHandlerWorkflowImpl.class);
366-
worker.registerNexusServiceImplementation(new NexusServiceImpl());
366+
worker.registerNexusServiceImplementation(new SampleNexusServiceImpl());
367367

368368
factory.start();
369369
}
@@ -380,16 +380,16 @@ Import the Service API package that has the necessary service and operation name
380380
```java
381381
package io.temporal.samples.nexus.caller;
382382

383-
import io.temporal.samples.nexus.service.NexusService;
383+
import io.temporal.samples.nexus.service.SampleNexusService;
384384
import io.temporal.workflow.NexusOperationOptions;
385385
import io.temporal.workflow.NexusServiceOptions;
386386
import io.temporal.workflow.Workflow;
387387
import java.time.Duration;
388388

389389
public class EchoCallerWorkflowImpl implements EchoCallerWorkflow {
390-
NexusService nexusService =
390+
SampleNexusService sampleNexusService =
391391
Workflow.newNexusServiceStub(
392-
NexusService.class,
392+
SampleNexusService.class,
393393
NexusServiceOptions.newBuilder()
394394
.setOperationOptions(
395395
NexusOperationOptions.newBuilder()
@@ -399,7 +399,7 @@ public class EchoCallerWorkflowImpl implements EchoCallerWorkflow {
399399

400400
@Override
401401
public String echo(String message) {
402-
return nexusService.echo(new NexusService.EchoInput(message)).getMessage();
402+
return sampleNexusService.echo(new SampleNexusService.EchoInput(message)).getMessage();
403403
}
404404
}
405405
```
@@ -410,17 +410,17 @@ public class EchoCallerWorkflowImpl implements EchoCallerWorkflow {
410410
```java
411411
package io.temporal.samples.nexus.caller;
412412

413-
import io.temporal.samples.nexus.service.NexusService;
413+
import io.temporal.samples.nexus.service.SampleNexusService;
414414
import io.temporal.workflow.NexusOperationHandle;
415415
import io.temporal.workflow.NexusOperationOptions;
416416
import io.temporal.workflow.NexusServiceOptions;
417417
import io.temporal.workflow.Workflow;
418418
import java.time.Duration;
419419

420420
public class HelloCallerWorkflowImpl implements HelloCallerWorkflow {
421-
NexusService nexusService =
421+
SampleNexusService sampleNexusService =
422422
Workflow.newNexusServiceStub(
423-
NexusService.class,
423+
SampleNexusService.class,
424424
NexusServiceOptions.newBuilder()
425425
.setOperationOptions(
426426
NexusOperationOptions.newBuilder()
@@ -429,10 +429,10 @@ public class HelloCallerWorkflowImpl implements HelloCallerWorkflow {
429429
.build());
430430

431431
@Override
432-
public String hello(String message, NexusService.Language language) {
433-
NexusOperationHandle<NexusService.HelloOutput> handle =
432+
public String hello(String message, SampleNexusService.Language language) {
433+
NexusOperationHandle<SampleNexusService.HelloOutput> handle =
434434
Workflow.startNexusOperation(
435-
nexusService::hello, new NexusService.HelloInput(message, language));
435+
sampleNexusService::hello, new SampleNexusService.HelloInput(message, language));
436436
// Optionally wait for the operation to be started. NexusOperationExecution will contain the
437437
// operation token in case this operation is asynchronous.
438438
handle.getExecution().get();
@@ -472,7 +472,7 @@ public class CallerWorker {
472472
WorkflowImplementationOptions.newBuilder()
473473
.setNexusServiceOptions(
474474
Collections.singletonMap(
475-
"NexusService",
475+
"SampleNexusService",
476476
NexusServiceOptions.newBuilder().setEndpoint("my-nexus-endpoint-name").build()))
477477
.build(),
478478
EchoCallerWorkflowImpl.class,
@@ -497,7 +497,7 @@ import io.temporal.api.common.v1.WorkflowExecution;
497497
import io.temporal.client.WorkflowClient;
498498
import io.temporal.client.WorkflowOptions;
499499
import io.temporal.samples.nexus.options.ClientOptions;
500-
import io.temporal.samples.nexus.service.NexusService;
500+
import io.temporal.samples.nexus.service.SampleNexusService;
501501
import org.slf4j.Logger;
502502
import org.slf4j.LoggerFactory;
503503

@@ -519,12 +519,12 @@ public class CallerStarter {
519519
logger.info("Workflow result: {}", echoWorkflow.echo("Nexus Echo 👋"));
520520
HelloCallerWorkflow helloWorkflow =
521521
client.newWorkflowStub(HelloCallerWorkflow.class, workflowOptions);
522-
execution = WorkflowClient.start(helloWorkflow::hello, "Nexus", NexusService.Language.EN);
522+
execution = WorkflowClient.start(helloWorkflow::hello, "Nexus", SampleNexusService.Language.EN);
523523
logger.info(
524524
"Started HelloCallerWorkflow workflowId: {} runId: {}",
525525
execution.getWorkflowId(),
526526
execution.getRunId());
527-
logger.info("Workflow result: {}", helloWorkflow.hello("Nexus", NexusService.Language.ES));
527+
logger.info("Workflow result: {}", helloWorkflow.hello("Nexus", SampleNexusService.Language.ES));
528528
}
529529
}
530530
```

0 commit comments

Comments
 (0)