|
25 | 25 | import java.util.concurrent.CancellationException;
|
26 | 26 |
|
27 | 27 | /**
|
28 |
| - * Use this method from within activity implementation to get information about activity task and |
29 |
| - * heartbeat. |
| 28 | + * An activity is the implementation of a particular task in the business logic. |
| 29 | + * |
| 30 | + * <h2>Activity Interface</h2> |
| 31 | + * |
| 32 | + * <p>Activities are defined as methods of a plain Java interface. Each method defines a single |
| 33 | + * activity type. A single workflow can use more than one activity interface and call more than one |
| 34 | + * activity method from the same interface. The only requirement is that activity method arguments |
| 35 | + * and return values are serializable to a byte array using the provided {@link |
| 36 | + * com.uber.cadence.converter.DataConverter} implementation. The default implementation uses JSON |
| 37 | + * serializer, but an alternative implementation can be easily configured. |
| 38 | + * |
| 39 | + * <p>Example of an interface that defines four activities: |
| 40 | + * |
| 41 | + * <pre><code> |
| 42 | + * public interface FileProcessingActivities { |
| 43 | + * |
| 44 | + * void upload(String bucketName, String localName, String targetName); |
| 45 | + * |
| 46 | + * String download(String bucketName, String remoteName); |
| 47 | + * |
| 48 | + * {@literal @}ActivityMethod(scheduleToCloseTimeoutSeconds = 2) |
| 49 | + * String processFile(String localName); |
| 50 | + * |
| 51 | + * void deleteLocalFile(String fileName); |
| 52 | + * } |
| 53 | + * |
| 54 | + * </code></pre> |
| 55 | + * |
| 56 | + * An optional {@literal @}{@link ActivityMethod} annotation can be used to specify activity options |
| 57 | + * like timeouts or a task list. Required options that are not specified through the annotation must |
| 58 | + * be specified at run time. |
| 59 | + * |
| 60 | + * <h2>Activity Implementation</h2> |
| 61 | + * |
| 62 | + * <p>Activity implementation is an implementation of an activity interface. A single instance of |
| 63 | + * the activity's implementation is shared across multiple simultaneous activity invocations. |
| 64 | + * Therefore, the activity implementation code must be <i>thread safe</i>. |
| 65 | + * |
| 66 | + * <p>The values passed to activities through invocation parameters or returned through a result |
| 67 | + * value are recorded in the execution history. The entire execution history is transferred from the |
| 68 | + * Cadence service to workflow workers when a workflow state needs to recover. A large execution |
| 69 | + * history can thus adversely impact the performance of your workflow. Therefore, be mindful of the |
| 70 | + * amount of data you transfer via activity invocation parameters or return values. Other than that, |
| 71 | + * no additional limitations exist on activity implementations. |
| 72 | + * |
| 73 | + * <pre><code> |
| 74 | + * public class FileProcessingActivitiesImpl implements FileProcessingActivities { |
| 75 | + * |
| 76 | + * private final AmazonS3 s3Client; |
| 77 | + * |
| 78 | + * private final String localDirectory; |
| 79 | + * |
| 80 | + * void upload(String bucketName, String localName, String targetName) { |
| 81 | + * File f = new File(localName); |
| 82 | + * s3Client.putObject(bucket, remoteName, f); |
| 83 | + * } |
| 84 | + * |
| 85 | + * String download(String bucketName, String remoteName, String localName) { |
| 86 | + * // Implementation omitted for brevity. |
| 87 | + * return downloadFileFromS3(bucketName, remoteName, localDirectory + localName); |
| 88 | + * } |
| 89 | + * |
| 90 | + * String processFile(String localName) { |
| 91 | + * // Implementation omitted for brevity. |
| 92 | + * return compressFile(localName); |
| 93 | + * } |
| 94 | + * |
| 95 | + * void deleteLocalFile(String fileName) { |
| 96 | + * File f = new File(localDirectory + fileName); |
| 97 | + * f.delete(); |
| 98 | + * } |
| 99 | + * } |
| 100 | + * </code></pre> |
| 101 | + * |
| 102 | + * <h3>Accessing Activity Info</h3> |
| 103 | + * |
| 104 | + * <p>The {@link Activity} class provides static getters to access information about the workflow |
| 105 | + * that invoked it. Note that this information is stored in a thread-local variable. Therefore, |
| 106 | + * calls to Activity accessors succeed only in the thread that invoked the activity function. |
| 107 | + * |
| 108 | + * <pre><code> |
| 109 | + * public class FileProcessingActivitiesImpl implements FileProcessingActivities { |
| 110 | + * |
| 111 | + * {@literal @}Override |
| 112 | + * public String download(String bucketName, String remoteName, String localName) { |
| 113 | + * log.info("domain=" + Activity.getDomain()); |
| 114 | + * WorkflowExecution execution = Activity.getWorkflowExecution(); |
| 115 | + * log.info("workflowId=" + execution.getWorkflowId()); |
| 116 | + * log.info("runId=" + execution.getRunId()); |
| 117 | + * ActivityTask activityTask = Activity.getTask(); |
| 118 | + * log.info("activityId=" + activityTask.getActivityId()); |
| 119 | + * log.info("activityTimeout=" + activityTask.getStartToCloseTimeoutSeconds()); |
| 120 | + * return downloadFileFromS3(bucketName, remoteName, localDirectory + localName); |
| 121 | + * } |
| 122 | + * ... |
| 123 | + * } |
| 124 | + * </code></pre> |
| 125 | + * |
| 126 | + * <h3>Asynchronous Activity Completion</h3> |
| 127 | + * |
| 128 | + * <p>Sometimes an activity lifecycle goes beyond a synchronous method invocation. For example, a |
| 129 | + * request can be put in a queue and later a reply comes and is picked up by a different worker |
| 130 | + * process. The whole request-reply interaction can be modeled as a single Cadence activity. |
| 131 | + * |
| 132 | + * <p>To indicate that an activity should not be completed upon its method return, call {@link |
| 133 | + * Activity#doNotCompleteOnReturn()} from the original activity thread. Then later, when replies |
| 134 | + * come, complete the activity using {@link com.uber.cadence.client.ActivityCompletionClient}. To |
| 135 | + * correlate activity invocation with completion use either {@code TaskToken} or workflow and |
| 136 | + * activity IDs. |
| 137 | + * |
| 138 | + * <pre><code> |
| 139 | + * public class FileProcessingActivitiesImpl implements FileProcessingActivities { |
| 140 | + * |
| 141 | + * public String download(String bucketName, String remoteName, String localName) { |
| 142 | + * byte[] taskToken = Activity.getTaskToken(); // Used to correlate reply |
| 143 | + * asyncDownloadFileFromS3(taskToken, bucketName, remoteName, localDirectory + localName); |
| 144 | + * Activity.doNotCompleteOnReturn(); |
| 145 | + * return "ignored"; // Return value is ignored when doNotCompleteOnReturn was called. |
| 146 | + * } |
| 147 | + * ... |
| 148 | + * } |
| 149 | + * </code></pre> |
| 150 | + * |
| 151 | + * When the download is complete, the download service potentially calls back from a different |
| 152 | + * process: |
| 153 | + * |
| 154 | + * <pre><code> |
| 155 | + * public <R> void completeActivity(byte[] taskToken, R result) { |
| 156 | + * completionClient.complete(taskToken, result); |
| 157 | + * } |
| 158 | + * |
| 159 | + * public void failActivity(byte[] taskToken, Exception failure) { |
| 160 | + * completionClient.completeExceptionally(taskToken, failure); |
| 161 | + * } |
| 162 | + * </code></pre> |
| 163 | + * |
| 164 | + * <h3>Activity Heartbeating</h3> |
| 165 | + * |
| 166 | + * <p>Some activities are long running. To react to their crashes quickly, use a heartbeat |
| 167 | + * mechanism. Use the {@link Activity#heartbeat(Object)} function to let the Cadence service know |
| 168 | + * that the activity is still alive. You can piggyback `details` on an activity heartbeat. If an |
| 169 | + * activity times out, the last value of `details` is included in the ActivityTimeoutException |
| 170 | + * delivered to a workflow. Then the workflow can pass the details to the next activity invocation. |
| 171 | + * This acts as a periodic checkpointing mechanism of an activity's progress. |
| 172 | + * |
| 173 | + * <pre><code> |
| 174 | + * public class FileProcessingActivitiesImpl implements FileProcessingActivities { |
| 175 | + * |
| 176 | + * {@literal @}Override |
| 177 | + * public String download(String bucketName, String remoteName, String localName) { |
| 178 | + * InputStream inputStream = openInputStream(file); |
| 179 | + * try { |
| 180 | + * byte[] bytes = new byte[MAX_BUFFER_SIZE]; |
| 181 | + * while ((read = inputStream.read(bytes)) != -1) { |
| 182 | + * totalRead += read; |
| 183 | + * f.write(bytes, 0, read); |
| 184 | + * // Let the service know about the download progress. |
| 185 | + * Activity.heartbeat(totalRead); |
| 186 | + * } |
| 187 | + * }finally{ |
| 188 | + * inputStream.close(); |
| 189 | + * } |
| 190 | + * } |
| 191 | + * ... |
| 192 | + * } |
| 193 | + * </code></pre> |
| 194 | + * |
| 195 | + * @see com.uber.cadence.worker.Worker |
| 196 | + * @see com.uber.cadence.workflow.Workflow |
| 197 | + * @see com.uber.cadence.client.WorkflowClient |
30 | 198 | */
|
31 | 199 | public final class Activity {
|
32 | 200 |
|
|
0 commit comments