|
| 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 | +} |
0 commit comments