|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved |
| 3 | + * |
| 4 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 9 | + * use this file except in compliance with the License. A copy of the License is |
| 10 | + * located at |
| 11 | + * |
| 12 | + * http://aws.amazon.com/apache2.0 |
| 13 | + * |
| 14 | + * or in the "license" file accompanying this file. This file is distributed on |
| 15 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | + * express or implied. See the License for the specific language governing |
| 17 | + * permissions and limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.temporal.samples.hello; |
| 21 | + |
| 22 | +import io.temporal.activity.ActivityInterface; |
| 23 | +import io.temporal.activity.ActivityMethod; |
| 24 | +import io.temporal.activity.ActivityOptions; |
| 25 | +import io.temporal.client.WorkflowClient; |
| 26 | +import io.temporal.client.WorkflowOptions; |
| 27 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 28 | +import io.temporal.worker.Worker; |
| 29 | +import io.temporal.worker.WorkerFactory; |
| 30 | +import io.temporal.workflow.Workflow; |
| 31 | +import io.temporal.workflow.WorkflowInterface; |
| 32 | +import io.temporal.workflow.WorkflowMethod; |
| 33 | +import java.time.Duration; |
| 34 | + |
| 35 | +/** |
| 36 | + * Demonstrates activities that extend a common interface. The core idea is that an activity |
| 37 | + * interface annotated with {@literal @}{@link ActivityInterface} enumerates all the methods it |
| 38 | + * inherited and declared and generates an activity for each of them. To avoid collisions in |
| 39 | + * activity names (which are by default just method names) the {@link |
| 40 | + * ActivityInterface#namePrefix()} or {@link ActivityMethod#name()} parameters should be used. |
| 41 | + */ |
| 42 | +public class HelloPolymorphicActivity { |
| 43 | + |
| 44 | + static final String TASK_LIST = "HelloPolymorphicActivity"; |
| 45 | + |
| 46 | + @WorkflowInterface |
| 47 | + public interface GreetingWorkflow { |
| 48 | + @WorkflowMethod |
| 49 | + String getGreeting(String name); |
| 50 | + } |
| 51 | + |
| 52 | + /** Base activity interface. Note that it must not be annotated with @ActivityInterface. */ |
| 53 | + public interface GreetingActivity { |
| 54 | + String composeGreeting(String name); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Activity definition interface. Must redefine the name of the composeGreeting activity to avoid |
| 59 | + * collision. |
| 60 | + */ |
| 61 | + @ActivityInterface(namePrefix = "Hello_") |
| 62 | + public interface HelloActivity extends GreetingActivity { |
| 63 | + @Override |
| 64 | + String composeGreeting(String name); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Activity definition interface. Must redefine the name of the composeGreeting activity to avoid |
| 69 | + * collision. |
| 70 | + */ |
| 71 | + @ActivityInterface(namePrefix = "Bye_") |
| 72 | + public interface ByeActivity extends GreetingActivity { |
| 73 | + @Override |
| 74 | + String composeGreeting(String name); |
| 75 | + } |
| 76 | + |
| 77 | + public static class GreetingWorkflowImpl implements GreetingWorkflow { |
| 78 | + |
| 79 | + private final GreetingActivity[] activities = |
| 80 | + new GreetingActivity[] { |
| 81 | + Workflow.newActivityStub( |
| 82 | + HelloActivity.class, |
| 83 | + ActivityOptions.newBuilder() |
| 84 | + .setScheduleToCloseTimeout(Duration.ofSeconds(2)) |
| 85 | + .build()), |
| 86 | + Workflow.newActivityStub( |
| 87 | + ByeActivity.class, |
| 88 | + ActivityOptions.newBuilder().setScheduleToCloseTimeout(Duration.ofSeconds(2)).build()) |
| 89 | + }; |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getGreeting(String name) { |
| 93 | + StringBuilder result = new StringBuilder(); |
| 94 | + for (GreetingActivity activity : activities) { |
| 95 | + result.append(activity.composeGreeting(name)); |
| 96 | + result.append('\n'); |
| 97 | + } |
| 98 | + return result.toString(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + static class HelloActivityImpl implements HelloActivity { |
| 103 | + @Override |
| 104 | + public String composeGreeting(String name) { |
| 105 | + return "Hello " + name + "!"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static class ByeActivityImpl implements ByeActivity { |
| 110 | + @Override |
| 111 | + public String composeGreeting(String name) { |
| 112 | + return "Bye " + name + "!"; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static void main(String[] args) { |
| 117 | + // gRPC stubs wrapper that talks to the local docker instance of temporal service. |
| 118 | + WorkflowServiceStubs service = WorkflowServiceStubs.newInstance(); |
| 119 | + // client that can be used to start and signal workflows |
| 120 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 121 | + |
| 122 | + // worker factory that can be used to create workers for specific task lists |
| 123 | + WorkerFactory factory = WorkerFactory.newInstance(client); |
| 124 | + // Worker that listens on a task list and hosts both workflow and activity implementations. |
| 125 | + Worker worker = factory.newWorker(TASK_LIST); |
| 126 | + // Workflows are stateful. So you need a type to create instances. |
| 127 | + worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class); |
| 128 | + // Activities are stateless and thread safe. So a shared instance is used. |
| 129 | + worker.registerActivitiesImplementations(new HelloActivityImpl(), new ByeActivityImpl()); |
| 130 | + // Start listening to the workflow and activity task lists. |
| 131 | + factory.start(); |
| 132 | + |
| 133 | + // Start a workflow execution. Usually this is done from another program. |
| 134 | + // Uses task list from the GreetingWorkflow @WorkflowMethod annotation. |
| 135 | + GreetingWorkflow workflow = |
| 136 | + client.newWorkflowStub( |
| 137 | + GreetingWorkflow.class, WorkflowOptions.newBuilder().setTaskList(TASK_LIST).build()); |
| 138 | + // Execute a workflow waiting for it to complete. See {@link |
| 139 | + // io.temporal.samples.hello.HelloSignal} |
| 140 | + // for an example of starting workflow without waiting synchronously for its result. |
| 141 | + String greeting = workflow.getGreeting("World"); |
| 142 | + System.out.println(greeting); |
| 143 | + System.exit(0); |
| 144 | + } |
| 145 | +} |
0 commit comments