|
| 1 | +package io.temporal.samples.hello; |
| 2 | + |
| 3 | +import io.temporal.activity.ActivityInterface; |
| 4 | +import io.temporal.activity.ActivityMethod; |
| 5 | +import io.temporal.client.ActivityClient; |
| 6 | +import io.temporal.client.ActivityClientOptions; |
| 7 | +import io.temporal.client.StartActivityOptions; |
| 8 | +import io.temporal.client.WorkflowClient; |
| 9 | +import io.temporal.envconfig.ClientConfigProfile; |
| 10 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 11 | +import io.temporal.worker.Worker; |
| 12 | +import io.temporal.worker.WorkerFactory; |
| 13 | +import java.io.IOException; |
| 14 | +import java.time.Duration; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +/** |
| 19 | + * Sample Temporal application that executes a Standalone Activity — an Activity that runs |
| 20 | + * independently, without being orchestrated by a Workflow. Requires a local instance of the |
| 21 | + * Temporal service to be running. |
| 22 | + * |
| 23 | + * <p>Unlike regular Activities, a Standalone Activity is started directly from a Temporal Client |
| 24 | + * using {@link ActivityClient}, not from inside a Workflow Definition. Writing the Activity and |
| 25 | + * registering it with the Worker is identical in both cases. |
| 26 | + */ |
| 27 | +public class HelloStandaloneActivity { |
| 28 | + |
| 29 | + static final String TASK_QUEUE = "HelloStandaloneActivityTaskQueue"; |
| 30 | + static final String ACTIVITY_ID = "hello-standalone-activity-id"; |
| 31 | + |
| 32 | + /** |
| 33 | + * Activity interface. Writing a Standalone Activity is identical to writing an Activity |
| 34 | + * orchestrated by a Workflow — the same Activity can be used for both. |
| 35 | + * |
| 36 | + * @see io.temporal.activity.ActivityInterface |
| 37 | + * @see io.temporal.activity.ActivityMethod |
| 38 | + */ |
| 39 | + @ActivityInterface |
| 40 | + public interface GreetingActivities { |
| 41 | + |
| 42 | + // Define your activity method which can be called directly from a Temporal Client. |
| 43 | + @ActivityMethod |
| 44 | + String composeGreeting(String greeting, String name); |
| 45 | + } |
| 46 | + |
| 47 | + /** Simple activity implementation that concatenates two strings. */ |
| 48 | + public static class GreetingActivitiesImpl implements GreetingActivities { |
| 49 | + |
| 50 | + private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class); |
| 51 | + |
| 52 | + @Override |
| 53 | + public String composeGreeting(String greeting, String name) { |
| 54 | + log.info("Composing greeting..."); |
| 55 | + return greeting + ", " + name + "!"; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static void main(String[] args) { |
| 60 | + // Load configuration from environment and files. |
| 61 | + ClientConfigProfile profile; |
| 62 | + try { |
| 63 | + profile = ClientConfigProfile.load(); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new RuntimeException("Failed to load client configuration", e); |
| 66 | + } |
| 67 | + |
| 68 | + // gRPC stubs wrapper that talks to the temporal service. |
| 69 | + WorkflowServiceStubs service = |
| 70 | + WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions()); |
| 71 | + |
| 72 | + // WorkflowClient is required to create a Worker. |
| 73 | + WorkflowClient workflowClient = |
| 74 | + WorkflowClient.newInstance(service, profile.toWorkflowClientOptions()); |
| 75 | + |
| 76 | + // Worker factory that can be used to create workers for specific task queues. |
| 77 | + WorkerFactory factory = WorkerFactory.newInstance(workflowClient); |
| 78 | + |
| 79 | + // Worker that listens on a task queue and hosts activity implementations. |
| 80 | + Worker worker = factory.newWorker(TASK_QUEUE); |
| 81 | + |
| 82 | + // Activities are stateless and thread safe. So a shared instance is used. |
| 83 | + worker.registerActivitiesImplementations(new GreetingActivitiesImpl()); |
| 84 | + |
| 85 | + // Start listening to the activity task queue. |
| 86 | + factory.start(); |
| 87 | + |
| 88 | + // ActivityClient executes standalone activities directly from application code, |
| 89 | + // without a Workflow. |
| 90 | + ActivityClient client = |
| 91 | + ActivityClient.newInstance( |
| 92 | + service, |
| 93 | + ActivityClientOptions.newBuilder().setNamespace(profile.getNamespace()).build()); |
| 94 | + |
| 95 | + // Options specifying the activity ID, task queue, and timeout. |
| 96 | + StartActivityOptions options = |
| 97 | + StartActivityOptions.newBuilder() |
| 98 | + .setId(ACTIVITY_ID) |
| 99 | + .setTaskQueue(TASK_QUEUE) |
| 100 | + .setStartToCloseTimeout(Duration.ofSeconds(10)) |
| 101 | + .build(); |
| 102 | + |
| 103 | + try { |
| 104 | + // Execute the activity and wait for its result. The typed API uses an unbound method |
| 105 | + // reference so the SDK can infer the activity type name and result type automatically. |
| 106 | + String result = |
| 107 | + client.execute( |
| 108 | + GreetingActivities.class, |
| 109 | + GreetingActivities::composeGreeting, |
| 110 | + options, |
| 111 | + "Hello", |
| 112 | + "World"); |
| 113 | + |
| 114 | + System.out.println(result); |
| 115 | + } finally { |
| 116 | + // Shut down the worker before the service so polling threads stop cleanly. |
| 117 | + factory.shutdown(); |
| 118 | + service.shutdown(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments