Skip to content

Commit 10e1376

Browse files
authored
Hello delayed start sample (#569)
* update to SpringBoot 3 Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * hello delayed start sample Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * fix Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * fix comment Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update to readme Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> --------- Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent 404adcb commit 10e1376

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ See the README.md file in each main sample directory for cut/paste Gradle comman
7171
- [**HelloSearchAttributes**](/core/src/main/java/io/temporal/samples/hello/HelloSearchAttributes.java): Demonstrates how to add custom Search Attributes to Workflow Executions.
7272
- [**HelloSideEffect**](/core/src/main/java/io/temporal/samples/hello/HelloSideEffect.java)**: Demonstrates how to implement a Side Effect.
7373
- [**HelloUpdate**](/core/src/main/java/io/temporal/samples/hello/HelloUpdate.java): Demonstrates how to create and interact with an Update.
74+
- [**HelloDelayedStart**](/core/src/main/java/io/temporal/samples/hello/HelloDelayedStart.java): Demonstrates how to use delayed start config option when starting a Workflow Executions.
7475

7576

7677
#### Scenario-based samples
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 static org.junit.Assert.assertEquals;
23+
24+
import io.temporal.client.WorkflowOptions;
25+
import io.temporal.common.WorkflowExecutionHistory;
26+
import io.temporal.testing.TestWorkflowRule;
27+
import java.time.Duration;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
31+
public class HelloDelayedStartTest {
32+
private final String WORKFLOW_ID = "HelloDelayedStartWorkflow";
33+
34+
@Rule
35+
public TestWorkflowRule testWorkflowRule =
36+
TestWorkflowRule.newBuilder()
37+
.setWorkflowTypes(HelloDelayedStart.DelayedStartWorkflowImpl.class)
38+
.build();
39+
40+
@Test
41+
public void testDelayedStart() {
42+
// Get a workflow stub using the same task queue the worker uses.
43+
WorkflowOptions workflowOptions =
44+
WorkflowOptions.newBuilder()
45+
.setTaskQueue(testWorkflowRule.getTaskQueue())
46+
.setWorkflowId(WORKFLOW_ID)
47+
.setStartDelay(Duration.ofSeconds(2))
48+
.build();
49+
50+
HelloDelayedStart.DelayedStartWorkflow workflow =
51+
testWorkflowRule
52+
.getWorkflowClient()
53+
.newWorkflowStub(HelloDelayedStart.DelayedStartWorkflow.class, workflowOptions);
54+
55+
workflow.start();
56+
57+
// Fetch event history and make sure we got the 2 seconds first workflow task backoff
58+
WorkflowExecutionHistory history =
59+
testWorkflowRule.getWorkflowClient().fetchHistory(WORKFLOW_ID);
60+
com.google.protobuf.Duration backoff =
61+
history
62+
.getHistory()
63+
.getEvents(0)
64+
.getWorkflowExecutionStartedEventAttributes()
65+
.getFirstWorkflowTaskBackoff();
66+
67+
assertEquals(2, backoff.getSeconds());
68+
}
69+
}

0 commit comments

Comments
 (0)