From 48d6d0d87fcf279a66afb0f06003e7f9da966280 Mon Sep 17 00:00:00 2001 From: Mason Egger Date: Wed, 1 May 2024 10:28:11 -0500 Subject: [PATCH] tweaks as part of Quinns review --- samples/custom-search-attributes/README.md | 3 +-- .../main/java/customsearchattributes/Constants.java | 3 +++ .../customsearchattributes/PizzaWorkflowImpl.java | 12 +++++------- .../main/java/customsearchattributes/Starter.java | 11 ++++------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/samples/custom-search-attributes/README.md b/samples/custom-search-attributes/README.md index 988e3a8..c4dc4bc 100644 --- a/samples/custom-search-attributes/README.md +++ b/samples/custom-search-attributes/README.md @@ -24,8 +24,7 @@ Within the Pizza Workflow code, we will now dynamically update Search Attributes ## Part D: Running Your Workflow 1. In one terminal, run `mvn exec:java -Dexec.mainClass='customsearchattributes.PizzaWorker` to start the Worker. -1. In another terminal and run `mvn exec:java -Dexec.mainClass='customsearchattributes.Starter'` to initiate execution of the Workflow. Now, try your query to retrieve the results. If you don't immediately see the correct result, wait a few seconds -and try again. Recall that it is possible to send a query before the updated Search Attribute has been recorded. +2. In another terminal and run `mvn exec:java -Dexec.mainClass='customsearchattributes.Starter'` to initiate execution of the Workflow. Now, try your query to retrieve the results. Recall that visibility is eventually consistent, so not all updates are going to be immediately reflected. If you don't see your update, wait a bit an try again. ## Part E: Querying Workflows by Search Attributes diff --git a/samples/custom-search-attributes/src/main/java/customsearchattributes/Constants.java b/samples/custom-search-attributes/src/main/java/customsearchattributes/Constants.java index 7f44e89..9810a89 100644 --- a/samples/custom-search-attributes/src/main/java/customsearchattributes/Constants.java +++ b/samples/custom-search-attributes/src/main/java/customsearchattributes/Constants.java @@ -1,7 +1,10 @@ package customsearchattributes; +import io.temporal.common.SearchAttributeKey; + public class Constants { public static final String TASK_QUEUE_NAME = "pizza-tasks"; + public static final SearchAttributeKey IS_ORDER_FAILED = SearchAttributeKey.forBoolean("isOrderFailed"); } diff --git a/samples/custom-search-attributes/src/main/java/customsearchattributes/PizzaWorkflowImpl.java b/samples/custom-search-attributes/src/main/java/customsearchattributes/PizzaWorkflowImpl.java index 2d378bb..45ee736 100644 --- a/samples/custom-search-attributes/src/main/java/customsearchattributes/PizzaWorkflowImpl.java +++ b/samples/custom-search-attributes/src/main/java/customsearchattributes/PizzaWorkflowImpl.java @@ -16,9 +16,7 @@ import customsearchattributes.exceptions.OutOfServiceAreaException; import java.time.Duration; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.slf4j.Logger; @@ -41,7 +39,7 @@ public OrderConfirmation orderPizza(PizzaOrder order) { boolean isDelivery = order.isDelivery(); Address address = order.getAddress(); - final SearchAttributeKey IS_ORDER_FAILED = SearchAttributeKey.forBoolean("isOrderFailed"); + logger.info("orderPizza Workflow Invoked"); @@ -56,14 +54,14 @@ public OrderConfirmation orderPizza(PizzaOrder order) { } catch (NullPointerException e) { logger.error("Unable to get distance"); - Workflow.upsertTypedSearchAttributes(IS_ORDER_FAILED.valueSet(true)); + Workflow.upsertTypedSearchAttributes(Constants.IS_ORDER_FAILED.valueSet(true)); throw new NullPointerException("Unable to get distance"); } if (isDelivery && (distance.getKilometers() > 25)) { logger.error("Customer lives outside the service area"); - Workflow.upsertTypedSearchAttributes(IS_ORDER_FAILED.valueSet(true)); + Workflow.upsertTypedSearchAttributes(Constants.IS_ORDER_FAILED.valueSet(true)); throw ApplicationFailure.newFailure("Customer lives outside the service area", OutOfServiceAreaException.class.getName()); } @@ -81,11 +79,11 @@ public OrderConfirmation orderPizza(PizzaOrder order) { confirmation = activities.sendBill(bill); } catch (InvalidChargeAmountException e) { logger.error("Unable to bill customer"); - Workflow.upsertTypedSearchAttributes(IS_ORDER_FAILED.valueSet(true)); + Workflow.upsertTypedSearchAttributes(Constants.IS_ORDER_FAILED.valueSet(true)); throw Workflow.wrap(new InvalidChargeAmountException("Unable to bill customer")); } - Workflow.upsertTypedSearchAttributes(IS_ORDER_FAILED.valueSet(false)); + Workflow.upsertTypedSearchAttributes(Constants.IS_ORDER_FAILED.valueSet(false)); return confirmation; } } diff --git a/samples/custom-search-attributes/src/main/java/customsearchattributes/Starter.java b/samples/custom-search-attributes/src/main/java/customsearchattributes/Starter.java index 248f0b4..0c93003 100644 --- a/samples/custom-search-attributes/src/main/java/customsearchattributes/Starter.java +++ b/samples/custom-search-attributes/src/main/java/customsearchattributes/Starter.java @@ -3,6 +3,7 @@ import io.temporal.client.WorkflowClient; import io.temporal.client.WorkflowOptions; import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.common.SearchAttributes; import customsearchattributes.model.PizzaOrder; import customsearchattributes.model.Pizza; @@ -29,7 +30,7 @@ public static void main(String[] args) throws Exception { WorkflowOptions options = WorkflowOptions.newBuilder() .setWorkflowId(workflowID) .setTaskQueue(Constants.TASK_QUEUE_NAME) - .setSearchAttributes(generateSearchAttributes()) + .setTypedSearchAttributes(generateSearchAttributes()) .build(); PizzaWorkflow workflow = client.newWorkflowStub(PizzaWorkflow.class, options); @@ -55,11 +56,7 @@ private static PizzaOrder createPizzaOrder() { return order; } - private static Map generateSearchAttributes(){ - Map searchAttributes = new HashMap<>(); - - searchAttributes.put("isOrderFailed", false); - - return searchAttributes; + private static SearchAttributes generateSearchAttributes(){ + return SearchAttributes.newBuilder().set(Constants.IS_ORDER_FAILED, false).build(); } }