Skip to content

Commit

Permalink
Add KeyWordList type to typed SA sample
Browse files Browse the repository at this point in the history
Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo committed Apr 3, 2024
1 parent 4058f18 commit 427bc6d
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,25 @@
import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

/**
* Sample Temporal workflow that demonstrates setting up, updating, and retrieving workflow search
* attributes using the typed search attributes API.
*
* NOTE: you may need to add these custom search attributes yourself before running the sample.
* If you are using autosetup image for service, you will need to create the "CustomKeywordListField"
* search attribute with Temporal cli, for example:
*
* temporal operator search-attribute create -name "CustomKeywordListField" -type "KeywordList"
*
* If you run your test and don't have some custom SA defined that are used here you would see error like:
* INVALID_ARGUMENT: Namespace default has no mapping defined for search attribute CustomBoolField
* when trying to start the workflow execution. In that case use cli to add the needed search attribute with its
* needed type.
*
*/
public class HelloTypedSearchAttributes {

Expand All @@ -50,6 +65,8 @@ public class HelloTypedSearchAttributes {
// Define all our search attributes with appropriate types
static final SearchAttributeKey<String> CUSTOM_KEYWORD_SA =
SearchAttributeKey.forKeyword("CustomKeywordField");
static final SearchAttributeKey<List<String>> CUSTOM_KEYWORD_LIST_SA =
SearchAttributeKey.forKeywordList("CustomKeywordListField");
static final SearchAttributeKey<Long> CUSTOM_LONG_SA =
SearchAttributeKey.forLong("CustomIntField");
static final SearchAttributeKey<Double> CUSTOM_DOUBLE_SA =
Expand Down Expand Up @@ -95,7 +112,7 @@ public interface GreetingWorkflow {
@ActivityInterface
public interface GreetingActivities {
@ActivityMethod
String composeGreeting(String greeting, String name);
String composeGreeting(String greeting, List<String> salutations, String name);
}

// Define the workflow implementation which implements our getGreeting workflow method.
Expand Down Expand Up @@ -124,8 +141,9 @@ public String getGreeting(String name) {
io.temporal.common.SearchAttributes searchAttributes = Workflow.getTypedSearchAttributes();
// Get a particular value out of the container using the typed key
String greeting = searchAttributes.get(CUSTOM_KEYWORD_SA);
List<String> salutations = searchAttributes.get(CUSTOM_KEYWORD_LIST_SA);
// This is a blocking call that returns only after the activity has completed.
return activities.composeGreeting(greeting, name);
return activities.composeGreeting(greeting, salutations, name);
}
}

Expand All @@ -135,8 +153,13 @@ public String getGreeting(String name) {
*/
static class GreetingActivitiesImpl implements GreetingActivities {
@Override
public String composeGreeting(String greeting, String name) {
return greeting + " " + name + "!";
public String composeGreeting(String greeting, List<String> salutations, String name) {
StringJoiner greetingJoiner = new StringJoiner(" ");
greetingJoiner.add(greeting);
greetingJoiner.add(name);
salutations.forEach(s -> greetingJoiner.add(s));

return greetingJoiner.toString();
}
}

Expand Down Expand Up @@ -211,6 +234,7 @@ public static void main(String[] args) {
private static io.temporal.common.SearchAttributes generateTypedSearchAttributes() {
return io.temporal.common.SearchAttributes.newBuilder()
.set(CUSTOM_KEYWORD_SA, "keyword")
.set(CUSTOM_KEYWORD_LIST_SA, Arrays.asList("how", "are", "you", "doing?"))
.set(CUSTOM_LONG_SA, 1l)
.set(CUSTOM_DOUBLE_SA, 0.1)
.set(CUSTOM_BOOL_SA, true)
Expand Down

0 comments on commit 427bc6d

Please sign in to comment.