Skip to content

Commit

Permalink
tweaks as part of Quinns review
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonEgger committed May 1, 2024
1 parent 42dbd75 commit 48d6d0d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
3 changes: 1 addition & 2 deletions samples/custom-search-attributes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean> IS_ORDER_FAILED = SearchAttributeKey.forBoolean("isOrderFailed");

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,7 +39,7 @@ public OrderConfirmation orderPizza(PizzaOrder order) {
boolean isDelivery = order.isDelivery();
Address address = order.getAddress();

final SearchAttributeKey<Boolean> IS_ORDER_FAILED = SearchAttributeKey.forBoolean("isOrderFailed");


logger.info("orderPizza Workflow Invoked");

Expand All @@ -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());
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -55,11 +56,7 @@ private static PizzaOrder createPizzaOrder() {
return order;
}

private static Map<String, Object> generateSearchAttributes(){
Map<String, Object> searchAttributes = new HashMap<>();

searchAttributes.put("isOrderFailed", false);

return searchAttributes;
private static SearchAttributes generateSearchAttributes(){
return SearchAttributes.newBuilder().set(Constants.IS_ORDER_FAILED, false).build();
}
}

0 comments on commit 48d6d0d

Please sign in to comment.