Search in sources :

Example 1 with DataConverter

use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.

the class InternalUtils method convertMapToSearchAttributes.

public static SearchAttributes convertMapToSearchAttributes(Map<String, Object> searchAttributes) {
    DataConverter converter = DataConverter.getDefaultInstance();
    Map<String, Payload> mapOfByteBuffer = new HashMap<>();
    searchAttributes.forEach((key, value) -> mapOfByteBuffer.put(key, converter.toPayload(value).get()));
    return SearchAttributes.newBuilder().putAllIndexedFields(mapOfByteBuffer).build();
}
Also used : HashMap(java.util.HashMap) Payload(io.temporal.api.common.v1.Payload) DataConverter(io.temporal.common.converter.DataConverter)

Example 2 with DataConverter

use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.

the class WorkflowContextTest method TestMergeSearchAttributes.

@Test
public void TestMergeSearchAttributes() {
    WorkflowExecutionStartedEventAttributes startAttr = WorkflowExecutionStartedEventAttributes.getDefaultInstance();
    WorkflowContext workflowContext = new WorkflowContext("namespace", null, startAttr, 0, null);
    DataConverter converter = DataConverter.getDefaultInstance();
    Map<String, Payload> indexedFields = new HashMap<>();
    indexedFields.put("CustomKeywordField", converter.toPayload("key").get());
    SearchAttributes searchAttributes = SearchAttributes.newBuilder().putAllIndexedFields(indexedFields).build();
    workflowContext.mergeSearchAttributes(searchAttributes);
    assertEquals("key", SearchAttributesUtil.getValueFromSearchAttributes(workflowContext.getSearchAttributes(), "CustomKeywordField", String.class));
}
Also used : HashMap(java.util.HashMap) SearchAttributes(io.temporal.api.common.v1.SearchAttributes) Payload(io.temporal.api.common.v1.Payload) WorkflowExecutionStartedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes) DataConverter(io.temporal.common.converter.DataConverter) Test(org.junit.Test)

Example 3 with DataConverter

use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.

the class SyncWorkflowContext method sideEffect.

@Override
public <R> R sideEffect(Class<R> resultClass, Type resultType, Func<R> func) {
    try {
        DataConverter dataConverter = getDataConverter();
        CompletablePromise<Optional<Payloads>> result = Workflow.newPromise();
        context.sideEffect(() -> {
            R r = func.apply();
            return dataConverter.toPayloads(r);
        }, (p) -> runner.executeInWorkflowThread("side-effect-callback", () -> result.complete(Objects.requireNonNull(p))));
        return dataConverter.fromPayloads(0, result.get(), resultClass, resultType);
    } catch (Exception e) {
        // fail the workflow task by throwing an Error.
        throw new Error(e);
    }
}
Also used : DataConverter(io.temporal.common.converter.DataConverter) ChildWorkflowTaskFailedException(io.temporal.internal.replay.ChildWorkflowTaskFailedException) WorkflowException(io.temporal.client.WorkflowException)

Example 4 with DataConverter

use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.

the class SearchAttributesTest method testSearchAttributes.

