|
| 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.customannotation; |
| 21 | + |
| 22 | +import io.temporal.activity.ActivityInterface; |
| 23 | +import io.temporal.activity.ActivityOptions; |
| 24 | +import io.temporal.client.WorkflowClient; |
| 25 | +import io.temporal.client.WorkflowOptions; |
| 26 | +import io.temporal.common.RetryOptions; |
| 27 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 28 | +import io.temporal.worker.Worker; |
| 29 | +import io.temporal.worker.WorkerFactory; |
| 30 | +import io.temporal.worker.WorkerFactoryOptions; |
| 31 | +import io.temporal.workflow.Workflow; |
| 32 | +import io.temporal.workflow.WorkflowInterface; |
| 33 | +import io.temporal.workflow.WorkflowMethod; |
| 34 | +import java.time.Duration; |
| 35 | + |
| 36 | +public class CustomAnnotation { |
| 37 | + |
| 38 | + // Define the task queue name |
| 39 | + static final String TASK_QUEUE = "CustomAnnotationTaskQueue"; |
| 40 | + |
| 41 | + // Define our workflow unique id |
| 42 | + static final String WORKFLOW_ID = "CustomAnnotationWorkflow"; |
| 43 | + |
| 44 | + /** |
| 45 | + * The Workflow Definition's Interface must contain one method annotated with @WorkflowMethod. |
| 46 | + * |
| 47 | + * <p>Workflow Definitions should not contain any heavyweight computations, non-deterministic |
| 48 | + * code, network calls, database operations, etc. Those things should be handled by the |
| 49 | + * Activities. |
| 50 | + * |
| 51 | + * @see WorkflowInterface |
| 52 | + * @see WorkflowMethod |
| 53 | + */ |
| 54 | + @WorkflowInterface |
| 55 | + public interface GreetingWorkflow { |
| 56 | + |
| 57 | + /** |
| 58 | + * This is the method that is executed when the Workflow Execution is started. The Workflow |
| 59 | + * Execution completes when this method finishes execution. |
| 60 | + */ |
| 61 | + @WorkflowMethod |
| 62 | + String getGreeting(String name); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * This is the Activity Definition's Interface. Activities are building blocks of any Temporal |
| 67 | + * Workflow and contain any business logic that could perform long running computation, network |
| 68 | + * calls, etc. |
| 69 | + * |
| 70 | + * <p>Annotating Activity Definition methods with @ActivityMethod is optional. |
| 71 | + * |
| 72 | + * @see ActivityInterface |
| 73 | + * @see io.temporal.activity.ActivityMethod |
| 74 | + */ |
| 75 | + @ActivityInterface |
| 76 | + public interface GreetingActivities { |
| 77 | + |
| 78 | + /** Define your activity method which can be called during workflow execution */ |
| 79 | + String composeGreeting(String greeting, String name); |
| 80 | + } |
| 81 | + |
| 82 | + // Define the workflow implementation which implements our getGreeting workflow method. |
| 83 | + public static class GreetingWorkflowImpl implements GreetingWorkflow { |
| 84 | + |
| 85 | + /** |
| 86 | + * Define the GreetingActivities stub. Activity stubs are proxies for activity invocations that |
| 87 | + * are executed outside of the workflow thread on the activity worker, that can be on a |
| 88 | + * different host. Temporal is going to dispatch the activity results back to the workflow and |
| 89 | + * unblock the stub as soon as activity is completed on the activity worker. |
| 90 | + * |
| 91 | + * <p>In the {@link ActivityOptions} definition the "setStartToCloseTimeout" option sets the |
| 92 | + * maximum time of a single Activity execution attempt. For this example it is set to 10 |
| 93 | + * seconds. |
| 94 | + * |
| 95 | + * <p>In the {@link ActivityOptions} definition the "setInitialInterval" option sets the |
| 96 | + * interval of the first retry. It is set to 1 second. The "setDoNotRetry" option is a list of |
| 97 | + * application failures for which retries should not be performed. |
| 98 | + * |
| 99 | + * <p>By default the maximum number of retry attempts is set to "unlimited" however you can |
| 100 | + * change it by adding the "setMaximumAttempts" option to the retry options. |
| 101 | + */ |
| 102 | + private final GreetingActivities activities = |
| 103 | + Workflow.newActivityStub( |
| 104 | + GreetingActivities.class, |
| 105 | + ActivityOptions.newBuilder() |
| 106 | + .setStartToCloseTimeout(Duration.ofSeconds(10)) |
| 107 | + .setRetryOptions( |
| 108 | + RetryOptions.newBuilder() |
| 109 | + .setInitialInterval(Duration.ofSeconds(1)) |
| 110 | + .setDoNotRetry(IllegalArgumentException.class.getName()) |
| 111 | + .build()) |
| 112 | + .build()); |
| 113 | + |
| 114 | + @Override |
| 115 | + public String getGreeting(String name) { |
| 116 | + // This is a blocking call that returns only after activity is completed. |
| 117 | + return activities.composeGreeting("Hello", name); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Implementation of our workflow activity interface. It overwrites our defined composeGreeting |
| 123 | + * activity method. |
| 124 | + */ |
| 125 | + static class GreetingActivitiesImpl implements GreetingActivities { |
| 126 | + private int callCount; |
| 127 | + private long lastInvocationTime; |
| 128 | + |
| 129 | + /** |
| 130 | + * Our activity implementation simulates a failure 3 times. Given our previously set |
| 131 | + * RetryOptions, our workflow is going to retry our activity execution. |
| 132 | + */ |
| 133 | + @Override |
| 134 | + @NextRetryDelay(failureType = "java.lang.IllegalStateException", delaySeconds = 2) |
| 135 | + public synchronized String composeGreeting(String greeting, String name) { |
| 136 | + if (lastInvocationTime != 0) { |
| 137 | + long timeSinceLastInvocation = System.currentTimeMillis() - lastInvocationTime; |
| 138 | + System.out.print(timeSinceLastInvocation + " milliseconds since last invocation. "); |
| 139 | + } |
| 140 | + lastInvocationTime = System.currentTimeMillis(); |
| 141 | + if (++callCount < 4) { |
| 142 | + System.out.println("composeGreeting activity is going to fail"); |
| 143 | + |
| 144 | + /* |
| 145 | + * We throw IllegalStateException here. It is not in the list of "do not retry" exceptions |
| 146 | + * set in our RetryOptions, so a workflow retry is going to be issued |
| 147 | + */ |
| 148 | + throw new IllegalStateException("not yet"); |
| 149 | + } |
| 150 | + |
| 151 | + // after 3 unsuccessful retries we finally can complete our activity execution |
| 152 | + System.out.println("composeGreeting activity is going to complete"); |
| 153 | + return greeting + " " + name + "!"; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * With our Workflow and Activities defined, we can now start execution. The main method starts |
| 159 | + * the worker and then the workflow. |
| 160 | + */ |
| 161 | + public static void main(String[] args) { |
| 162 | + |
| 163 | + // Get a Workflow service stub. |
| 164 | + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
| 165 | + |
| 166 | + /* |
| 167 | + * Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions. |
| 168 | + */ |
| 169 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 170 | + |
| 171 | + /* |
| 172 | + * Define the workflow factory. It is used to create workflow workers for a specific task queue. |
| 173 | + */ |
| 174 | + WorkerFactory factory = |
| 175 | + WorkerFactory.newInstance( |
| 176 | + client, |
| 177 | + WorkerFactoryOptions.newBuilder() |
| 178 | + .setWorkerInterceptors(new NextRetryDelayActivityAnnotationInterceptor()) |
| 179 | + .build()); |
| 180 | + |
| 181 | + /* |
| 182 | + * Define the workflow worker. Workflow workers listen to a defined task queue and process |
| 183 | + * workflows and activities. |
| 184 | + */ |
| 185 | + Worker worker = factory.newWorker(TASK_QUEUE); |
| 186 | + |
| 187 | + /* |
| 188 | + * Register our workflow implementation with the worker. |
| 189 | + * Workflow implementations must be known to the worker at runtime in |
| 190 | + * order to dispatch workflow tasks. |
| 191 | + */ |
| 192 | + worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class); |
| 193 | + |
| 194 | + /* |
| 195 | + * Register our Activity Types with the Worker. Since Activities are stateless and thread-safe, |
| 196 | + * the Activity Type is a shared instance. |
| 197 | + */ |
| 198 | + worker.registerActivitiesImplementations(new GreetingActivitiesImpl()); |
| 199 | + |
| 200 | + /* |
| 201 | + * Start all the workers registered for a specific task queue. |
| 202 | + * The started workers then start polling for workflows and activities. |
| 203 | + */ |
| 204 | + factory.start(); |
| 205 | + |
| 206 | + // Set our workflow options |
| 207 | + WorkflowOptions workflowOptions = |
| 208 | + WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build(); |
| 209 | + |
| 210 | + // Create the workflow client stub. It is used to start our workflow execution. |
| 211 | + GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions); |
| 212 | + |
| 213 | + /* |
| 214 | + * Execute our workflow and wait for it to complete. The call to our getGreeting method is |
| 215 | + * synchronous. |
| 216 | + * |
| 217 | + * See {@link io.temporal.samples.hello.HelloSignal} for an example of starting workflow |
| 218 | + * without waiting synchronously for its result. |
| 219 | + */ |
| 220 | + String greeting = workflow.getGreeting("World"); |
| 221 | + |
| 222 | + // Display workflow execution results |
| 223 | + System.out.println(greeting); |
| 224 | + System.exit(0); |
| 225 | + } |
| 226 | +} |
0 commit comments