Skip to content

Commit 00eb355

Browse files
authored
Merge pull request #675 from fjtirado/refactor_java_task
Refactoring java executor to avoid instanceof at runtime
2 parents a980cd7 + 7817344 commit 00eb355

File tree

8 files changed

+269
-69
lines changed

8 files changed

+269
-69
lines changed

experimental/lambda/src/main/java/io/serverlessworkflow/impl/executors/func/JavaCallExecutor.java

Lines changed: 0 additions & 67 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
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+
package io.serverlessworkflow.impl.executors.func;
17+
18+
import io.serverlessworkflow.api.types.TaskBase;
19+
import io.serverlessworkflow.api.types.Workflow;
20+
import io.serverlessworkflow.api.types.func.CallJava;
21+
import io.serverlessworkflow.impl.TaskContext;
22+
import io.serverlessworkflow.impl.WorkflowApplication;
23+
import io.serverlessworkflow.impl.WorkflowContext;
24+
import io.serverlessworkflow.impl.WorkflowModel;
25+
import io.serverlessworkflow.impl.executors.CallableTask;
26+
import io.serverlessworkflow.impl.resources.ResourceLoader;
27+
import java.util.concurrent.CompletableFuture;
28+
import java.util.function.Consumer;
29+
30+
public class JavaConsumerCallExecutor implements CallableTask<CallJava.CallJavaConsumer> {
31+
32+
private Consumer consumer;
33+
34+
public void init(
35+
CallJava.CallJavaConsumer task,
36+
Workflow workflow,
37+
WorkflowApplication application,
38+
ResourceLoader loader) {
39+
consumer = task.consumer();
40+
}
41+
42+
@Override
43+
public CompletableFuture<WorkflowModel> apply(
44+
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
45+
consumer.accept(input.asJavaObject());
46+
return CompletableFuture.completedFuture(input);
47+
}
48+
49+
@Override
50+
public boolean accept(Class<? extends TaskBase> clazz) {
51+
return CallJava.CallJavaConsumer.class.isAssignableFrom(clazz);
52+
}
53+
}

experimental/lambda/src/main/java/io/serverlessworkflow/impl/executors/func/JavaForExecutorBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package io.serverlessworkflow.impl.executors.func;
1818

19-
import static io.serverlessworkflow.impl.executors.func.JavaCallExecutor.safeObject;
19+
import static io.serverlessworkflow.impl.executors.func.JavaFuncUtils.safeObject;
2020

