Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KeyWordList type to typed SA sample #607

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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