Skip to content

Commit

Permalink
update dsl sample, make it simpler, more domain specific
Browse files Browse the repository at this point in the history
Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo committed Jul 13, 2024
1 parent f666773 commit 544a305
Show file tree
Hide file tree
Showing 38 changed files with 316 additions and 2,071 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ See the README.md file in each main sample directory for cut/paste Gradle comman

- [**Money Batch**](/core/src/main/java/io/temporal/samples/moneybatch): Demonstrates a situation where a single deposit should be initiated for multiple withdrawals. For example, a seller might want to be paid once per fixed number of transactions. This sample can be easily extended to perform a payment based on more complex criteria, such as at a specific time or an accumulated amount. The sample also demonstrates how to Signal the Workflow when it executes (*Signal with start*). If the Workflow is already executing, it just receives the Signal. If it is not executing, then the Workflow executes first, and then the Signal is delivered to it. *Signal with start* is a "lazy" way to execute Workflows when Signaling them.

- [**Customer Application Approval DSL**](/core/src/main/java/io/temporal/samples/dsl): Demonstrates execution of a customer application approval workflow defined in a DSL (like JSON or YAML)
- [**DSL (JSON)**](/core/src/main/java/io/temporal/samples/dsl): Demonstrates using domain specific language (DSL) defined in JSON to specify sequence of steps to be performed in our workflow.

- [**Polling Services**](/core/src/main/java/io/temporal/samples/polling): Recommended implementation of an activity that needs to periodically poll an external resource waiting its successful completion

Expand Down
4 changes: 0 additions & 4 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ dependencies {
implementation group: 'io.cloudevents', name: 'cloudevents-core', version: '3.0.0'
implementation group: 'io.cloudevents', name: 'cloudevents-api', version: '3.0.0'
implementation group: 'io.cloudevents', name: 'cloudevents-json-jackson', version: '3.0.0'
implementation group: 'io.serverlessworkflow', name: 'serverlessworkflow-api', version: '4.0.5.Final'
implementation group: 'io.serverlessworkflow', name: 'serverlessworkflow-validation', version: '4.0.5.Final'
implementation group: 'io.serverlessworkflow', name: 'serverlessworkflow-spi', version: '4.0.5.Final'
implementation group: 'io.serverlessworkflow', name: 'serverlessworkflow-util', version: '4.0.5.Final'
implementation group: 'net.thisptr', name: 'jackson-jq', version: '1.0.0-preview.20240207'

// we don't update it to 2.1.0 because 2.1.0 requires Java 11
Expand Down
12 changes: 4 additions & 8 deletions core/src/main/java/io/temporal/samples/dsl/DslActivities.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
package io.temporal.samples.dsl;

import io.temporal.activity.ActivityInterface;
import io.temporal.samples.dsl.model.ActResult;
import io.temporal.samples.dsl.model.Customer;

@ActivityInterface
public interface DslActivities {
ActResult checkCustomerInfo(Customer customer);
String one();

ActResult approveApplication(Customer customer);
String two();

ActResult rejectApplication(Customer customer);
String three();

ActResult updateApplicationInfo(Customer customer);

ActResult invokeBankingService(Customer customer);
String four();
}
49 changes: 17 additions & 32 deletions core/src/main/java/io/temporal/samples/dsl/DslActivitiesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,38 @@

package io.temporal.samples.dsl;

import io.temporal.activity.Activity;
import io.temporal.samples.dsl.model.ActResult;
import io.temporal.samples.dsl.model.Customer;
import java.util.concurrent.TimeUnit;

public class DslActivitiesImpl implements DslActivities {
@Override
public ActResult checkCustomerInfo(Customer customer) {
try {
return new ActResult(Activity.getExecutionContext().getInfo().getActivityType(), "invoked");
} catch (Exception e) {
return null;
}
public String one() {
sleep(1);
return "Activity one done...";
}

@Override
public ActResult updateApplicationInfo(Customer customer) {
try {
return new ActResult(Activity.getExecutionContext().getInfo().getActivityType(), "invoked");
} catch (Exception e) {
return null;
}
public String two() {
sleep(1);
return "Activity two done...";
}

@Override
public ActResult approveApplication(Customer customer) {
try {
return new ActResult("decision", "APPROVED");
} catch (Exception e) {
return null;
}
public String three() {
sleep(1);
return "Activity three done...";
}

@Override
public ActResult rejectApplication(Customer customer) {
try {
return new ActResult("decision-" + customer.getName(), "DENIED");
} catch (Exception e) {
return null;
}
public String four() {
sleep(1);
return "Activity four done...";
}

@Override
public ActResult invokeBankingService(Customer customer) {
private void sleep(int seconds) {
try {
return new ActResult(Activity.getExecutionContext().getInfo().getActivityType(), "invoked");
} catch (Exception e) {
return null;
Thread.sleep(TimeUnit.SECONDS.toMillis(seconds));
} catch (InterruptedException ee) {
// Empty
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,14 @@
* permissions and limitations under the License.
*/

package io.temporal.samples.dsl.model;
package io.temporal.samples.dsl;

public class ActResult {
private String type;
private String result;
import io.temporal.samples.dsl.model.Flow;
import io.temporal.workflow.WorkflowInterface;
import io.temporal.workflow.WorkflowMethod;

public ActResult() {}

public ActResult(String type, String result) {
this.type = type;
this.result = result;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
@WorkflowInterface
public interface DslWorkflow {
@WorkflowMethod
String run(Flow flow, String input);
}
76 changes: 0 additions & 76 deletions core/src/main/java/io/temporal/samples/dsl/DslWorkflowCache.java

This file was deleted.

68 changes: 68 additions & 0 deletions core/src/main/java/io/temporal/samples/dsl/DslWorkflowImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package io.temporal.samples.dsl;

import io.temporal.activity.ActivityOptions;
import io.temporal.common.RetryOptions;
import io.temporal.failure.ActivityFailure;
import io.temporal.failure.ApplicationFailure;
import io.temporal.samples.dsl.model.Flow;
import io.temporal.samples.dsl.model.FlowAction;
import io.temporal.workflow.ActivityStub;
import io.temporal.workflow.Workflow;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class DslWorkflowImpl implements DslWorkflow {
@Override
public String run(Flow flow, String input) {
if (flow == null || flow.getActions().isEmpty()) {
throw ApplicationFailure.newFailure(
"Flow is null or does not have any actions", "illegal flow");
}

try {
return runActions(flow, input);
} catch (ActivityFailure e) {
throw ApplicationFailure.newFailure(
"failing execution after compensation initiated", e.getCause().getClass().getName());
}
}

private String runActions(Flow flow, String input) {
List<String> results = new ArrayList<>();
for (FlowAction action : flow.getActions()) {
// build activity options based on flow action input
ActivityOptions.Builder activityOptionsBuilder = ActivityOptions.newBuilder();
activityOptionsBuilder.setStartToCloseTimeout(
Duration.ofSeconds(action.getStartToCloseSec()));
if (action.getRetries() > 0) {
activityOptionsBuilder.setRetryOptions(
RetryOptions.newBuilder().setMaximumAttempts(action.getRetries()).build());
}
// create untyped activity stub and run activity based on flow action
ActivityStub activityStub = Workflow.newUntypedActivityStub(activityOptionsBuilder.build());

results.add(activityStub.execute(action.getAction(), String.class, input));
}
return String.join(",", results);
}
}
Loading

0 comments on commit 544a305

Please sign in to comment.