|
| 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.client.WorkflowClient; |
| 23 | +import io.temporal.client.WorkflowOptions; |
| 24 | +import io.temporal.common.WorkflowExecutionHistory; |
| 25 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 26 | +import io.temporal.worker.Worker; |
| 27 | +import io.temporal.worker.WorkerFactory; |
| 28 | +import io.temporal.workflow.Workflow; |
| 29 | +import io.temporal.workflow.WorkflowInterface; |
| 30 | +import io.temporal.workflow.WorkflowMethod; |
| 31 | +import java.time.Duration; |
| 32 | + |
| 33 | +/** Sample Temporal Workflow Definition that shows how to use delayed start. */ |
| 34 | +public class HelloDelayedStart { |
| 35 | + // Define the task queue name |
| 36 | + static final String TASK_QUEUE = "HelloDelayedStartTaskQueue"; |
| 37 | + |
| 38 | + // Define our workflow unique id |
| 39 | + static final String WORKFLOW_ID = "HelloDelayedStartWorkflow"; |
| 40 | + |
| 41 | + /** |
| 42 | + * The Workflow Definition's Interface must contain one method annotated with @WorkflowMethod. |
| 43 | + * |
| 44 | + * <p>Workflow Definitions should not contain any heavyweight computations, non-deterministic |
| 45 | + * code, network calls, database operations, etc. Those things should be handled by the |
| 46 | + * Activities. |
| 47 | + * |
| 48 | + * @see io.temporal.workflow.WorkflowInterface |
| 49 | + * @see io.temporal.workflow.WorkflowMethod |
| 50 | + */ |
| 51 | + @WorkflowInterface |
| 52 | + public interface DelayedStartWorkflow { |
| 53 | + |
| 54 | + /** |
| 55 | + * This is the method that is executed when the Workflow Execution is started. The Workflow |
| 56 | + * Execution completes when this method finishes execution. |
| 57 | + */ |
| 58 | + @WorkflowMethod |
| 59 | + void start(); |
| 60 | + } |
| 61 | + |
| 62 | + // Define the workflow implementation which implements our start workflow method. |
| 63 | + public static class DelayedStartWorkflowImpl implements DelayedStartWorkflow { |
| 64 | + @Override |
| 65 | + public void start() { |
| 66 | + // this workflow just sleeps for a second |
| 67 | + Workflow.sleep(Duration.ofSeconds(1)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static void main(String[] args) { |
| 72 | + // Get a Workflow service stub. |
| 73 | + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
| 74 | + |
| 75 | + /* |
| 76 | + * Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions. |
| 77 | + */ |
| 78 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 79 | + |
| 80 | + /* |
| 81 | + * Define the workflow factory. It is used to create workflow workers for a specific task queue. |
| 82 | + */ |
| 83 | + WorkerFactory factory = WorkerFactory.newInstance(client); |
| 84 | + |
| 85 | + /* |
| 86 | + * Define the workflow worker. Workflow workers listen to a defined task queue and process |
| 87 | + * workflows and activities. |
| 88 | + */ |
| 89 | + Worker worker = factory.newWorker(TASK_QUEUE); |
| 90 | + |
| 91 | + /* |
| 92 | + * Register our workflow implementation with the worker. |
| 93 | + * Workflow implementations must be known to the worker at runtime in |
| 94 | + * order to dispatch workflow tasks. |
| 95 | + */ |
| 96 | + worker.registerWorkflowImplementationTypes(DelayedStartWorkflowImpl.class); |
| 97 | + |
| 98 | + /* |
| 99 | + * Start all the workers registered for a specific task queue. |
| 100 | + * The started workers then start polling for workflows and activities. |
| 101 | + */ |
| 102 | + factory.start(); |
| 103 | + |
| 104 | + DelayedStartWorkflow workflow = |
| 105 | + client.newWorkflowStub( |
| 106 | + DelayedStartWorkflow.class, |
| 107 | + WorkflowOptions.newBuilder() |
| 108 | + .setWorkflowId(WORKFLOW_ID) |
| 109 | + .setTaskQueue(TASK_QUEUE) |
| 110 | + // set delayed start in 2 seconds |
| 111 | + .setStartDelay(Duration.ofSeconds(2)) |
| 112 | + .build()); |
| 113 | + |
| 114 | + workflow.start(); |
| 115 | + |
| 116 | + // Delayed executions are still created right away by the service but |
| 117 | + // they have a firstWorkflowTaskBackoff set to the delay duration |
| 118 | + // meaning the first workflow task is dispatched by service |
| 119 | + // 2 second after exec is started in our sample |
| 120 | + WorkflowExecutionHistory history = client.fetchHistory(WORKFLOW_ID); |
| 121 | + com.google.protobuf.Duration backoff = |
| 122 | + history |
| 123 | + .getHistory() |
| 124 | + .getEvents(0) |
| 125 | + .getWorkflowExecutionStartedEventAttributes() |
| 126 | + .getFirstWorkflowTaskBackoff(); |
| 127 | + System.out.println("First workflow task backoff: " + backoff.getSeconds()); |
| 128 | + |
| 129 | + System.exit(0); |
| 130 | + } |
| 131 | +} |
0 commit comments