|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | +package com.uber.cadence.samples.hello; |
| 18 | + |
| 19 | +import com.uber.cadence.client.WorkflowClient; |
| 20 | +import com.uber.cadence.client.WorkflowOptions; |
| 21 | +import com.uber.cadence.worker.Worker; |
| 22 | +import com.uber.cadence.workflow.ActivityOptions; |
| 23 | +import com.uber.cadence.workflow.Functions; |
| 24 | +import com.uber.cadence.workflow.RetryOptions; |
| 25 | +import com.uber.cadence.workflow.Workflow; |
| 26 | +import com.uber.cadence.workflow.WorkflowMethod; |
| 27 | + |
| 28 | +import java.time.Duration; |
| 29 | + |
| 30 | +import static com.uber.cadence.samples.common.SampleConstants.DOMAIN; |
| 31 | + |
| 32 | +/** |
| 33 | + * Demonstrates activity retries using exponential backoff algorithm. |
| 34 | + * Requires a local instance of Cadence server running. |
| 35 | + */ |
| 36 | +public class HelloActivityRetry { |
| 37 | + |
| 38 | + private static final String TASK_LIST = "HelloActivity"; |
| 39 | + |
| 40 | + public interface GreetingWorkflow { |
| 41 | + /** |
| 42 | + * @return greeting string |
| 43 | + */ |
| 44 | + @WorkflowMethod |
| 45 | + String getGreeting(String name); |
| 46 | + } |
| 47 | + |
| 48 | + public interface GreetingActivities { |
| 49 | + String composeGreeting(String greeting, String name); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * GreetingWorkflow implementation that demonstrates activity stub configured with |
| 54 | + * {@link RetryOptions}. |
| 55 | + */ |
| 56 | + public static class GreetingWorkflowImpl implements GreetingWorkflow { |
| 57 | + |
| 58 | + /** |
| 59 | + * To enable activity retry set {@link RetryOptions} on {@link ActivityOptions}. |
| 60 | + * It also works for activities invoked through |
| 61 | + * {@link com.uber.cadence.workflow.Async#invoke(Functions.Proc)} and for child workflows. |
| 62 | + */ |
| 63 | + private final GreetingActivities activities = Workflow.newActivityStub( |
| 64 | + GreetingActivities.class, |
| 65 | + new ActivityOptions.Builder() |
| 66 | + .setScheduleToCloseTimeoutSeconds(10) |
| 67 | + .setRetryOptions(new RetryOptions.Builder() |
| 68 | + .setInitialInterval(Duration.ofSeconds(1)) |
| 69 | + .setExpiration(Duration.ofMinutes(1)) |
| 70 | + .build()) |
| 71 | + .build()); |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getGreeting(String name) { |
| 75 | + // This is blocking call that returns only after activity is completed. |
| 76 | + return activities.composeGreeting("Hello", name); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static class GreetingActivitiesImpl implements GreetingActivities { |
| 81 | + private int callCount; |
| 82 | + private long lastInvocationTime; |
| 83 | + |
| 84 | + @Override |
| 85 | + public synchronized String composeGreeting(String greeting, String name) { |
| 86 | + if (lastInvocationTime != 0) { |
| 87 | + long timeSinceLastInvocation = System.currentTimeMillis() - lastInvocationTime; |
| 88 | + System.out.print(timeSinceLastInvocation + " milliseconds since last invocation. "); |
| 89 | + } |
| 90 | + lastInvocationTime = System.currentTimeMillis(); |
| 91 | + if (++callCount < 4) { |
| 92 | + System.out.println("composeGreeting activity is going to fail"); |
| 93 | + throw new IllegalStateException("not yet"); |
| 94 | + } |
| 95 | + System.out.println("composeGreeting activity is going to complete"); |
| 96 | + return greeting + " " + name + "!"; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static void main(String[] args) { |
| 101 | + // Start a worker that hosts both workflow and activity implementation |
| 102 | + Worker worker = new Worker(DOMAIN, TASK_LIST); |
| 103 | + // Workflows are stateful. So need a type to create instances. |
| 104 | + worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class); |
| 105 | + // Activities are stateless and thread safe. So a shared instance is used. |
| 106 | + worker.registerActivitiesImplementations(new GreetingActivitiesImpl()); |
| 107 | + // Start listening to the workflow and activity task lists. |
| 108 | + worker.start(); |
| 109 | + |
| 110 | + // Start a workflow execution. Usually it is done from another program. |
| 111 | + WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN); |
| 112 | + // Get a workflow stub using the same task list the worker uses. |
| 113 | + WorkflowOptions workflowOptions = new WorkflowOptions.Builder() |
| 114 | + .setTaskList(TASK_LIST) |
| 115 | + .setExecutionStartToCloseTimeoutSeconds(30) |
| 116 | + .build(); |
| 117 | + GreetingWorkflow workflow = workflowClient.newWorkflowStub(GreetingWorkflow.class, |
| 118 | + workflowOptions); |
| 119 | + // Execute a workflow waiting for it complete. |
| 120 | + String greeting = workflow.getGreeting("World"); |
| 121 | + System.out.println(greeting); |
| 122 | + System.exit(0); |
| 123 | + } |
| 124 | +} |
0 commit comments