Skip to content

Commit

Permalink
Add KeyWordList type to typed SA sample (temporalio#607)
Browse files Browse the repository at this point in the history
* Add KeyWordList type to typed SA sample

Signed-off-by: Tihomir Surdilovic <[email protected]>

* formatting

Signed-off-by: Tihomir Surdilovic <[email protected]>

---------

Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo authored and antmendoza committed Apr 30, 2024
1 parent 31df9e4 commit a87db56
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,24 @@
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.
*
* <p>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:
*
* <p>temporal operator search-attribute create -name "CustomKeywordListField" -type "KeywordList"
*
* <p>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 +64,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 +111,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 +140,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 +152,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 +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_LONG_SA, 1l)
.set(CUSTOM_DOUBLE_SA, 0.1)
.set(CUSTOM_BOOL_SA, true)
Expand Down

0 comments on commit a87db56

Please sign in to comment.