Skip to content

Commit dc24cee

Browse files
feat(workflows): add execute with and without arguments (#10255)
* feat(workflows): add execute with and without arguments * fix lint issues * remove redundant code
1 parent 3fc7766 commit dc24cee

6 files changed

Lines changed: 394 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START workflows_myfirstworkflow_yaml_java]
16+
# This workflow accepts an optional "searchTerm" argument for the Wikipedia API.
17+
# If no input arguments are provided or "searchTerm" is absent,
18+
# it will fetch the day of the week in Amsterdam and use it as the search term.
19+
20+
main:
21+
params: [input]
22+
steps:
23+
- validateSearchTermAndRedirectToReadWikipedia:
24+
switch:
25+
- condition: '${map.get(input, "searchTerm") != null}'
26+
assign:
27+
- searchTerm: '${input.searchTerm}'
28+
next: readWikipedia
29+
- getCurrentTime:
30+
call: http.get
31+
args:
32+
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
33+
result: currentTime
34+
- setFromCallResult:
35+
assign:
36+
- searchTerm: '${currentTime.body.dayOfWeek}'
37+
- readWikipedia:
38+
call: http.get
39+
args:
40+
url: 'https://en.wikipedia.org/w/api.php'
41+
query:
42+
action: opensearch
43+
search: '${searchTerm}'
44+
result: wikiResult
45+
- returnOutput:
46+
return: '${wikiResult.body[1]}'
47+
# [END workflows_myfirstworkflow_yaml_java]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.workflows;
18+
19+
import com.google.cloud.workflows.executions.v1.CreateExecutionRequest;
20+
import com.google.cloud.workflows.executions.v1.Execution;
21+
import com.google.cloud.workflows.executions.v1.ExecutionsClient;
22+
import com.google.cloud.workflows.executions.v1.WorkflowName;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
// [START workflows_execute_with_arguments]
27+
28+
public class ExecuteWithArguments {
29+
public static void main(String[] args)
30+
throws IOException, InterruptedException, ExecutionException {
31+
String projectId = "your-project-id";
32+
String location = "your-location"; // For example: us-central1
33+
String workflowId = "your-workflow-id";
34+
35+
executeWorkflowWithArguments(projectId, location, workflowId);
36+
}
37+
38+
public static void executeWorkflowWithArguments(
39+
String projectId, String location, String workflowId)
40+
throws IOException, InterruptedException, ExecutionException {
41+
try (ExecutionsClient executionsClient = ExecutionsClient.create()) {
42+
WorkflowName parent = WorkflowName.of(projectId, location, workflowId);
43+
44+
CreateExecutionRequest executionRequest =
45+
CreateExecutionRequest.newBuilder()
46+
.setParent(parent.toString())
47+
.setExecution(
48+
Execution.newBuilder().setArgument("{\"searchTerm\":\"Cloud\"}").build())
49+
.build();
50+
51+
Execution executionCreated = executionsClient.createExecution(executionRequest);
52+
53+
System.out.println("Created execution: " + executionCreated.getName());
54+
55+
// Wait for execution to finish using exponential backoff
56+
long backoffDelay = 1_000;
57+
Execution execution;
58+
System.out.println("Poll for result...");
59+
while (true) {
60+
execution = executionsClient.getExecution(executionCreated.getName());
61+
62+
// Check if execution finished
63+
if (execution.getState() != Execution.State.ACTIVE) {
64+
break;
65+
}
66+
67+
// Wait using exponential backoff
68+
System.out.println("- Waiting for results...");
69+
Thread.sleep(backoffDelay);
70+
backoffDelay *= 2;
71+
}
72+
73+
System.out.println("Execution finished with state: " + execution.getState().name());
74+
System.out.println("Execution results: " + execution.getResult());
75+
}
76+
}
77+
}
78+
// [END workflows_execute_with_arguments]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.workflows;
18+
19+
// [START workflows_execute_without_arguments]
20+
21+
import com.google.cloud.workflows.executions.v1.CreateExecutionRequest;
22+
import com.google.cloud.workflows.executions.v1.Execution;
23+
import com.google.cloud.workflows.executions.v1.ExecutionsClient;
24+
import com.google.cloud.workflows.executions.v1.WorkflowName;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class ExecuteWithoutArguments {
29+
public static void main(String[] args)
30+
throws IOException, InterruptedException, ExecutionException {
31+
String projectId = "your-project-id";
32+
String location = "your-location"; // For example: us-central1
33+
String workflowId = "your-workflow-id";
34+
35+
executeWorkflowWithoutArguments(projectId, location, workflowId);
36+
}
37+
38+
public static void executeWorkflowWithoutArguments(
39+
String projectId, String location, String workflowId)
40+
throws IOException, InterruptedException, ExecutionException {
41+
try (ExecutionsClient executionsClient = ExecutionsClient.create()) {
42+
WorkflowName parent = WorkflowName.of(projectId, location, workflowId);
43+
44+
CreateExecutionRequest executionRequest =
45+
CreateExecutionRequest.newBuilder()
46+
.setParent(parent.toString())
47+
.setExecution(Execution.newBuilder().build())
48+
.build();
49+
50+
Execution executionCreated = executionsClient.createExecution(executionRequest);
51+
52+
System.out.println("Created execution: " + executionCreated.getName());
53+
54+
// Wait for execution to finish using exponential backoff
55+
long backoffDelay = 1_000;
56+
Execution execution;
57+
System.out.println("Poll for result...");
58+
while (true) {
59+
execution = executionsClient.getExecution(executionCreated.getName());
60+
61+
// Check if execution finished
62+
if (execution.getState() != Execution.State.ACTIVE) {
63+
break;
64+
}
65+
66+
// Wait using exponential backoff
67+
System.out.println("- Waiting for results...");
68+
Thread.sleep(backoffDelay);
69+
backoffDelay *= 2;
70+
}
71+
72+
System.out.println("Execution finished with state: " + execution.getState().name());
73+
System.out.println("Execution results: " + execution.getResult());
74+
}
75+
}
76+
}
77+
// [END workflows_execute_without_arguments]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.workflows;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import java.util.concurrent.ExecutionException;
27+
import org.junit.AfterClass;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class ExecuteWithArgumentsIT {
32+
private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
33+
private static String LOCATION = "us-central1";
34+
private static String WORKFLOW_ID = "java_myFirstWorkflow_" + UUID.randomUUID();
35+
private static ByteArrayOutputStream bout;
36+
37+
private static void requireEnvVar(String varName) {
38+
assertNotNull(
39+
"Environment variable " + varName + " is required to perform these tests.",
40+
System.getenv(varName));
41+
}
42+
43+
@BeforeClass
44+
public static void setUp() throws IOException, InterruptedException, ExecutionException {
45+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
46+
47+
// Create workflow
48+
Utils.createWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID);
49+
50+
bout = new ByteArrayOutputStream();
51+
System.setOut(new PrintStream(bout));
52+
}
53+
54+
@AfterClass
55+
public static void tearDown() throws IOException, InterruptedException, ExecutionException {
56+
// Delete workflow
57+
Utils.deleteWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID);
58+
59+
System.setOut(null);
60+
}
61+
62+
@Test
63+
public void testExecuteWorkflowWithArguments()
64+
throws IOException, InterruptedException, ExecutionException {
65+
ExecuteWithArguments.executeWorkflowWithArguments(PROJECT_ID, LOCATION, WORKFLOW_ID);
66+
67+
String output = bout.toString();
68+
assertThat(output).contains("Execution finished with state: SUCCEEDED");
69+
assertThat(output).contains("Execution results: ");
70+
assertThat(output).contains("Cloud");
71+
}
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.workflows;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import java.util.concurrent.ExecutionException;
27+
import org.junit.AfterClass;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class ExecuteWithoutArgumentsIT {
32+
private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
33+
private static String LOCATION = "us-central1";
34+
private static String WORKFLOW_ID = "java_myFirstWorkflow_" + UUID.randomUUID();
35+
private static ByteArrayOutputStream bout;
36+
37+
private static void requireEnvVar(String varName) {
38+
assertNotNull(
39+
"Environment variable " + varName + " is required to perform these tests.",
40+
System.getenv(varName));
41+
}
42+
43+
@BeforeClass
44+
public static void setUp() throws IOException, InterruptedException, ExecutionException {
45+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
46+
47+
// Create workflow
48+
Utils.createWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID);
49+
50+
bout = new ByteArrayOutputStream();
51+
System.setOut(new PrintStream(bout));
52+
}
53+
54+
@AfterClass
55+
public static void tearDown() throws IOException, InterruptedException, ExecutionException {
56+
// Delete workflow
57+
Utils.deleteWorkflow(PROJECT_ID, LOCATION, WORKFLOW_ID);
58+
59+
System.setOut(null);
60+
}
61+
62+
@Test
63+
public void testExecuteWorkflowWithoutArguments()
64+
throws IOException, InterruptedException, ExecutionException {
65+
ExecuteWithoutArguments.executeWorkflowWithoutArguments(PROJECT_ID, LOCATION, WORKFLOW_ID);
66+
67+
String output = bout.toString();
68+
assertThat(output).contains("Execution finished with state: SUCCEEDED");
69+
assertThat(output).contains("Execution results: ");
70+
}
71+
}

0 commit comments

Comments
 (0)