@Test
public void testSearchAttributes() {
    if (testWorkflowRule.isUseExternalService()) {
        // LocalDateTime fails to deserialize in the real service, with a message like
        // INVALID_ARGUMENT: 2021-08-26T13:21:52.059738 is not a valid value for search attribute
        // CustomDatetimeField of type Datetime
        // Tracked in https://github.com/temporalio/sdk-java/issues/673
        searchAttributes.remove(testKeyDateTime);
    }
    WorkflowOptions workflowOptions = SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()).toBuilder().setSearchAttributes(searchAttributes).build();
    TestNoArgsWorkflowFunc stubF = testWorkflowRule.getWorkflowClient().newWorkflowStub(TestNoArgsWorkflowFunc.class, workflowOptions);
    WorkflowExecution executionF = WorkflowClient.start(stubF::func);
    GetWorkflowExecutionHistoryResponse historyResp = WorkflowClientHelper.getHistoryPage(testWorkflowRule.getTestEnvironment().getWorkflowService(), SDKTestWorkflowRule.NAMESPACE, executionF, ByteString.EMPTY, new NoopScope());
    HistoryEvent startEvent = historyResp.getHistory().getEvents(0);
    SearchAttributes searchAttrFromEvent = startEvent.getWorkflowExecutionStartedEventAttributes().getSearchAttributes();
    Map<String, Payload> fieldsMap = searchAttrFromEvent.getIndexedFieldsMap();
    Payload searchAttrStringBytes = fieldsMap.get(testKeyString);
    DataConverter converter = DataConverter.getDefaultInstance();
    String retrievedString = converter.fromPayload(searchAttrStringBytes, String.class, String.class);
    assertEquals(testValueString, retrievedString);
    Payload searchAttrIntegerBytes = fieldsMap.get(testKeyInteger);
    Integer retrievedInteger = converter.fromPayload(searchAttrIntegerBytes, Integer.class, Integer.class);
    assertEquals(testValueInteger, retrievedInteger);
    Payload searchAttrDateTimeBytes = fieldsMap.get(testKeyDateTime);
    if (!testWorkflowRule.isUseExternalService()) {
        LocalDateTime retrievedDateTime = converter.fromPayload(searchAttrDateTimeBytes, LocalDateTime.class, LocalDateTime.class);
        assertEquals(testValueDateTime, retrievedDateTime);
    }
    Payload searchAttrBoolBytes = fieldsMap.get(testKeyBool);
    Boolean retrievedBool = converter.fromPayload(searchAttrBoolBytes, Boolean.class, Boolean.class);
    assertEquals(testValueBool, retrievedBool);
    Payload searchAttrDoubleBytes = fieldsMap.get(testKeyDouble);
    Double retrievedDouble = converter.fromPayload(searchAttrDoubleBytes, Double.class, Double.class);
    assertEquals(testValueDouble, retrievedDouble);
}
Also used : LocalDateTime(java.time.LocalDateTime) SearchAttributes(io.temporal.api.common.v1.SearchAttributes) TestNoArgsWorkflowFunc(io.temporal.workflow.shared.TestMultiArgWorkflowFunctions.TestNoArgsWorkflowFunc) ByteString(com.google.protobuf.ByteString) HistoryEvent(io.temporal.api.history.v1.HistoryEvent) WorkflowOptions(io.temporal.client.WorkflowOptions) WorkflowExecution(io.temporal.api.common.v1.WorkflowExecution) Payload(io.temporal.api.common.v1.Payload) GetWorkflowExecutionHistoryResponse(io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryResponse) NoopScope(com.uber.m3.tally.NoopScope) DataConverter(io.temporal.common.converter.DataConverter) Test(org.junit.Test)

Example 5 with DataConverter

use of io.temporal.common.converter.DataConverter in project samples-java by temporalio.

the class HelloSearchAttributes method getKeywordFromSearchAttribute.

// example for extracting a value from search attributes
private static String getKeywordFromSearchAttribute(SearchAttributes searchAttributes) {
    Payload field = searchAttributes.getIndexedFieldsOrThrow("CustomKeywordField");
    DataConverter dataConverter = DataConverter.getDefaultInstance();
    return dataConverter.fromPayload(field, String.class, String.class);
}
Also used : Payload(io.temporal.api.common.v1.Payload) DataConverter(io.temporal.common.converter.DataConverter)

Aggregations

DataConverter (io.temporal.common.converter.DataConverter)5 Payload (io.temporal.api.common.v1.Payload)4 SearchAttributes (io.temporal.api.common.v1.SearchAttributes)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ByteString (com.google.protobuf.ByteString)1 NoopScope (com.uber.m3.tally.NoopScope)1 WorkflowExecution (io.temporal.api.common.v1.WorkflowExecution)1 HistoryEvent (io.temporal.api.history.v1.HistoryEvent)1 WorkflowExecutionStartedEventAttributes (io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes)1 GetWorkflowExecutionHistoryResponse (io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryResponse)1 WorkflowException (io.temporal.client.WorkflowException)1 WorkflowOptions (io.temporal.client.WorkflowOptions)1 ChildWorkflowTaskFailedException (io.temporal.internal.replay.ChildWorkflowTaskFailedException)1 TestNoArgsWorkflowFunc (io.temporal.workflow.shared.TestMultiArgWorkflowFunctions.TestNoArgsWorkflowFunc)1 LocalDateTime (java.time.LocalDateTime)1