|
| 1 | +package io.temporal.samples.hello; |
| 2 | + |
| 3 | +import io.temporal.activity.ActivityInterface; |
| 4 | +import io.temporal.activity.ActivityOptions; |
| 5 | +import io.temporal.client.WorkflowClient; |
| 6 | +import io.temporal.client.WorkflowOptions; |
| 7 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 8 | +import io.temporal.worker.Worker; |
| 9 | +import io.temporal.worker.WorkerFactory; |
| 10 | +import io.temporal.workflow.Workflow; |
| 11 | +import io.temporal.workflow.WorkflowInterface; |
| 12 | +import io.temporal.workflow.WorkflowMethod; |
| 13 | +import java.time.Duration; |
| 14 | + |
| 15 | +public class Scratchpad { |
| 16 | + static final String TASK_QUEUE = "ScratchpadTaskQueue"; |
| 17 | + static final String WORKFLOW_ID = "ScratchpadWorkflow"; |
| 18 | + |
| 19 | + @WorkflowInterface |
| 20 | + public interface MyWorkflow { |
| 21 | + @WorkflowMethod |
| 22 | + String getGreeting(String name); |
| 23 | + } |
| 24 | + |
| 25 | + @ActivityInterface |
| 26 | + public interface MyActivities { |
| 27 | + String composeGreeting(String greeting, String name); |
| 28 | + } |
| 29 | + |
| 30 | + public static class MyWorkflowImpl implements MyWorkflow { |
| 31 | + |
| 32 | + private final MyActivities activities = |
| 33 | + Workflow.newActivityStub( |
| 34 | + MyActivities.class, |
| 35 | + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build()); |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getGreeting(String name) { |
| 39 | + return activities.composeGreeting("Hello", name); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static class MyActivitiesImpl implements MyActivities { |
| 44 | + @Override |
| 45 | + public String composeGreeting(String greeting, String name) { |
| 46 | + return greeting + " " + name + "!"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static void main(String[] args) { |
| 51 | + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
| 52 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 53 | + WorkerFactory factory = WorkerFactory.newInstance(client); |
| 54 | + Worker worker = factory.newWorker(TASK_QUEUE); |
| 55 | + worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class); |
| 56 | + worker.registerActivitiesImplementations(new MyActivitiesImpl()); |
| 57 | + factory.start(); |
| 58 | + MyWorkflow workflow = |
| 59 | + client.newWorkflowStub( |
| 60 | + MyWorkflow.class, |
| 61 | + WorkflowOptions.newBuilder() |
| 62 | + .setWorkflowId(WORKFLOW_ID) |
| 63 | + .setTaskQueue(TASK_QUEUE) |
| 64 | + .build()); |
| 65 | + |
| 66 | + String greeting = workflow.getGreeting("World"); |
| 67 | + |
| 68 | + System.out.println(greeting); |
| 69 | + System.exit(0); |
| 70 | + } |
| 71 | +} |
0 commit comments