|
| 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.*; |
| 23 | +import io.temporal.client.WorkflowClient; |
| 24 | +import io.temporal.client.WorkflowOptions; |
| 25 | +import io.temporal.common.RetryOptions; |
| 26 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 27 | +import io.temporal.worker.Worker; |
| 28 | +import io.temporal.worker.WorkerFactory; |
| 29 | +import io.temporal.workflow.*; |
| 30 | +import java.time.Duration; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +public class HelloActivityCancel { |
| 37 | + |
| 38 | + static final String TASK_QUEUE = "HelloActivityTaskQueue"; |
| 39 | + |
| 40 | + static final String WORKFLOW_ID = "HelloActivityWorkflow"; |
| 41 | + |
| 42 | + @WorkflowInterface |
| 43 | + public interface GreetingWorkflow { |
| 44 | + |
| 45 | + @WorkflowMethod |
| 46 | + String getGreeting(String name); |
| 47 | + } |
| 48 | + |
| 49 | + @WorkflowInterface |
| 50 | + public interface ChildGreetingWorkflow { |
| 51 | + |
| 52 | + @WorkflowMethod |
| 53 | + String getGreeting(String name); |
| 54 | + } |
| 55 | + |
| 56 | + public static class ChildGreetingWorkflowImpl implements ChildGreetingWorkflow { |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getGreeting(final String name) { |
| 60 | + |
| 61 | + Workflow.sleep(Duration.ofSeconds(30)); |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @ActivityInterface |
| 68 | + public interface GreetingActivities { |
| 69 | + |
| 70 | + @ActivityMethod(name = "greet") |
| 71 | + String composeGreeting(String greeting, String name); |
| 72 | + } |
| 73 | + |
| 74 | + public static class GreetingWorkflowImpl implements GreetingWorkflow { |
| 75 | + |
| 76 | + private final GreetingActivities activities = |
| 77 | + Workflow.newActivityStub( |
| 78 | + GreetingActivities.class, |
| 79 | + ActivityOptions.newBuilder() |
| 80 | + .setCancellationType(ActivityCancellationType.WAIT_CANCELLATION_COMPLETED) |
| 81 | + .setStartToCloseTimeout(Duration.ofSeconds(20)) |
| 82 | + .setHeartbeatTimeout(Duration.ofSeconds(3)) |
| 83 | + .setRetryOptions( |
| 84 | + RetryOptions.newBuilder().setInitialInterval(Duration.ofSeconds(10)).build()) |
| 85 | + .build()); |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getGreeting(String name) { |
| 89 | + String hello = null; |
| 90 | + |
| 91 | + try { |
| 92 | + |
| 93 | + final List<Promise<String>> promises = new ArrayList<>(); |
| 94 | + |
| 95 | + ChildWorkflowStub child = |
| 96 | + Workflow.newUntypedChildWorkflowStub( |
| 97 | + ChildGreetingWorkflow.class.getSimpleName(), |
| 98 | + ChildWorkflowOptions.newBuilder() |
| 99 | + // |
| 100 | + // .setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON) |
| 101 | + // |
| 102 | + // .setCancellationType(ChildWorkflowCancellationType.WAIT_CANCELLATION_REQUESTED) |
| 103 | + .setWorkflowId("Child_of_" + WORKFLOW_ID) |
| 104 | + .build()); |
| 105 | + promises.add(child.executeAsync(String.class, "Hello", name)); |
| 106 | + |
| 107 | + // Wait for the child workflow to start before returning the result |
| 108 | + // Promise<WorkflowExecution> childExecution = child.getExecution(); |
| 109 | + // WorkflowExecution childWorkflowExecution = childExecution.get(); |
| 110 | + |
| 111 | + promises.add(Async.function(activities::composeGreeting, "Hello", name)); |
| 112 | + |
| 113 | + for (int i = promises.size() - 1; i >= 0; i--) { |
| 114 | + |
| 115 | + try { |
| 116 | + promises.get(i).get(); |
| 117 | + } catch (Exception e) { |
| 118 | + System.out.println("In for promise >>>>>>> " + e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + } catch (Exception e) { |
| 123 | + System.out.println("Something cancelled " + e); |
| 124 | + // throw e; |
| 125 | + } |
| 126 | + |
| 127 | + return hello; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + static class GreetingActivitiesImpl implements GreetingActivities { |
| 132 | + private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class); |
| 133 | + |
| 134 | + @Override |
| 135 | + public String composeGreeting(String greeting, String name) { |
| 136 | + log.info("Composing greeting..."); |
| 137 | + |
| 138 | + // int i = 0; |
| 139 | + |
| 140 | + try { |
| 141 | + |
| 142 | + for (int i = 0; i < 15; i++) { |
| 143 | + |
| 144 | + System.out.println(">>>>>>> heartbeat " + i); |
| 145 | + try { |
| 146 | + Activity.getExecutionContext().heartbeat("" + i); |
| 147 | + } catch (Exception e) { |
| 148 | + System.out.println(">>>>>>> heartbeat " + e); |
| 149 | + |
| 150 | + // return greeting + " " + name + "!"; |
| 151 | + throw e; |
| 152 | + } |
| 153 | + |
| 154 | + try { |
| 155 | + Thread.sleep(1000); |
| 156 | + } catch (InterruptedException e) { |
| 157 | + throw new RuntimeException(e); |
| 158 | + } |
| 159 | + } |
| 160 | + } catch (Exception e) { |
| 161 | + System.out.println(">>>>>>> " + e); |
| 162 | + |
| 163 | + // return greeting + " " + name + "!"; |
| 164 | + throw e; |
| 165 | + } |
| 166 | + return greeting + " " + name + "!"; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * With our Workflow and Activities defined, we can now start execution. The main method starts |
| 172 | + * the worker and then the workflow. |
| 173 | + */ |
| 174 | + public static void main(String[] args) { |
| 175 | + |
| 176 | + // Get a Workflow service stub. |
| 177 | + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
| 178 | + |
| 179 | + /* |
| 180 | + * Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions. |
| 181 | + */ |
| 182 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 183 | + |
| 184 | + /* |
| 185 | + * Define the workflow factory. It is used to create workflow workers for a specific task queue. |
| 186 | + */ |
| 187 | + WorkerFactory factory = WorkerFactory.newInstance(client); |
| 188 | + |
| 189 | + /* |
| 190 | + * Define the workflow worker. Workflow workers listen to a defined task queue and process |
| 191 | + * workflows and activities. |
| 192 | + */ |
| 193 | + Worker worker = factory.newWorker(TASK_QUEUE); |
| 194 | + |
| 195 | + /* |
| 196 | + * Register our workflow implementation with the worker. |
| 197 | + * Workflow implementations must be known to the worker at runtime in |
| 198 | + * order to dispatch workflow tasks. |
| 199 | + */ |
| 200 | + worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class); |
| 201 | + worker.registerWorkflowImplementationTypes(ChildGreetingWorkflowImpl.class); |
| 202 | + |
| 203 | + /* |
| 204 | + * Register our Activity Types with the Worker. Since Activities are stateless and thread-safe, |
| 205 | + * the Activity Type is a shared instance. |
| 206 | + */ |
| 207 | + worker.registerActivitiesImplementations(new GreetingActivitiesImpl()); |
| 208 | + |
| 209 | + /* |
| 210 | + * Start all the workers registered for a specific task queue. |
| 211 | + * The started workers then start polling for workflows and activities. |
| 212 | + */ |
| 213 | + factory.start(); |
| 214 | + |
| 215 | + // Create the workflow client stub. It is used to start our workflow execution. |
| 216 | + GreetingWorkflow workflow = |
| 217 | + client.newWorkflowStub( |
| 218 | + GreetingWorkflow.class, |
| 219 | + WorkflowOptions.newBuilder() |
| 220 | + .setWorkflowId(WORKFLOW_ID) |
| 221 | + .setTaskQueue(TASK_QUEUE) |
| 222 | + .build()); |
| 223 | + |
| 224 | + WorkflowClient.start(workflow::getGreeting, "World"); |
| 225 | + |
| 226 | + try { |
| 227 | + Thread.sleep(2000); |
| 228 | + } catch (InterruptedException e) { |
| 229 | + throw new RuntimeException(e); |
| 230 | + } |
| 231 | + |
| 232 | + System.out.println("About to cancel.. "); |
| 233 | + client.newUntypedWorkflowStub(WORKFLOW_ID).cancel(); |
| 234 | + System.out.println("cancellation request sent .. "); |
| 235 | + |
| 236 | + // System.exit(0); |
| 237 | + } |
| 238 | +} |
0 commit comments