2121
import io.serverlessworkflow.api.types.ForTask;
2222
import io.serverlessworkflow.api.types.Workflow;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
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+
package io.serverlessworkflow.impl.executors.func;
17+
18+
import io.serverlessworkflow.impl.WorkflowModel;
19+
20+
public class JavaFuncUtils {
21+
22+
static Object safeObject(Object obj) {
23+
return obj instanceof WorkflowModel model ? model.asJavaObject() : obj;
24+
}
25+
26+
private JavaFuncUtils() {}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
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+
package io.serverlessworkflow.impl.executors.func;
17+
18+
import io.serverlessworkflow.api.types.TaskBase;
19+
import io.serverlessworkflow.api.types.Workflow;
20+
import io.serverlessworkflow.api.types.func.CallJava;
21+
import io.serverlessworkflow.impl.TaskContext;
22+
import io.serverlessworkflow.impl.WorkflowApplication;
23+
import io.serverlessworkflow.impl.WorkflowContext;
24+
import io.serverlessworkflow.impl.WorkflowModel;
25+
import io.serverlessworkflow.impl.WorkflowModelFactory;
26+
import io.serverlessworkflow.impl.executors.CallableTask;
27+
import io.serverlessworkflow.impl.resources.ResourceLoader;
28+
import java.util.concurrent.CompletableFuture;
29+
import java.util.function.Function;
30+
31+
public class JavaFunctionCallExecutor implements CallableTask<CallJava.CallJavaFunction> {
32+
33+
private Function function;
34+
35+
public void init(
36+
CallJava.CallJavaFunction task,
37+
Workflow workflow,
38+
WorkflowApplication application,
39+
ResourceLoader loader) {
40+
function = task.function();
41+
}
42+
43+
@Override
44+
public CompletableFuture<WorkflowModel> apply(
45+
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
46+
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
47+
return CompletableFuture.completedFuture(
48+
modelFactory.fromAny(function.apply(input.asJavaObject())));
49+
}
50+
51+
@Override
52+
public boolean accept(Class<? extends TaskBase> clazz) {
53+
return CallJava.CallJavaFunction.class.isAssignableFrom(clazz);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
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+
package io.serverlessworkflow.impl.executors.func;
17+
18+
import static io.serverlessworkflow.impl.executors.func.JavaFuncUtils.safeObject;
19+
20+
import io.serverlessworkflow.api.types.TaskBase;
21+
import io.serverlessworkflow.api.types.Workflow;
22+
import io.serverlessworkflow.api.types.func.CallJava;
23+
import io.serverlessworkflow.impl.TaskContext;
24+
import io.serverlessworkflow.impl.WorkflowApplication;
25+
import io.serverlessworkflow.impl.WorkflowContext;
26+
import io.serverlessworkflow.impl.WorkflowModel;
27+
import io.serverlessworkflow.impl.WorkflowModelFactory;
28+
import io.serverlessworkflow.impl.executors.CallableTask;
29+
import io.serverlessworkflow.impl.expressions.LoopFunction;
30+
import io.serverlessworkflow.impl.resources.ResourceLoader;
31+
import java.util.concurrent.CompletableFuture;
32+
33+
public class JavaLoopFunctionCallExecutor implements CallableTask<CallJava.CallJavaLoopFunction> {
34+
35+
private LoopFunction function;
36+
private String varName;
37+
38+
public void init(
39+
CallJava.CallJavaLoopFunction task,
40+
Workflow workflow,
41+
WorkflowApplication application,
42+
ResourceLoader loader) {
43+
function = task.function();
44+
varName = task.varName();
45+
}
46+
47+
@Override
48+
public CompletableFuture<WorkflowModel> apply(
49+
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
50+
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
51+
return CompletableFuture.completedFuture(
52+
modelFactory.fromAny(
53+
function.apply(
54+
input.asJavaObject(), safeObject(taskContext.variables().get(varName)))));
55+
}
56+
57+
@Override
58+
public boolean accept(Class<? extends TaskBase> clazz) {
59+
60+
return CallJava.CallJavaLoopFunction.class.isAssignableFrom(clazz);
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
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+
package io.serverlessworkflow.impl.executors.func;
17+
18+
import static io.serverlessworkflow.impl.executors.func.JavaFuncUtils.safeObject;
19+
20+
import io.serverlessworkflow.api.types.TaskBase;
21+
import io.serverlessworkflow.api.types.Workflow;
22+
import io.serverlessworkflow.api.types.func.CallJava;
23+
import io.serverlessworkflow.impl.TaskContext;
24+
import io.serverlessworkflow.impl.WorkflowApplication;
25+
import io.serverlessworkflow.impl.WorkflowContext;
26+
import io.serverlessworkflow.impl.WorkflowModel;
27+
import io.serverlessworkflow.impl.WorkflowModelFactory;
28+
import io.serverlessworkflow.impl.executors.CallableTask;
29+
import io.serverlessworkflow.impl.expressions.LoopFunctionIndex;
30+
import io.serverlessworkflow.impl.resources.ResourceLoader;
31+
import java.util.concurrent.CompletableFuture;
32+
33+
public class JavaLoopFunctionIndexCallExecutor
34+
implements CallableTask<CallJava.CallJavaLoopFunctionIndex> {
35+
36+
private LoopFunctionIndex function;
37+
private String varName;
38+
private String indexName;
39+
40+
public void init(
41+
CallJava.CallJavaLoopFunctionIndex task,
42+
Workflow workflow,
43+
WorkflowApplication application,
44+
ResourceLoader loader) {
45+
function = task.function();
46+
varName = task.varName();
47+
indexName = task.indexName();
48+
}
49+
50+
@Override
51+
public CompletableFuture<WorkflowModel> apply(
52+
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
53+
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
54+
55+
return CompletableFuture.completedFuture(
56+
modelFactory.fromAny(
57+
function.apply(
58+
input.asJavaObject(),
59+
safeObject(taskContext.variables().get(varName)),
60+
(Integer) safeObject(taskContext.variables().get(indexName)))));
61+
}
62+
63+
@Override
64+
public boolean accept(Class<? extends TaskBase> clazz) {
65+
return CallJava.CallJavaLoopFunctionIndex.class.isAssignableFrom(clazz);
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
io.serverlessworkflow.impl.executors.func.JavaCallExecutor
1+
io.serverlessworkflow.impl.executors.func.JavaLoopFunctionIndexCallExecutor
2+
io.serverlessworkflow.impl.executors.func.JavaLoopFunctionCallExecutor
3+
io.serverlessworkflow.impl.executors.func.JavaFunctionCallExecutor
4+
io.serverlessworkflow.impl.executors.func.JavaConsumerCallExecutor

0 commit comments

Comments
 (0)