• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #157

pending completion
#157

push

github-actions

web-flow
Provide SerializationContext for PayloadConverter and PayloadCodec (#1695)

Issue #1694

497 of 497 new or added lines in 32 files covered. (100.0%)

16942 of 20806 relevant lines covered (81.43%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.47
/temporal-sdk/src/main/java/io/temporal/internal/client/WorkflowClientRequestFactory.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.internal.client;
22

23
import static io.temporal.internal.common.HeaderUtils.toHeaderGrpc;
24
import static io.temporal.internal.common.SerializerUtils.toRetryPolicy;
25

26
import com.google.common.base.MoreObjects;
27
import com.google.common.base.Strings;
28
import com.google.protobuf.ByteString;
29
import io.temporal.api.common.v1.*;
30
import io.temporal.api.enums.v1.HistoryEventFilterType;
31
import io.temporal.api.taskqueue.v1.TaskQueue;
32
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest;
33
import io.temporal.api.workflowservice.v1.SignalWithStartWorkflowExecutionRequest;
34
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequest;
35
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequestOrBuilder;
36
import io.temporal.client.WorkflowClientOptions;
37
import io.temporal.client.WorkflowOptions;
38
import io.temporal.common.RetryOptions;
39
import io.temporal.common.context.ContextPropagator;
40
import io.temporal.internal.common.ProtobufTimeUtils;
41
import io.temporal.internal.common.SearchAttributesUtil;
42
import java.util.*;
43
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
45

46
final class WorkflowClientRequestFactory {
47
  private final WorkflowClientOptions clientOptions;
48

49
  public WorkflowClientRequestFactory(WorkflowClientOptions clientOptions) {
50
    this.clientOptions = clientOptions;
51
  }
52

53
  // If you add anything new here, keep newSignalWithStartWorkflowExecutionRequest in sync
54
  @Nonnull
55
  StartWorkflowExecutionRequest.Builder newStartWorkflowExecutionRequest(
56
      @Nonnull String workflowId,
57
      @Nonnull String workflowTypeName,
58
      @Nonnull io.temporal.common.interceptors.Header header,
59
      @Nonnull WorkflowOptions options,
60
      @Nullable Payloads inputArgs,
61
      @Nullable Memo memo) {
62
    StartWorkflowExecutionRequest.Builder request =
63
        StartWorkflowExecutionRequest.newBuilder()
64
            .setNamespace(clientOptions.getNamespace())
65
            .setRequestId(generateUniqueId())
66
            .setIdentity(clientOptions.getIdentity())
67
            .setWorkflowId(workflowId)
68
            .setWorkflowType(WorkflowType.newBuilder().setName(workflowTypeName))
69
            .setWorkflowRunTimeout(
70
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowRunTimeout()))
71
            .setWorkflowExecutionTimeout(
72
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowExecutionTimeout()))
73
            .setWorkflowTaskTimeout(
74
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowTaskTimeout()));
75

76
    if (inputArgs != null) {
77
      request.setInput(inputArgs);
78
    }
79

80
    if (options.getWorkflowIdReusePolicy() != null) {
81
      request.setWorkflowIdReusePolicy(options.getWorkflowIdReusePolicy());
82
    }
83

84
    String taskQueue = options.getTaskQueue();
85
    if (taskQueue != null && !taskQueue.isEmpty()) {
86
      request.setTaskQueue(TaskQueue.newBuilder().setName(taskQueue).build());
87
    }
88

89
    RetryOptions retryOptions = options.getRetryOptions();
90
    if (retryOptions != null) {
91
      request.setRetryPolicy(toRetryPolicy(retryOptions));
92
    }
93

94
    if (!Strings.isNullOrEmpty(options.getCronSchedule())) {
95
      request.setCronSchedule(options.getCronSchedule());
96
    }
97

98
    if (memo != null) {
99
      request.setMemo(memo);
100
    }
101

102
    if (options.getSearchAttributes() != null && !options.getSearchAttributes().isEmpty()) {
103
      request.setSearchAttributes(SearchAttributesUtil.encode(options.getSearchAttributes()));
104
    }
105

106
    Header grpcHeader =
107
        toHeaderGrpc(header, extractContextsAndConvertToBytes(options.getContextPropagators()));
108
    request.setHeader(grpcHeader);
109

110
    return request;
111
  }
