Skip to content

Commit fb1d26e

Browse files
committed
Merge remote-tracking branch 'core/main' into taskinteraction_
2 parents 29e3ad9 + 10e1376 commit fb1d26e

File tree

6 files changed

+232
-25
lines changed

6 files changed

+232
-25
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Temporal using the [Java SDK](https://github.com/temporalio/sdk-java).
55

66
It contains two modules:
77
* [Core](/core): showcases many different SDK features.
8-
* [SpringBoot](/springboot): showcases springboot autoconfig integration.
8+
* [SpringBoot](/springboot): showcases SpringBoot autoconfig integration.
99

1010
## Learn more about Temporal and Java SDK
1111

@@ -15,8 +15,9 @@ It contains two modules:
1515

1616
## Requirements
1717

18-
- Java 1.8+ for build and runtime
19-
- Java 11+ for development and contribution
18+
- Java 1.8+ for build and runtime of core samples
19+
- Java 1.8+ for build and runtime of SpringBoot samples when using SpringBoot 2
20+
- Java 1.17+ for build and runtime of Spring Boot samples when using SpringBoot 3
2021
- Local Temporal Server, easiest to get started would be using [Temporal CLI](https://github.com/temporalio/cli).
2122
For more options see docs [here](https://docs.temporal.io/kb/all-the-ways-to-run-a-cluster).
2223

@@ -70,6 +71,7 @@ See the README.md file in each main sample directory for cut/paste Gradle comman
7071
- [**HelloSearchAttributes**](/core/src/main/java/io/temporal/samples/hello/HelloSearchAttributes.java): Demonstrates how to add custom Search Attributes to Workflow Executions.
7172
- [**HelloSideEffect**](/core/src/main/java/io/temporal/samples/hello/HelloSideEffect.java)**: Demonstrates how to implement a Side Effect.
7273
- [**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.
7375

7476

7577
#### Scenario-based samples
@@ -133,6 +135,9 @@ See the README.md file in each main sample directory for cut/paste Gradle comman
133135

134136
### Running SpringBoot Samples
135137

138+
These samples use SpringBoot 2 by default. To switch to using SpringBoot 3 look at the [gradle.properties](gradle.properties) file
139+
and follow simple instructions there.
140+
136141
1. Start SpringBoot from main repo dir:
137142

138143
./gradlew bootRun

build.gradle

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'org.cadixdev.licenser' version '0.6.1'
33
id "net.ltgt.errorprone" version "3.1.0"
44
id 'com.diffplug.spotless' version '6.22.0' apply false
5-
id 'org.springframework.boot' version '2.7.13'
5+
id "org.springframework.boot" version "${springBootPluginVersion}"
66
}
77

88
subprojects {
@@ -11,15 +11,20 @@ subprojects {
1111
apply plugin: 'net.ltgt.errorprone'
1212
apply plugin: 'com.diffplug.spotless'
1313

14-
java {
15-
sourceCompatibility = JavaVersion.VERSION_1_8
16-
targetCompatibility = JavaVersion.VERSION_1_8
17-
}
18-
1914
compileJava {
2015
options.compilerArgs << "-Werror"
2116
}
2217

18+
java {
19+
if(project.property("springBootPluginVersion") == "2.7.13") {
20+
sourceCompatibility = JavaVersion.VERSION_11
21+
targetCompatibility = JavaVersion.VERSION_11
22+
} else {
23+
sourceCompatibility = JavaVersion.VERSION_17
24+
targetCompatibility = JavaVersion.VERSION_17
25+
}
26+
}
27+
2328
ext {
2429
otelVersion = '1.30.1'
2530
otelVersionAlpha = "${otelVersion}-alpha"
@@ -47,21 +52,18 @@ subprojects {
4752
exclude '**/*.js'
4853
}
4954

50-
if (JavaVersion.current().isJava11Compatible()) {
51-
// Code should be formatted using the latest googleJavaFormat, but it doesn't support Java <11 since version 1.8
52-
apply plugin: 'com.diffplug.spotless'
55+
apply plugin: 'com.diffplug.spotless'
5356

54-
spotless {
55-
java {
56-
target 'src/*/java/**/*.java'
57-
targetExclude '**/.idea/**'
58-
googleJavaFormat('1.16.0')
59-
}
57+
spotless {
58+
java {
59+
target 'src/*/java/**/*.java'
60+
targetExclude '**/.idea/**'
61+
googleJavaFormat('1.16.0')
6062
}
61-
62-
compileJava.dependsOn 'spotlessApply'
6363
}
6464

65+
compileJava.dependsOn 'spotlessApply'
66+
6567
test {
6668
useJUnitPlatform()
6769
}
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+
}

gradle.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
springBootPluginVersion=2.7.13
2+
3+
# use this for spring boot 3 support
4+
#springBootPluginVersion=3.2.0

springboot/build.gradle

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ dependencies {
1414
testImplementation "org.springframework.boot:spring-boot-starter-test"
1515
dependencies {
1616
errorproneJavac('com.google.errorprone:javac:9+181-r4173-1')
17-
if (JavaVersion.current().isJava11Compatible()) {
18-
errorprone('com.google.errorprone:error_prone_core:2.23.0')
19-
} else {
20-
errorprone('com.google.errorprone:error_prone_core:2.23.0')
21-
}
17+
errorprone('com.google.errorprone:error_prone_core:2.23.0')
2218
}
2319
}
2420

0 commit comments

Comments
 (0)