From 427bc6d70fc60402a616c4d78e978b772c0148d8 Mon Sep 17 00:00:00 2001 From: Tihomir Surdilovic Date: Wed, 3 Apr 2024 09:54:31 -0400 Subject: [PATCH 1/2] Add KeyWordList type to typed SA sample Signed-off-by: Tihomir Surdilovic --- .../hello/HelloTypedSearchAttributes.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java b/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java index 589778c1..1527d8a9 100644 --- a/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java +++ b/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java @@ -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 { @@ -50,6 +65,8 @@ public class HelloTypedSearchAttributes { // Define all our search attributes with appropriate types static final SearchAttributeKey CUSTOM_KEYWORD_SA = SearchAttributeKey.forKeyword("CustomKeywordField"); + static final SearchAttributeKey> CUSTOM_KEYWORD_LIST_SA = + SearchAttributeKey.forKeywordList("CustomKeywordListField"); static final SearchAttributeKey CUSTOM_LONG_SA = SearchAttributeKey.forLong("CustomIntField"); static final SearchAttributeKey CUSTOM_DOUBLE_SA = @@ -95,7 +112,7 @@ public interface GreetingWorkflow { @ActivityInterface public interface GreetingActivities { @ActivityMethod - String composeGreeting(String greeting, String name); + String composeGreeting(String greeting, List salutations, String name); } // Define the workflow implementation which implements our getGreeting workflow method. @@ -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 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); } } @@ -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 salutations, String name) { + StringJoiner greetingJoiner = new StringJoiner(" "); + greetingJoiner.add(greeting); + greetingJoiner.add(name); + salutations.forEach(s -> greetingJoiner.add(s)); + + return greetingJoiner.toString(); } } @@ -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) From bc5adc84e1e3c2ad8b2dc12b076c4b936d84365b Mon Sep 17 00:00:00 2001 From: Tihomir Surdilovic Date: Wed, 3 Apr 2024 09:55:53 -0400 Subject: [PATCH 2/2] formatting Signed-off-by: Tihomir Surdilovic --- .../hello/HelloTypedSearchAttributes.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java b/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java index 1527d8a9..df196d76 100644 --- a/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java +++ b/core/src/main/java/io/temporal/samples/hello/HelloTypedSearchAttributes.java @@ -42,17 +42,16 @@ * 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: + *

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. + *

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 { @@ -66,7 +65,7 @@ public class HelloTypedSearchAttributes { static final SearchAttributeKey CUSTOM_KEYWORD_SA = SearchAttributeKey.forKeyword("CustomKeywordField"); static final SearchAttributeKey> CUSTOM_KEYWORD_LIST_SA = - SearchAttributeKey.forKeywordList("CustomKeywordListField"); + SearchAttributeKey.forKeywordList("CustomKeywordListField"); static final SearchAttributeKey CUSTOM_LONG_SA = SearchAttributeKey.forLong("CustomIntField"); static final SearchAttributeKey CUSTOM_DOUBLE_SA = @@ -234,7 +233,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_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)