112

113
  @Nonnull
114
  SignalWithStartWorkflowExecutionRequest.Builder newSignalWithStartWorkflowExecutionRequest(
115
      @Nonnull StartWorkflowExecutionRequestOrBuilder startParameters,
116
      @Nonnull String signalName,
117
      @Nullable Payloads signalInput) {
118
    SignalWithStartWorkflowExecutionRequest.Builder request =
119
        SignalWithStartWorkflowExecutionRequest.newBuilder()
120
            .setNamespace(clientOptions.getNamespace())
121
            .setRequestId(generateUniqueId())
122
            .setIdentity(clientOptions.getIdentity())
123
            .setSignalName(signalName)
124
            .setWorkflowRunTimeout(startParameters.getWorkflowRunTimeout())
125
            .setWorkflowExecutionTimeout(startParameters.getWorkflowExecutionTimeout())
126
            .setWorkflowTaskTimeout(startParameters.getWorkflowTaskTimeout())
127
            .setWorkflowType(startParameters.getWorkflowType())
128
            .setWorkflowIdReusePolicy(startParameters.getWorkflowIdReusePolicy())
129
            .setCronSchedule(startParameters.getCronSchedule());
130

131
    String workflowId = startParameters.getWorkflowId();
132
    if (workflowId.isEmpty()) {
133
      workflowId = generateUniqueId();
134
    }
135
    request.setWorkflowId(workflowId);
136

137
    if (signalInput != null) {
138
      request.setSignalInput(signalInput);
139
    }
140

141
    if (startParameters.hasInput()) {
142
      request.setInput(startParameters.getInput());
143
    }
144

145
    if (startParameters.hasTaskQueue()) {
146
      request.setTaskQueue(startParameters.getTaskQueue());
147
    }
148

149
    if (startParameters.hasRetryPolicy()) {
150
      request.setRetryPolicy(startParameters.getRetryPolicy());
151
    }
152

153
    if (startParameters.hasMemo()) {
154
      request.setMemo(startParameters.getMemo());
155
    }
156

157
    if (startParameters.hasSearchAttributes()) {
158
      request.setSearchAttributes(startParameters.getSearchAttributes());
159
    }
160

161
    if (startParameters.hasHeader()) {
162
      request.setHeader(startParameters.getHeader());
163
    }
164

165
    return request;
166
  }
167

168
  @Nonnull
169
  GetWorkflowExecutionHistoryRequest newHistoryLongPollRequest(
170
      WorkflowExecution workflowExecution, ByteString pageToken) {
171
    return GetWorkflowExecutionHistoryRequest.newBuilder()
172
        .setNamespace(clientOptions.getNamespace())
173
        .setExecution(workflowExecution)
174
        .setHistoryEventFilterType(HistoryEventFilterType.HISTORY_EVENT_FILTER_TYPE_CLOSE_EVENT)
175
        .setWaitNewEvent(true)
176
        .setNextPageToken(pageToken)
177
        .build();
178
  }
179

180
  private io.temporal.common.interceptors.Header extractContextsAndConvertToBytes(
181
      List<ContextPropagator> workflowOptionsContextPropagators) {
182
    List<ContextPropagator> workflowClientContextPropagators =
183
        clientOptions.getContextPropagators();
184
    if ((workflowClientContextPropagators.isEmpty() && workflowOptionsContextPropagators == null)
185
        || (workflowOptionsContextPropagators != null
186
            && workflowOptionsContextPropagators.isEmpty())) {
187
      return null;
188
    }
189

190
    List<ContextPropagator> listToUse =
191
        MoreObjects.firstNonNull(
192
            workflowOptionsContextPropagators, workflowClientContextPropagators);
193
    Map<String, Payload> result = new HashMap<>();
194
    for (ContextPropagator propagator : listToUse) {
195
      result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
196
    }
197
    return new io.temporal.common.interceptors.Header(result);
198
  }
199

200
  private static String generateUniqueId() {
201
    return UUID.randomUUID().toString();
202
  }
203
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc